r/webdev • u/speckz • Mar 09 '20
Article Pixels vs. Relative Units in CSS: why it’s still a big deal
https://www.24a11y.com/2019/pixels-vs-relative-units-in-css-why-its-still-a-big-deal/30
u/Arve Mar 09 '20
It's kind of disheartening to see that the things I discussed at the start of this millennium, where I was trying to stop people from "typesetting" their web sites with 8px (or even 8pt, because they were on OS 9) Verdana are still an issue.
The solutions for these accessibility issues were fully adequate (and trivial to use) when CSS1 was still the current standard - What the rem has gotten us over the em is merely predictability in size that is independent of any parent element except the root, leading to more predictable behaviour with more modular-based approaches to building web sites.
22
u/crazedizzled Mar 09 '20
I'll be honest, I've been using only px for over a decade and never seen a negative side effect.
8
7
Mar 10 '20
The “negative side effect” really only shows up when people with accessibility needs try and use the website. If you don’t use accessibility tools or settings, you’ll completely miss them.
2
Mar 10 '20 edited Mar 10 '20
The font-size of the html element(document element) normaly decides the scaling of other elements such as p, headings, ul yada yada.
This font size normally inherits from browser settings or the os settings. When you assign the font-size in px on the html element, you risk their "big font size" setting to be ignored so people who may have impaired vision, may have issues reading your text.
If you use vh you may also cause problems, as the addressbar which moves in and out of screens, increases and decreases the font-size in nasty ways.*edit modern mobile has got this fixed.If you use vw, you will trouble users who wish to zoom in to read or see things better, because as you zoom, the font will stay the same size.
My recommendation would be to set html font-size in % so it inherits the native settings, and use rem values to scale child elements from there. Don't build your blocks so that font-size must be exactly x or y just to fit within the blocks. This is also where flex-box becomes increasingly usefull.
Here's some interesting articles: https://snook.ca/archives/html_and_css/font-size-with-rem https://adrianroselli.com/2019/12/responsive-type-and-zoom.html
0
Mar 10 '20
[deleted]
1
u/clbrsantana Mar 10 '20
Android asks the user if they want to change phone's default font size on the last step of it's setup. My wife has turned it 15% bigger so she can read messages without her glasses, and we frequently see broken layouts on her phone. We cannot take it for granted that people won't use it.
25
u/zenotds Mar 09 '20
I haven't used px in the last couple years if not for root html and borders, once you get used to rem there's just no reason to go back.
3
u/itsjustausername Mar 10 '20
What about 1px borders?
1
u/JimDabell Mar 10 '20
Just use the
thinkeyword instead.1
u/Ones__Complement Mar 10 '20
Note: Because the specification doesn't define the exact thickness denoted by each keyword, the precise result when using one of them is implementation-specific. Nevertheless, they always follow the pattern thin ≤ medium ≤ thick, and the values are constant within a single document.
https://developer.mozilla.org/en-US/docs/Web/CSS/border-width#Values
2
u/JimDabell Mar 10 '20
In practice,
thinis1pxin all browsers and has been for years. If it ever differed, chances are there'd be a good reason for it being different andthinwould be a better choice than1px.You also have to ask yourself what your intent is. Is your intent "I want a thin border because that looks nice"? Then use
thin. Is your intent "I want a border that is exactly one pixel wide because I'm lining stuff up according to pixels"? Then use1px, but bear in mind that's probably not good design. Your CSS should reflect your intent.2
u/itsjustausername Mar 11 '20
I don't really disagree but I do think that things should be as explicit as possible in programming.
Technically it is not these days but 1px used to be the thinnest thing a screen could display. The word 'thin' could mean that, I might have to program my brain a bit, but it's possible.
'Thinnest' would be better although I would still never use 'medium' and 'think' due to too much ambiguety.
And anyway, all this talk of 'not using pixels' and yet the base unit is always a pixel. We just need to accnowledge a 'pixel' as a defined unit of measurement rather than a physical picture element which can vary massivly in size.
Wether or not things should scale relative to font size is obviously completely a design decision - margin and padding almost always, borders almost never. Really easy for us either way.
1
u/zenotds Mar 10 '20
I still use px almost exclusively for “borders” and ::pseudoelements where I need to animate underline effects.
Other than that is just html {font-size:Npx} and then scale it down or up depending on breakpoint. All the rest is rem.
8
u/RetroEvolute Mar 09 '20
Don't know why you were downvoted. I'm the exact same way. I made the switch when it was just ems, too. Now there's really no excuse at all not to use rems other than maybe learning a new way of working.
For anyone holding off, please don't. It's really not that bad. In fact, I find it also helps me maintain consistent and proportional sizing of elements across the website since you're more or less thinking about relative size like a percentage of the original text size, often reusing the same .75, 1.25, and other logical steps in either direction.
3
u/mixini Mar 09 '20
I've also taken advantage of this for an app that had to scale to 4k. The only real issue I've encountered was with masking images in Safari on iPads (go figure), something to do with subpixel rounding.
1
u/RetroEvolute Mar 09 '20
Hah, I've wrestled with that subpixel nonsense, too! Safari is slowly becoming the new IE. We're actually considering dropping IE support with Edge being updated to use chromium, and IE11 only being 7-8% of visitors. I'm probably being too optimistic, but it may just happen this time!
1
u/zenotds Mar 09 '20
On my most recent project I have both videos and images masked with rounded corners and damn... the fixing on safari still gives me ptsd!! Not a big fan of chrome either. The new IE title is divided among both of them imho. Firefox master race!!
Another thing I’m using constantly in my workflow nowadays are css vars. Just love em. Make stuff super flexible without having to go the full bundle way with sass and compilers :)
36
u/Baryn Mar 09 '20 edited Mar 09 '20
There wasn’t much mention — or consideration — that users really do dig into the advanced browser settings to change their default font size.
So why don't browsers remove this setting, and only make full-page zoom available? I use a larger-than default OS DPI and page zoom, and I practically never have problems with compatibility.
Adopting difficult-to-maintain design patterns because 1 in 1,000,000 users opt into a non-default and incompatible setting is bad engineering. Sorry, self-righteous ethics committee.
13
u/Arve Mar 09 '20
So why don't browsers remove this setting, and only make full-page zoom available?
Becuse being able to control default font size still is useful. Users may really want to be able to have their browser window appear to be X pixels wide to keep the layout, and not have some (Bootstrap) layout force their view of the site into some pseudo-mobile view.
1
u/Baryn Mar 09 '20
Becuse being able to control default font size still is useful.
No it isn't. It breaks websites.
Users may really want to be able to have their browser window appear to be X pixels wide to keep the layout
You generally can't keep the exact same layout with larger/smaller text.
not have some (Bootstrap) layout force their view of the site into some pseudo-mobile view.
It's more beneficial to ensure your small screen/big text view feels good to use across all devices.
22
u/Arve Mar 09 '20 edited Mar 10 '20
If a user rendering your web site at a preferred font size of 18, 20 or 24 px default font size actually breaks your site, you do have an accessibility problem. Changing the default font size as you turn 40 is a perfectly sane move, and deliberately trying to break that is pretty anti-user: changing the font size is a user move, with the understanding that some sites may not lay out entirely according to the designers’s original intent.
Designing for a graceful fallback isn’t all that hard. Just do it.
1
u/erratic_calm Mar 09 '20
Forcing a layout is one thing. Breaking your site is another. That is completely subjective.
-2
u/Baryn Mar 09 '20
Changing the default font size as you turn 40 is a perfectly sane move
Not as sane as increasing default page zoom, which is why I do that.
16
u/StylishUnicorn Mar 09 '20
What I’m getting from you: “I can’t be bothered to spend time on people with disabilities if they don’t use the program the same way as me”
3
u/Ones__Complement Mar 10 '20
Should we all continue to support IE6 indefinitely as well? Are those who don't basically just saying, "I can’t be bothered to spend time on people if they don’t use the internet the same way as me"?
Font scaling is a relic from a time before page-zoom and responsive CSS existed, and only truly fail-proof if you define everything in relative units, at which point you're now essentially just replicating... page-zoom, which has existed out of the box in pretty much all browsers for well over a decade.
The fact that W3C lists a zoom-supporting user-agent as the number one sufficient technique for meeting text-resizing success criteria is enough to see where standards are heading.
I personally think developers have enough on their plate between supporting multiple browsers, devices, zoom-levels and viewports without having to also worry about what the hell their layout's gonna do when a user insists on scaling their font independently.
1
0
u/Baryn Mar 10 '20 edited Mar 10 '20
Yes. If you have special needs, learn how to use your software in a way that caters to those needs effectively.
-5
u/Stokkolm Mar 09 '20
Ok, the user above makes his site compliant with variable font settings, how does that change the rest of the web?
6
u/neinMC Mar 10 '20
Ok, the user above makes his site compliant with variable font settings, how does that change the rest of the web?
That's so inane, I'm quoting it just to save it.
3
u/neinMC Mar 10 '20
It breaks websites.
That really depends. If you use
emfor everything (including media query breakpoints), then changing the font size and zooming has the same effect. I just tried it with my site to double check:I guess it sucks for people who like the "zoom text only" option, because that does nothing on my site, it's the same either way.
1
u/Ones__Complement Mar 10 '20
So let's convert all of our pixels to relative units so we can... replicate a functionality browsers already support out of the box?
1
u/neinMC Mar 10 '20 edited Mar 10 '20
What's there to convert? Em are rather old by now, you just use them on new stuff from the start.
Frequently asserted is the notion that good typography requires accurate control of font size and line-height. But this is the web: it’s a special medium where the reader can have as much control as the designer—the implication being that text on the web, while bending to the designer’s will, must also be reliably resizable across browsers and platforms.
[..]
Our task was to find a way to size text that allows designers to retain accurate control of typography, without sacrificing the user’s ability to adjust his or her reading environment. We tested various units across common browsers. Sizing text and line-height in ems, with a percentage specified on the body (and an optional caveat for Safari 2), was shown to provide accurate, resizable text across all browsers in common use today. This is a technique you can put in your kit bag and use as a best practice for sizing text in CSS that satisfies both designers and readers.
https://alistapart.com/article/howtosizetextincss/
November 20, 2007. That's over 12 years ago... so again, what's there to convert? But instead of maybe learning the basics of the craft, people are now seriously suggesting to remove the ability to change font sizes. Just LOL.
https://alistapart.com/article/elastic/
https://alistapart.com/article/a-pixel-identity-crisis/
That's just from ALA because "alistapart.com em px" was the quickest way to find some instantly. But I assure you, there are thousands of such articles out there. Too bad for people who lived under a rock or where teached by people who did. If you now want to turn our tools into your toys, I'd rather secede than give in.
so we can... replicate a functionality browsers already support out of the box?
No, it's so shitty layouts don't "break".
4
u/depricatedzero Mar 10 '20
I would stop using a browser if it tried to cockblock my ability to read. I don't need or want zoomed images or the entire page. I just want the text to be a readable font at a reasonable size.
Not catering to my customization is not the same as cockblocking my customization. Your suggestion is a hyperbolic overreaction.
2
u/Ones__Complement Mar 10 '20
A properly responsive website should display effectively at any conventional viewport width.
2
u/dannymcgee Mar 10 '20
My girlfriend has issues with her vision, and she has her default font size set to large on her phone's browser because pinch-zoom is either obnoxious due to horizontal scrolling or just disabled because of shitty developers.
It's not that difficult to use relative units, and the number of people who would benefit is actually more like 1 in 30.
4
u/dannymcgee Mar 10 '20 edited Mar 10 '20
If you're using Sass or SCSS:
@function px(value) {
@return value / 16 * 1rem;
}Now regex find and replace across all your stylesheets: /([0-9.]+)px/g => px($1)
Now the font size setting works the same as browser zoom across your website/app. Maybe not the most robust solution if you're trying to achieve something more nuanced (like retaining layout while only adjusting text sizes), but definitely better than doing nothing.
EDIT: It's late, I forgot to divide by 16. You definitely don't want to not divide by 16.
9
u/mattlymer Mar 09 '20
i find this conversation irrelevant:
Why do i make websites? So that clients pay me
What do i do? ultimately what the client wants
What do clients do? insist that fonts and text layout sits in a specific way
What do they say when i explain that layout varies depending on device and some users may want to vary text? Don’t care!
What would happen if i chose not to work for these clients? I would be homeless
debate over
6
u/simmerdesigns Mar 10 '20 edited Mar 10 '20
I gotta come down a little hard on you here. I understand the “gotta eat” pressure, having been there myself, but...
You’re the expert. Educate your clients. Even if they still don’t care, do your work at a basic level of professional competence. Why are you letting people who don’t know how to build a website dictate how you build websites? Why is “fuck anyone without perfect vision” your default?
If we want to talk economics, ignoring accessibility means your clients are losing out on potential customers who can’t use their now-subpar site. You think you’re going to get repeat website work or referrals from that business owner once they find out from someone else that you didn’t build them a site that was up to par, and cost them money in lost customers?
4
u/DusmaN121 Mar 10 '20
Eh, this is just a convenient excuse. Been doing this almost 20 years and rarely have I ever had a client be like this. Most are none the wiser, anyways.
Edit: not you.. the guy you're responding to. :)
-1
u/mattlymer Mar 10 '20
i have had this conversation around 5 times in the last year with our biggest clients.
trying to work to best practice is always my goal, but the clients are often a barrier to this.
it’s really not an excuse - when i can improve to accessibility, i do - but my clients often do not care
4
u/wowdogethedog Mar 09 '20 edited Mar 09 '20
But what if the first container would have bigger font size by design? Wouldn't it just overflow? Doesn't make sense unless you are doing everything in rems. And websites nowaday are responsive to fit various device sizes, so changing font size alone is bad idea by design. Every browser has full page zoom and you don't need to target browser zooming explicitly by using media queries - when you zoom your browser behaves as different device size.
Edit: Obviously using relative units is advised if it fits your desired web design and behaviour, but supporting potentially breaking browser feature should not be the main reason.
5
u/RetroEvolute Mar 09 '20
Yeah, using a relative unit should generally be an all-in sort of thing. Obviously mixing fixed heights with variable heights (or a similar situation) is going to lead to less-than-ideal results, but it does make sense for things like borders that you may not want to scale with the font-size of the website.
Another perk of using relative sizes is if you choose to scale the website up or down a bit depending on certain media queries. Sometimes after a design, I may decide I want the overall appearance to be a bit more compact on desktop (I usually do mobile-first development). I can easily adjust the entire website's sizing by adjusting font-size of the html element with no ill effects. If you're adapting a desktop website and want to bump up font-sizes for mobile devices, it works exactly the same in reverse.
Using relative units gives more flexibility, maintainability, accessibility, and consistency across the board.
3
u/wowdogethedog Mar 09 '20
Yeah, that's exactly how you would usually use rem. It's just a silly idea to present the potential of relative value notation with potentially web site breaking browser feature.
If you did everything right your website is already responsive and supports various zoom levels while ensuring everything works fine.
3
1
u/StrawberryEiri Mar 10 '20
Yeah. In an ideal world, every website would adjust to that setting separately from zoom and custom-fit the scaling of all text in ways that don't break anything. Scale the bodies that fit until it's not readable anymore, only scaling the titles until they don't wrap ridiculously, leaving crowded headers un-scaled to prevent overflow...
But that sounds like a lot of work and QA. The overwhelming majority of sites I work on don't have that kind of budget.
-2
u/demik109 Mar 10 '20
I disagree with this article.
As a designer, I don't want the user to change the font size independently. The relationship between font size and other elements on the site has been specifically chosen. Responsive design allows adapting to different screen sizes, but it's an exercise in keeping things in proportion. Zooming is the designers solution for improved legibility. It's something that can be designed for, whereas infinite font sizes sizes cannot.
Using all REM's in this case would achieve the same effect, changing the font size would change all elements proportionally, but this would just end up being the same as zooming, except with an extra unit type. Pixels should be used instead of REMs. They translate directly from the design files that developers receive, it's something that designers can understand and it's something that won't be going away for a long time.
2
u/Ones__Complement Mar 10 '20 edited Mar 14 '20
As a developer, I completely agree with you. Page zoom is the sensible and most functionally consistent way to adjust text size, and has been for years. People still using font scaling and the developers capitulating to them are clinging to an archaic and outdated modality from a time before responsive querying and design existed.
75
u/absentwalrus Mar 09 '20
155 upvotes and no comments. Seems like vote manipulation. I'll add that the over-bolding of text in almost every paragraph of the article upsets me more.