Are the decimal places in a CSS width respected?

前端 未结 6 717
别那么骄傲
别那么骄傲 2020-11-22 04:35

Something I\'ve been wondering for a while whilst doing CSS design.

Are decimal places in CSS widths respected? Or are they rounded?

.percentage {
           


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 04:58

    Elements have to paint to an integer number of pixels, and as the other answers covered, percentages are indeed respected.

    An important note is that pixels in this case means css pixels, not screen pixels, so a 200px container with a 50.7499% child will be rounded to 101px css pixels, which then get rendered onto 202px on a retina screen, and not 400 * .507499 ~= 203px.

    Screen density is ignored in this calculation, and there is no way to paint* an element to specific retina subpixel sizes. You can't have elements' backgrounds or borders rendered at less than 1 css pixel size, even though the actual element's size could be less than 1 css pixel as Sandy Gifford showed.

    [*] You can use some techniques like 0.5 offset box-shadow, etc, but the actual box model properties will paint to a full CSS pixel.

提交回复
热议问题