Csswg-drafts: [css-backgrounds] Add a 'hairline' border-width value

Created on 11 Mar 2019  Â·  28Comments  Â·  Source: w3c/csswg-drafts

People regularly ask for the ability to size things in device pixels (#3715 is the latest). This is virtually always intended for the use-case of sizing borders, to create "hairline" borders or separators.

Since adding an actual device pixel unit is out of the question, we should still address this use-case directly, and add a hairline keyword to 'border-width', representing "the thinnest width that the UA believes is recognizable, snapped to the nearest whole number of device pixels". Right now, that'll be 1 device pixel, but if screens get significantly higher-res, it'll probably resolve to whatever number of device pixels gives you a value between 1/3 and 1/2 of a CSS px.


Why can't we add a device pixel unit, so you could just say border-width: 1dp?

Because device pixels are unpredictable in size. If you try to size anything bigger than a hairline with device pixels, the size will, today, vary by more than 300%, as there are devices that have 1dp==1px, and devices with 3dp=1px, and I think some devices are pushing higher than that. As display technology gets better, this gulf will continue to grow, as there will likely always be 1dp==1px devices in use.

We already know from past experience that authors can't generally handle units with an unpredictable "sliding scale" ratio; the CSS2 spec used to have in and px decoupled, so that in was as close to an actual inch as the browser could get, and px was the nearest whole number of device pixels approximating the visual angle of a device pixel on a 96dpi monitor. However, so many authors used the physical units (pt/in/cm/mm) to size their pages while assuming 96px=1in that browsers implementing the spec got broken pages on devices whose in-px ratio was different. And that was a ratio that could differ whose min and max could differ by, at most, 50%! (Handling an unpredictable ratio is a fundamentally very difficult problem! We have better, less fragile layout tools to help with that these days, but it's still virtually guaranteed to result in problems if we expose this.)

Plus, the notion of a "device pixel" is screen-specific; when printing, the analogue to "device pixel" is the "dot", the smallest drop of ink that can be placed at a particular location. Printers not only have much smaller dots than screens have pixels, and in a larger range (consumer printers can vary from 300dpi to more than 1000dpi), they also vary based on color; black-and-white dpi is often much higher than color dpi. Sizing anything based on "device pixels" will either be wildly unpredictable on printers, or else it'll be a "virtual device pixel" that doesn't correspond to anything physical anyway.

And all this isn't even getting into screens that simply more exotic than the "rectilinear grid of hardware pixels" design that this sort of feature assumes. Some screens layer their pixels in a non-square grid. Some have differently-sized pixels, layered in separate rectilinear grids. Some have different grids for each subpixel color. All in all, the notion of a "device pixel" as something you can naively size things in is already nonsensical for many output devices, and the situation is likely to only get worse as time goes on.

Why not a device-pixel rounding function?

In #3715 @florian and I talk about a function that rounds a length to the nearest number of device pixels, as a compromise that delivers many of the benefits of device-pixel sizing without as many of the downsides. On further consideration, I don't think we want this either.

For the case of hairline borders, a device-rounding function achieves basically the same thing as a 'hairline' value, just in a slightly more complicated way. Users have to know to write border-width: dp-round(.5px) or what-have-you, rather than border-width: hairline;. The only theoretical benefit of dp-round() over hairline is that it lets you distinguish between whether you really want .33px or .5px, when that distinction might matter for visibility, but in practice that's a wash anyway, since a 3x screen will still probably choose .33px over .67px if you say dp-round(.5px), and a 2x screen will still use .5px if you say dp-round(.33px), and many devices will end up still choosing 1px anyway, so the theoretical gain in precision is lost in practice. In a theoretical future with super high-res screens, it might become relevant; we can revisit the question at that point. ^_^

For any other case, rounding to the device pixel doesn't do anything meaningful unless you're also rounding the position. Otherwise it's possible that the box is exactly 200dp tall, but it's offset 150.2dp from the top of the screen, so it's still fuzzy. And unless we offer position-rounding as some sort of separate property switch, you have to instead use dp-round() on your ancestors, too, all the way up to the root. (And depending on how you're being laid out, that still might not be enough; flexbox alignment has no way to force a rounding on the gap between flex items, for example.)

So, dp-round() is overkill complexity for hairlines, and doesn't do anything useful for most non-hairline cases.

css-backgrounds-4

All 28 comments

I think this is a good compromise to the issue, especially if there is spec text recommending that browsers consider accessibility when defining the "hairline" thickness. E.g., if I'm browsing the web with 400% zoom level, I probably don't want lines that are only one retina-screen-pixel thick!

For the more general case of "snapping" lines to an even number of device pixels, the SVG shape-rendering property is another approach, which could be generalized to apply to CSS borders and boxes. However, shape-rendering: crispEdges wouldn't remove the need for a dedicated hairline option, since snapping very fine lines to the nearest number of device pixels sometimes results in snapping them down to nothing!

Yes, the spec should definitely state that the hairline-ness should be taken at "normal" zoom levels; zooming in shouldn't keep them hairline!

It seems like at some point these borders might become thinner than the user of this keyword expects. For example, when printing, or as devices get even-higher-resolution.

Also... with Gecko's border rounding behavior (round down in device pixels, except round values between 0 and 1 device pixels up to 1) you can effectively do this today without facing that problem. For example, a border with width `0.25px' will be one device pixel, unless there are 8 or more device pixels per CSS pixel. I think there was some movement of other browsers towards that behavior, but I'm not sure if things have converged.

@dbaron Did you miss this part (quoted below), or is your comment asserting that something about it would still be problematic?

Right now, that'll be 1 device pixel, but if screens get significantly higher-res, it'll probably resolve to whatever number of device pixels gives you a value between 1/3 and 1/2 of a CSS px.


Also... with Gecko's border rounding behavior [snip]

Hm, interesting. http://software.hixie.ch/utilities/js/live-dom-viewer/saved/6749 tests this behavior. In Chrome, on my 1x desktop monitor, I get a 1px border for everything down to .05px; the .01px border disappears. That might be an internal rounding issue?

There's an additional weirdness in Chrome, tho - despite the border displaying the same size in all of them, the internals of the box itself lays out as different heights, despite the only thing giving it a height being a padding: 2px declaration! Looks like the 1px-border is 4px tall internally, the .5px and .33px are 3px tall, and the .1px is 2px tall. It looks like we're rendering the border-box's height "correctly" for the specified padding+borders (6px, ~5px, and ~4px, respectively), but then just painting the borders larger, so it overlaps the padding box internals?

It looks like Firefox correctly renders a 4px padding box on each of them, so I assume this is a Chrome bug in the timing of our border-width snapping vs other layout calculations.

(Confirmed with @atotic, this is a Chrome bug that'll be fixed when they get around to rewriting the relevant chunk of our layout code into the new LayoutNG form.)

I did miss that "Right now" part -- although I think the proposal needs to define that behavior today, for printing.

When I don't want a keyline to effect geometry, I've used box-shadow. I don't s'pose we'd also be able to solve this hairline value for properties like box-shadow & outline-width, let alone hopeful properties like text-stroke-width(compat) or text-shadow?

I did miss that "Right now" part -- although I think the proposal needs to define that behavior today, for printing.

Right, sorry for not being clear, it is absolutely intended that the spec will explicitly say that UAs should choose an appropriate small width, and recommend that it probably be no smaller than 1/3 or 1/2 of a px.

I don't s'pose we'd also be able to solve this hairline value for properties like box-shadow & outline-width, let alone hopeful properties like text-stroke-width(compat) or text-shadow?

Hm, maybe.

Tho at this point dbaron's statement about just relying on rounding behavior (and specifying that you must always round away from zero) is starting to sound really good.

I had a chat with @bfgeek about this tho, and he has some reservations about it. He could elaborate on this more; I don't fully understand the problems he was alluding to.

I do think the supporting for fractional order px value, means 0.1~0.9px (may be even more precise than that in the number of both decimal places and decimal digits), is more useful than we finally only can have a new hairline key word value for border-width property.

As on mobile Apps, Android can use 0.3dp or 0.5dp , and iOS can use 0.3pt or 0.5pt to express the hairline line. For CSS, px just plays the same role as dp on Android and pt on iOS.

For the web pages that is embed in mobile apps, I wish, one day, we can use 0.3px or 0.5px to match the exact border width with the mobile apps, so the users can see the same thick or thin lines on their Apps.

I don't s'pose we'd also be able to solve this hairline value for properties like box-shadow & outline-width, let alone hopeful properties like text-stroke-width(compat) or text-shadow?

The outline-width property references the <line-width> compound type that is defined for border-width and therefore accepts border-width keywords. So it should automatically also get a hairline value if it is added as a new keyword.

stroke-width as defined in SVG doesn't accept keyword values, but there's no reason we couldn't add them (meaning, thick, medium, thin, as well as hairline) for the new module that extends it to CSS text.

box-shadow and text-shadow would be more complicated, since there are many different lengths (offset and expansion/inset) interacting. But maybe it's worth a separate issue to discuss the expected behavior of fine resolution shadow effects — I recently discovered that browsers are very inconsistent in how they handle text-shadow offsets of less than 1px (see Twitter thread with people posting screenshots).

I do think the supporting for fractional order px value … is more useful than we finally only can have a new hairline key word value

There's nothing stopping you from using a fractional px value for border width in CSS — it's just not guaranteed to snap to an even number of device pixels, and will therefore look bad on a screen with a different resolution than you designed it for. If this is really important, however, you could use media queries to check for the screen resolution.

The benefit of the keyword is that the browser would figure out the exact value required to get a crisp line.

There's nothing stopping you from using a fractional px value for border width in CSS — it's just not guaranteed to snap to an even number of device pixels

I think it should. If it was only a 1x screen, then use opacity to simulate the thinner line, like is typically done for antialiasing scaled images. So a 0.5px line is rendered with 1px at half the opacity of what it normally would be. On a 3x screen, it would use 2 device pixels, at 0.75 opacity.

There's nothing stopping you from using a fractional px value for border width in CSS — it's just not guaranteed to snap to an even number of device pixels,

It actually is guaranteed to do that. CSS itself doesn't guarantee it, but all browsers snap borders to whole device pixels, precisely because they otherwise look bad. (And do some fancy rounding to make sure all borders of a given width round in the same direction.)

@tabatkins I thought Chrome and Safari (and maybe others) snapped to the whole px, not to the device pixel smaller than 1px. Is that no longer true?

At least for border widths, I mean. Things like centering might be on device pixel space.

No, they definitely do device pixel snapping. Confirmed by looking at http://software.hixie.ch/utilities/js/live-dom-viewer/saved/6749 on my high-dpi phone; the first box (border-width: 1px) definitely has wider borders than all the others.

Hmm. You're right. But they all (except 1px as and 0px) seem to be rounded to the same value on my iPhone X. Looks like maybe 0.25px. Maybe we can standardize and improve this then? It seems better than 'hairline' to me. But maybe both are useful.

On my retina desktop display (Mac), in Chrome for Mac, the 0.05px to 1px borders all look the same (1px), and the last 2 are rounded down to zero.

If this is really important, however, you could use media queries to check for the screen resolution.

@AmeliaBR Yes, I could. But when I viewed http://software.hixie.ch/utilities/js/live-dom-viewer/?saved=6749 on Chrome/Safari/Firefox/Opera Mini on my iPhone XS Max, as @bradkemper has pointed, all values that less than 1px are rounded to 0.25/0.3px (maybe 1 device pixel). Based on this, I think using media queries to check the screen resolution is also not useful. Because it seems
none of the browsers have yield the control to us for how many device pixel one fractional px value could be snapped to.

It seems that different properties are snapped to device pixels by different rules. Can anybody shed some light on the general logic behind this, please?

My two cents here, maybe we should instead have a border-style: hairline and outline-style: hairline that would draw a perfect border around the content inside, and potentially leave extra blank space when that size is smaller than the border-width.

That way, the layout doesn't become unstable when you zoom in, only the rendering can change.

I'm personally mostly concerned about layout changing as you zoom, this could have unintended consequences.

@fremycompany zooming should still scale it, along with everything else.

@bradkemper Wait, so when you zoom, the "hairline" stops being an hairline, did I get that right?

Because for instance if you had specified 1hairline, landed on 1dpx, then zoom 1.5x then the new border size won't be a round number of pixels (1.5dpx in this case) and won't be an hairline. I guess that this is an option, but that doesn't sound great to me.

My proposal would have been to have the border be 1px all the time (which might be 1.5dpx, whatever), but then draw only 1dpx/2dpx around the content at painting time, and stick to either 1dpx (or 2dpx etc...) as you zoom while continue to scale the 1px value normally.

@fremycompany

Yes, Tab replied to Amelia’s comment with this:

Yes, the spec should definitely state that the hairline-ness should be taken at "normal" zoom levels; zooming in shouldn't keep them hairline!

I take zooming to be like looking through a magnifying glass, where a hair (or hairline) looks bigger and thicker as a result.

Because for instance if you had specified 1hairline, landed on 1dpx, then zoom 1.5x then the new border size won't be a round number of pixels (1.5dpx in this case) and won't be an hairline. I guess that this is an option, but that doesn't sound great to me.

That’s no worse than zooming in 1.5x on 1px line when 1dpx = 1px (non-retina display), is it?

Because for instance if you had specified 1hairline, landed on 1dpx, then zoom 1.5x then the new border size won't be a round number of pixels (1.5dpx in this case) and won't be an hairline. I guess that this is an option, but that doesn't sound great to me.

That’s no worse than zooming in 1.5x on 1px line when 1dpx = 1px (non-retina display), is it?

Fair point.

Was this page helpful?
0 / 5 - 0 ratings