-
Notifications
You must be signed in to change notification settings - Fork 719
[css-borders] Add a 'hairline' border-width value #3720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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 |
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?
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 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 |
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. |
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 As on mobile Apps, Android can use For the web pages that is embed in mobile apps, I wish, one day, we can use |
The
|
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. |
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. |
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 ( |
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. |
@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 |
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 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. |
Yes, Tab replied to Amelia’s comment with this:
I take zooming to be like looking through a magnifying glass, where a hair (or hairline) looks bigger and thicker as a result. |
That’s no worse than zooming in 1.5x on 1px line when 1dpx = 1px (non-retina display), is it? |
Fair point. |
Is this fraction left up to the UA to decide?
|
On Safari, 0.1px or 0.2px borders may not render. See this bug https://bugs.webkit.org/show_bug.cgi?id=286256 |
As we consider to expose device pixel rounding as a function, I think it makes more sense to call it something related to that like Sebastian |
For both |
@SebastianZ why not reuse the existing In my sites I currently inject a: @property --dpx {
syntax: " via javascript so that I can make things look non-fuzzy. My displays have a native non-integer ratio to css pixels, which is why the size is fractional. Used like |
As I've said a few times in this thread, we still don't want to actually tie anything to "1 device pixel". That unit of measure is not guaranteed to be reasonable in the future. We just want "a UA-defined 'small enough' length, greater than zero and less than or equal to 1px, and likely an integer multiple of the device pixel size". |
Device pixels may not be a reasonable for rendering So Here's a more complex but less contrived example https://codepen.io/webstrand/pen/pvvVxgY. Recommended viewing with zoom (not on safari) or on a display with a non-integer |
@tabatkins You have explained the problems with having a "1 device pixel" unit. But I think you haven't explained the reasons for not making |
Yeah, I think a unit would also be acceptable, it's just slightly weirder. But I suppose it does work better as a unit when you're doing anything non-trivial, like a calc(). |
Then it seems simpler to me to just add |
But what is |
100 times the hairline width? I'm not sure I understand the intent of your question. |
I was suggesting that a |
|
The CSS Working Group just discussed The full IRC log of that discussion |
I wonder if it could be a keyword similar to And additionally allow it without Then, both |
I had also considered constants like |
So, here are the options:
2 and 3 both have the advantage that they work everywhere, automatically, with no grammatical ambiguity. We don't have to review any existing properties, or keep this in mind with any future properties. 1 doesn't have that advantage. The plain keyword is grammatically ambiguous anywhere a length is mixed with an open set of keywords. We mostly nowadays make sure that open sets of keywords use dashed-idents, but it's still not 100%, and there are several legacy uses that might be ambiguous now. (I haven't surveyed, I'm going on intuition.) But it is certainly the simplest, best-looking of the options otherwise. Some responses from the late minutes that I couldn't get to during the call:
That only works sometimes, if you can make sure that the box you're matching uses a specifiable border-box size. The example I was giving doesn't allow that - the content box is specifiable (100px), plus two hairline borders, and so the matching box needs to be 100px + two hairlines.
I don't see how any of the options are more or less likely to do this. In all of them, you can reference device pixels in some way. A very limited solution that just allowed the keyword |
What about making it an |
An |
The CSS Working Group just discussed The full IRC log of that discussion |
I'm still a bit confused about the difference with 1 device pixel. Is the idea that https://drafts.csswg.org/css-values-4/#snap-a-length-as-a-border-width will then ensure that non-zero border widths are at least 1 hairline instead of the current 1 device pixel? For values above that, will the rounding be to an integer number of hairlines or still device pixels? |
The use case is not actually "render a border 1 device pixel wide". If you have a thoeretical future screen with 100dppx, you don't want the border that small - it would be practically invisible. The use-case is "render a border thinner than 1px, if it's possible for the screen to do so while maintaining the sharp line we expect from borders". You can do lines that thin in print, and it's totally reasonable for many cases. So providing a
Min of hairline, increments of device pixels. (There's no guarantee that a hairline width will evenly divide 1px - on a 5dppx screen, a hairline of 2dp might be reasonable, for instance.) |
Then this means that In your example, |
If that's the use case, isn't it already possible with e.g. EDIT: Ah, I think I see the problem. div {
--border-width: 0.5px;
border: var(--border-width) solid black;
width: calc(100px + 2 * var(--border-width))
} Presumably, the intention is to make the content's width exactly 100px, but on screens where the Is that correct? If so, then either EDIT 2: Here's an idea, I don't know if it's a good one:
(Merged my comments, sorry for spamming) |
Uh oh!
There was an error while loading. Please reload this page.
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
andpx
decoupled, so thatin
was as close to an actual inch as the browser could get, andpx
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 thanborder-width: hairline;
. The only theoretical benefit ofdp-round()
overhairline
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 saydp-round(.5px)
, and a 2x screen will still use .5px if you saydp-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.
The text was updated successfully, but these errors were encountered: