tailwindcss-px-to-rem
v0.0.1
Published
A Tailwind CSS plugin automatically converts all REM units to PX units in your Tailwind styles.
Readme
TailwindCSS PX to REM Plugin This TailwindCSS plugin automatically converts pixel (px) values to rem units in your Tailwind styles. It provides a seamless way to use pixel-based values in your utility classes while maintaining consistency with Tailwind’s default rem-based system.
Why Use This Plugin? TailwindCSS generally uses rem for spacing utilities like h-10, which resolves to 2.5rem. However, in some cases, you may know the pixel value you need (e.g., h-40px) and want to work with it directly. This plugin makes it easier to use pixel values (like h-40px) while automatically converting them to rem for consistency with Tailwind's default unit system.
Use case: You know the pixel value you need, and instead of manually converting it to rem, you can use the px suffix (e.g., h-40px), and the plugin will handle the conversion. Benefit: Ensures consistent use of rem units in your layout while allowing flexibility to use pixel values where necessary. Installation Install the Plugin: bash 코드 복사 pnpm install tailwindcss-px-to-rem Configure TailwindCSS to Use the Plugin: In your tailwind.config.js, include the plugin:
js 코드 복사 const plugin = require('tailwindcss-px-to-rem');
module.exports = { plugins: [ plugin, ], }; Usage With this plugin, you can define utility classes using pixel values, and they will automatically be converted to rem units.
Example: html 코드 복사
How It Works The plugin listens for utility classes with a px suffix (e.g., h-40px) and automatically converts the pixel value to rem. This conversion ensures that your Tailwind setup remains consistent, using rem for spacing and other layout properties.
Example Plugin Code: ts 코드 복사 import plugin from 'tailwindcss/plugin';
export default plugin(function({ addUtilities, theme }) { const newUtilities = { '.h-10': { height: theme('spacing.10'), // 2.5rem }, '.h-40px': { height: '2.5rem', // 40px converted to rem }, };
addUtilities(newUtilities); }); Supported Classes Height: h-10, h-20px, h-40px, etc. Width: w-10, w-20px, w-40px, etc. You can extend this plugin to support additional properties like margin, padding, etc., depending on your needs.
Example Output After configuring and using the plugin, you can use both rem and px based classes like this:
html 코드 복사
