r/webdev • • 18d ago

beginner here, why y'all use rem for almost everything? what if user increase the base font will it messed up the layout or your web?

why, can you give some tips, thank you so much

43 Upvotes

39 comments sorted by

169

u/pxlschbsr 18d ago

If your layout breaks when a user increases/decreases their default font size, it's been badly designed.

Using rem is tied directly to accessibility, readability and user experience, because it's the baseline of what can be done to cater to a single users individual need.

11

u/bhison 17d ago

Use rem for anything that scales with zoom e.g. not borders or padding - unless you have a conscious reason thy you need them to.

-3

u/pxlschbsr 17d ago

I think that's self-explanatory

23

u/cuelark 18d ago

A practical split is rem for type and spacing you want to scale with the root, and px for borders or shadows. Keep containers flexible with grid or flex and avoid fixed heights so larger text can wrap. Test with browser zoom and a bigger default font too, since they catch slightly different layout issues.

1

u/testingaurora 17d ago

Ive started using em for borders, outlines, and shadows now 😆

3

u/YourMatt 16d ago

I remember starting to do that too, lol. It lasted one project.

2

u/cuelark 17d ago

em makes sense when you want the effect to scale with that component's text. for a generic 1px border, i'd keep it in px so it doesn't get thicker just because the text changed.

1

u/testingaurora 16d ago

Yes I know. I also set padding and spacing in em so it scales with the element , eg buttons everything keeps the same ratio as it grows, clamped so ut didn't get too big

39

u/Vegetable-Capital-54 18d ago edited 18d ago

Generally good web design should account for any font size, within reason. You should not rely on the size of font, or length of the text to be static. If your layout breaks because the font got a few pixels larger, or user translated the page to German, that's poor workmanship.

7

u/Legitimate-Lock9965 18d ago

well thats the point if your user changes their base font size your elements should scale with it.

if your layout is breaking as a result, then its likely youve gone wrong somewhere

6

u/Ok-Snow-6553 17d ago

The whole point of rem is precisely so that the layout doesn't get messed up when users increase their base font size.

px is rigid. If a visually impaired user changes their browser default to 24px, all your px fonts stay tiny. But rem scales based on that root size. If you set font-size: 1rem and padding: 1rem, the font AND the padding will scale up together. That's called accessibility (a11y).

28

u/rhooManu full-stack 18d ago

Precisely so everything changes gracefully if a user changes the base font size. That's the purpose of it.

But not everything has to be rem. The intrication of how things should shrink or grow is a long road of learning. As a basic, remember at leat font size in rem, line-height in em. That's a safe start.

31

u/simonraynor 18d ago

Line height should be unitless

14

u/RotationSurgeon 10yr Lead FED turned Product Manager 18d ago

For anybody wondering why: this forces li e height to be based on the local font size instead of accidentally or incorrectly inheriting a parent element’s line height or scaling based on the wrong computed value.

4

u/Ok_Car_8951 18d ago

It won't mess up the layout as long as you let things flow instead of pinning heights. The usual gotcha is fixed heights on buttons, headers and cards, since bigger text then overflows the box. Use min-height or just padding in rem so the box grows with the text. Media queries in rem (like 48rem instead of 768px) also help, because the breakpoint moves with the user's font size and your layout switches to a single column sooner. Easy test: set your browser's default font size to 200% and click around your site.

3

u/Proud-Wish7823 18d ago

Users often increase their root font size for accessibility reasons, ie: they need the text to be bigger to read it more clearly, so using rem for font sizes allows a user to do this, and subsequently using rem for other components is precisely what allows a design to scale up relatively when a user increases the root font size (and therefore _not_ break)

3

u/ExecutiveChimp 18d ago

what if user increase the base font

You should be testing for that.

1

u/theoneandlonely1 18d ago

Easy there is REM for root and then you control base with EM

1

u/Open-Adhesiveness-86 18d ago

quick way to actually test it: set the browser's default font size to 24px in settings (not ctrl+zoom, that scales px too) and reload. if nothing changes you sized everything in px. and set html { font-size: 100% } rather than 16px, otherwise you've hardcoded over the user's choice and rem buys you nothing.

1

u/called9527 18d ago

easiest way to see it for yourself: go into browser settings, bump the default font size way up, then click around a few sites. the rem ones mostly just get bigger and still hold together. where stuff usually breaks is fixed heights and widths, text spilling out of a box that was sized in px. so it's less rem messing up the layout and more the layout not being allowed to grow

1

u/thekwoka 18d ago

That's kind of the point...

1

u/United_Baseball_3040 18d ago

Nah it only breaks if u hardcode fixed heights on boxes and buttons. just let stuff grow naturally with flex/grid and use min-height instead, works fine every time

1

u/Vast_Work5283 18d ago

rem is mostly for respecting the user’s font-size settings, and a decent responsive layout should reflow instead of exploding when the base size changes; if changing it wrecks everything, the layout

1

u/calimio6 front-end 18d ago

Zooming on a site that relies on rem is a blessing. I use them every where but borders

1

u/Senior-Assumption864 17d ago

did you test with a larger default font size too?

1

u/armahillo rails 17d ago

Have you read through the “ems and rems” section of this article already?

https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/Values_and_units

1

u/Eugene_Kosten 17d ago

one gotcha with mixing rem and vw in CSS clamp is that if the vw value takes over, text-only browser zoom stops scaling the font enough to hit 200% for WCAG accessibility. keeping a solid rem base inside clamp gives you fluid sizing without breaking zoom.

1

u/MoonAndCodeStudio 16d ago

rem is relative to the root font size, which by default comes from the user's browser setting. So if someone sets their default font bigger because they need it, a site built in rem respects that. With px you basically override their choice.

If the layout breaks when the font gets bigger, that's usually a sign something else is off, like fixed heights or containers that don't wrap. A few tips:

- rem for font sizes and spacing
- px is fine for borders and tiny details
- avoid fixed heights on things that hold text, let them grow
- max-width in rem or ch keeps line length readable

Quick test: set your browser's default font to 20 or 24 and click around your site. You'll spot the problems fast

-1

u/Honey-Entire 18d ago

No one here actually seems to be answering “why,” so here’s my answer: it’s a skill/knowledge gap. Period.

Building a system using responsive units, particularly if you want to use the even-more-powerful-footgun, the “em” unit, isn’t as easy to do at a systems level as throwing px around everywhere. To do it properly, you need a design team that understands responsive units and design, then you need developers who know how to separate what design wants from how the browser works, then you need the diligence to have a team that establishes rules and testing procedures to make sure things continue to work and behave correctly as the system grows.

In my decade-plus of web dev experience, I’ve been on exactly one project where this worked well and it was entirely driven by the fact we had a 2-dev team with one designer and we were building a core design system and component library where we had 100% control over EVERYTHING.

13

u/[deleted] 18d ago

[removed] — view removed comment

10

u/ichsagedir 18d ago

In Firefox you can set the behavior of how Ctrl + behaves. A user can set it so that only font size scales.

So don't take the behavior of Ctrl + for granted

1

u/[deleted] 18d ago

[removed] — view removed comment

1

u/m_redditUser 18d ago

why line height unitless? examples? tnx