1

TLDR: same lines drawn with the div element look different at different scales and sizes of the browser window. This is the example: imgur, jsfiddle.

I drew some horizontal, vertical and diagonal lines using DIV blocks. With different browser window sizes, different scales, different content above and below the drawn blocks, these blocks (with the same thickness) look different (some are thinner, others are wider). Conversely, blocks with different thicknesses may look the same in some moments.

These pictures demonstate how a part of my schema looks in different situations.

On a monitor with a normal pixel density and with system scale = 100% this problem is less noticeable or not showing itself at all.

I saw a similar problem earlier when I added narrow (1-2px) div elements as horizontal dividers. Some of them sometimes were disappeared (they were invisible) when others behaved as usual on the same page, if all of them were visible, some of them were thicker than others. This behavior for each element changes even I open/close Chrome developer tools panel or drag a corner of browser window.

I tried to replace div lines with svg elements, but they also demonstate strange behavior.

Can I make all elements resize the same? I would not want to replace each such element with a bitmap image.

A similar problem was described in this (not solved) question (if you can't see the problem, just zoom the result window in jsfiddle.

I cant catch the problem in other sites, for example, on Wikipedia, all horizontal lines (under h2 elements, left menu dividers) look the same at any scale. At stackoverflow there are comment horizontal dividers (narrow gray lines between comments), they are also look great.

This (imgur, jsfiddle) minimal examlpe demonstrates the different line scaling at 90, 100 and 110%. I noticed a strange feature. If I use a border to draw lines instead of the background-color, then these lines look much better. This method used at stackoverflow and wikipedia sites (Wiki also uses png at the left menu).

This is the minimal reproducible example, built in Stack Snippets:

.line {
  background-color: pink;
  /*  border-bottom: 1px solid pink; */
  margin: 20px auto;
  width: 80%;
  height: 1px;
}
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>

1 Answers1

0

This is just how different browsers solve for fractions at different zoom sizes. You can't have part of a pixel on your screen.

For example, let's say you have three divs stacked on top of each other, each 2px high. Zoom in to 120% and each div would now be 2.2px high. Since the browser can't actually render that, it might round the first div to 2px high. The second div might have a top of 2px and also be 2px high.

However, the third div should technically be 4.4px from the top, but its bottom should be 6.6px from the top. If the browser is rounding to the nearest whole number, you could see how it might compute the top of the third div at 4px and the bottom at 7px, giving it a height of 3px, instead of 2px.

I'm not saying that's exactly what's going on - each browser uses slightly different calculations when it comes to painting - but hopefully that helps illustrate the point. It sounds like you're having better luck with border so that might be your best approach.

Here's a similar question

sallf
  • 2,583
  • 3
  • 19
  • 24
  • All of my examples - from one browser. Yes, I can't have a part of pixel, but all 2px elements may scale to the same size. I don't understand, why borders look more correct than backgrounds, both of them use the same pixels... – WallOfBytes Feb 18 '20 at 18:31
  • I also tried to use em instead of px, but the result looks bad anyway (although better than with px). – WallOfBytes Feb 18 '20 at 18:43
  • I think the answer is still basically "because of browser calculations". In the jsfiddle example, your box sizing is default `content-box`, meaning borders are additive. You could see a browser calculating the bottom position of a box and then adding an additional pixel for the border, which would always round to the same number at different zoom levels. – sallf Feb 18 '20 at 18:51
  • Changing `box-sizing` rule has no effect. If the reason is browser calculations, why borders look great while backgrounds do not? – WallOfBytes Feb 18 '20 at 19:24
  • That's what I just tried to explain. Box sizes (background) and borders are calculated differently - and in addition different browsers calculate them differently. Point is, there's nothing you can really do about it. If `border` seems more accurate to you, then use it. – sallf Feb 18 '20 at 22:14
  • How other solve the problem? I am absolutely sure that many developers have encountered this problem. However, the grid in google tables looks perfect. Forums (with their horizontal dividers) also solved the problem. I search for alternative ways, border may be not an universal (and may be, incomfortable) way. – WallOfBytes Feb 18 '20 at 22:18
  • 1
    I've answered your question to the best of my ability. I encourage you to keep looking and post a better answer when you find it :). – sallf Feb 19 '20 at 01:55