6 ms·
I'm sorry, but you seem to be under some misconceptions about how pixel sizing works on the web. > resolution independent font-size and container sizing should
by crazygringo 6y ago
I'm sorry, but you seem to be under some misconceptions about how pixel sizing works on the web.
> resolution independent font-size and container sizing should be the default
It already is. CSS "px" doesn't refer to physical pixels at all, they're called logical pixels. Which on old screens at 100% zoom may map 1:1, but these days are more likely to map 2:1 or even 3:1, and with OS and browser zoom levels may be things like 1.666:1 or 2.5:1.
> Size your fonts with `em` and `rem` units
In the browser, em/rem are necessarily defined by a parent/root element that is ultimately defined in either px or pt, so your suggestion doesn't achieve what you think it achieves.
Today, whether you write your CSS in "em" or "px" results in an identical user experience. It's really just whether you prefer to work with a baseline of something like "16px" and use larger round integers (8px, 1000px, etc.) or prefer to work with something like a baseline of "1em" and use small numbers with lots of decimals (0.5rem, 30rem, 0.05rem).
Ultimately we need some measure of consistency, and browsers have long defined that as a default legible body text size of 16 px = 12 pt (= 1 em by default).
- jan_Inkepa 6y ago> It already is. CSS "px" doesn't refer to physical pixels at all, they're called logical pixels. Which on old screens at 100% zoom may map 1:1, but these days are more likely to map 2:1 or even 3:1, and with OS and browser zoom levels may be things like 1.666:1 or 2.5:1. Yeah it's a nightmare if you're doing pixel-art-y games in the browsers, there's last time I checked no way in general to get access to physical pixels anymore, which makes integer-multiple upscaling AFAIK impossible in general. Hurrah. [ disclaimer: The browser rendering stack is complicated enough that I might be wrong. ]
- wffurr 6y agoCSS px * devicePixelRatio = physical pixels I feel your pain about non-integer scaling ratios. The way I hack around it is to set the canvas context to an integer number of device pixels, and then set the CSS size of the canvas to a fractional size: device pixels / DPR. We use this along with some other hacks on keep.google.com and some other sites to map 1:1 to display pixels to try to ensure hardware overlay promotion for the inking surface.
- greggman3 6y agoDoesn't always work (may work for your use case). There are edgecases in the browser where at an HTML level the browser computes a CSS size but at compositing level (where devicePixelRatio is used) it might have clip by pixel here or there. And example, lets say your windows 99 device pixel wide and a DRP of 2.0 and lets say you ask for 2 side by side elements (via flexbox or whatever) each width:50%. At the HTML level the browser will compute there are 49.5 CSS pixels available. It will then compute the size of each 50% as 24.25 CSS pixels but in order to fill the 99 pixels, one of those 2 elements has to be 50 pixels and the other 49. There is no part of the spec that says which side gets 50 and which get 49. Most browser compute 50 for both sides, the pass it up their compositor which will use 50 pixels of one and 49 of the other. The point is, you can't know which one will get which by doing CSSpx * dpr. This is why ResizeObserver device-pixel-content-box was added. So that you can ask the browser what it did.
- jan_Inkepa 6y ago> ResizeObserver TIL. Oh gosh I don't know if I have the brain power to work through all the necessary rendering layers logic to fix up my game engine to make the pixels as they should be, but if I ever do and figure it out, great thanks to you! (and even otherwise, thanks!)
- wffurr 6y agoI will check out that ResizeObserver property. I hack around it by taking the CSS size of the container, finding the closest smaller integer device pixel size, and set the canvas context to that and the canvas CSS size to the fractional size, which can be smaller than the containers CSS size. Basically rounding down.
- greggman3 6y agoHere's a test https://jsgist.org/?src=0cfdcbad01c616047311b2b9ab51bbc3 https://jsgist.org/?src=0cfdcbad01c616047311b2b9ab51bbc3
- tracker1 6y agoPlaying with xterm.js with several fonts trying to get some regularity is/was an exercise in frustration for sure.
- greggman3 6y agoThere is, ResizeObserver -> device-pixel-content-box But ATM it's Chromium browsers only. Some examples on this page https://webglfundamentals.org/webgl/lessons/webgl-resizing-the-canvas.html https://webglfundamentals.org/webgl/lessons/webgl-resizing-t... Bugs filed for other browsers but AFAICT it's not a priority for them. Note: For a pixelated game having this info useful. Also for diagrams, as the user zooms in you'd like to be able to re-render a canvas at native resolution the same way SVG and text gets re-rendered. But, a noticeable problem is static images. You make pixelated image say, 100x50 and display it 4x like this <img src="100x50.png" style="width: 400px; height: 200px; image-rendering: pixelated;"> But, what's the browser supposed to do with that on a screen with 1.25 devicePixelRatio? It will look like crap.
- jan_Inkepa 6y agoAt someone's recommendation a while ago I made a proposal for integer-multiple upscaling - https://github.com/w3c/csswg-drafts/issues/5967 https://github.com/w3c/csswg-drafts/issues/5967 . It feels very niche, especially given how complex the standards already are, but OTOH I'd use it if it were a thing... (Also, that linked page is very cool, and shows how awfully tricky the problem is...none of the proposed solutions work on my iPad, especially not once one zooms in/out in the browser - oh it turns out it was related to browser zoom level - at normal zoom they work (sometimes when I resize the page at normal zoom it works, sometimes it doesn't...). So I guess it's 70% of the way there...but not all the way there).
- greggman3 6y agoThe reason it doesn't work on iPad is because iPad doesn't support the required API. Without that API there is nothing you can except special case per browser and pray. And even then, Safari doesn't change DRP based on zoom level like Firefox and Chrome do so it's currently impossible on Safari period.
- jan_Inkepa 6y agoI've added an event in my calendar to go back to this topic again this day one year from now to see if things have improved any (I skirt with frustration-induced burnout every time it raises its head as relevant, so I need to limit my exposure). Thanks for knowing more than me about this cursed subject matter.
- danShumway 6y ago> It already is. CSS "px" doesn't refer to physical pixels at all, they're called logical pixels. Okay, yes, but when people ask what the current resolution of a page is on the web, most of the time they're talking about those logical pixels. You're correct, but also this doesn't change anything about my argument. It's still a mistake to tie layout to the resolution of a web page, regardless of what unit you're using for a pixel. > Today, whether you write your CSS in "em" or "px" results in an identical user experience. No it doesn't. Like you said, `em` units are based on the current container font sizes. Pixels aren't. There's no way in the pixel world to say that you want a container that will always display 40 em-sized characters, and to have that continue to be true even as font size increases and decreases. You can try this yourself[0]. Set Firefox to Zoom Text Only (View->Zoom), then zoom in/out on a container sized with pixels and a container sized with `em` units. Only the `em` sized container will change its size, because its width is being tied to the font settings so its width scales proportionally as the font size changes. [0]: https://jsfiddle.net/jbwf6um4/ https://jsfiddle.net/jbwf6um4/
- crazygringo 6y ago> It's still a mistake to tie layout to the resolution of a web page You've got to tie it to something. Your argument falls apart because "em" are still defined in terms of "px" at the root element. You're always defining layout relative to a logical resolution. Also, responsive design reports viewport widths in logical pixels, so logical pixels are still what everything is ultimately tied to. > Set Firefox to Zoom Text Only (View->Zoom) Nobody does that anymore. That was how browsers zoomed two decades ago. For a long time now, browser zoom defaults to zooming all content, i.e. logical pixel size. Heck, Chrome doesn't even have a text zoom option anymore. I'm actually surprised Firefox still does -- maybe it's a backwards-compatibility thing? It's true that maybe two decades ago defining sizes based on em was a best practice, precisely because browsers only zoomed font sizes. But that ceased having relevance a long time ago, once browsers stopped using text zoom (except as an obscure setting). Today, in the browsers and browser settings 99% of people use, px vs em has zero difference for the user. It's just a matter of programming taste. (Especially now that it's generally recommended to use rem over em anyways.)
- gorgoiler 6y agoI understand why, but to outsiders it is odd that the one measurement you don’t specify is physical size. 12pt is ~4½mm.
- kazinator 6y agoYou don't want to be doing font-related things in px units. For instance, you don't want, say "margin-bottom: 10px" under a text element. The margin will stay the same if the font size is increased or decreased.
- leokennis 6y agoThe benefit of rem is of course that if I want all fonts to be 1.5 times bigger, al I need is: html { font-size: 150%; } Then, 1 rem = 16 px will become 1 rem = 24 px etc. With pixels, I will always be stuck with having to update every value in my CSS.
- mcguire 6y agoSo, rather than using the traditional device-independant units, we should use another device-independant unit that pretends to be device-dependant, but isn't?