Replies: 3 comments 11 replies
-
Beta Was this translation helpful? Give feedback.
-
I just made a variant and added it to padding and margin. It is probably riddled with bugs from edge cases or when used on other things, but will be using it for a while to see how it goes. require('tailwindcss/plugin')(function({ addVariant }) {
addVariant('em', ({ container }) => {
container.walkRules(rule => {
rule.selector = `.em\\:${rule.selector.slice(1)}`;
rule.walkDecls((decl) => {
decl.value = decl.value.replace('rem', 'em');
});
})
})
}) |
Beta Was this translation helpful? Give feedback.
-
I need to use em instead of rem because I don't want my web component to be affected by the host page's styles. As a solution, I wrote this tiny PostCSS plugin, that replaces all occurrences of "rem" with "em" in the output CSS files: const convertRemToEm = {
postcssPlugin: 'convertRemToEm',
// When debugging this, https://astexplorer.net/#/2uBU1BLuJ1 is very helpful
Declaration(declaration) {
declaration.value = declaration.value.replaceAll(remRegex, 'em');
},
};
// Regex to find all occurrences of "rem" units
const remRegex = /(?<=\d)rem/g;
/** @type {import('postcss-load-config').Config} */
module.exports = {
plugins: [
// ... your other plugins here
// Add this plugin:
convertRemToEm,
],
}; Few more things to note:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I understand for the most part, you want to stick with REMs for simplicity. However, there are times where I want to create a scalable module.
For example https://codepen.io/chriscoyier/pen/tvheK
Another example is if I create a button with x/y padding, I want that padding relative to the font size of the button, not the root.
I'd like to hear what types of solutions people came up with and if there's an easy way to toggle EM vs REM for padding and margin utilities.
Beta Was this translation helpful? Give feedback.
All reactions