Back to articles
Why Your CSS Should Use Rem Units and How to Convert From Pixels

Why Your CSS Should Use Rem Units and How to Convert From Pixels

via Dev.to WebdevMichael Lip

Every CSS value you write in pixels is a decision to ignore user accessibility preferences. That sounds harsh, but it's technically accurate. When a user sets their browser's default font size to 20px instead of the standard 16px, they're telling every website: "I need larger text." Rem units respect this preference. Pixel units do not. The conversion 1rem = the root element's font size (usually 16px by default). To convert: rem = px / 16. That's it. Here's a reference table for the most common values: 10px = 0.625rem 12px = 0.75rem 14px = 0.875rem 16px = 1rem 18px = 1.125rem 20px = 1.25rem 24px = 1.5rem 28px = 1.75rem 32px = 2rem 36px = 2.25rem 40px = 2.5rem 48px = 3rem 64px = 4rem Where to use rem, where to keep pixels Use rem for: Font sizes (always) Spacing and padding on text-adjacent elements Max-widths for readable text containers Media query breakpoints (though these always use the browser default regardless) Keep pixels for: Borders (a 1px border should stay 1px) Box shadows V

Continue reading on Dev.to Webdev

Opens in a new tab

Read Full Article
2 views

Related Articles