How do browsers deal with non-integer values for height and width?

后端 未结 1 1004
别那么骄傲
别那么骄傲 2020-12-03 02:32

When browsers are given widths and heights for elements that aren\'t integer values, how do they deal with them?

In particular,

  • At what stage do non-i
相关标签:
1条回答
  • 2020-12-03 03:29

    Assertion

    Browsers are designed to deal with floating point numbers and values less than one pixel.


    To see a simple example showing that browsers use floating point numbers in their calculations, create a 3% width item and look at its calculated properties in Chrome developer tools as it is resized.

    You should see something like this:

    enter image description here

    "35.296875" can't be precisely rendered by a display that maps one pixel to one pixel in the physical display (CRT, traditional LCD). However, newer high density displays use a different ratio than 1-1 and this fractional value could conceptually be used to provide a greater degree of precision.

    Even on low density displays, a fractional value could provide a hint for subpixel rendering, which uses the red, green and blue components of the pixel to make the edges of an object appear smoother than possible with whole pixel values.

    But exactly what the browser will do with such numbers isn't very predictable. You can't (currently) ask a browser to make a box 31.5px wide and expect a consistent or even meaningful result. Some browsers will truncate fractional values; others round up/down.

    Subpixel rendering is commonly used for text and works quite well in most/all browsers, but each browser implements this differently and there is very little a developer can do to impact how this works.

    When

    At what stage do non-integer values get rounded in the inheritance chain?

    Most/all calculations are performed as floating point numbers and any rounding may occur late in the process, or even outside of the browser's control. For example, a browser may delegate its anti-aliasing to an OS component (such as IE9 does to Windows Direct2D and DirectWrite).

    CSS transitions may be tightly integrated with OS and/or hardware acceleration. This is another case in which I think it is highly likely the floating point values are preserved by the browser and passed to the underlying layer(s).

    Rounding Behavior/Errors

    When a container's children have non-integer dimensions, will there ever be instances where the sum of the child lengths or heights not equal the inner width / height of the parent element?

    I've seen this in older browsers (IE7) as a result of percentage calculations, where 50% + 50% > 100%. Usually it is not a problem until you try to do something more complicated. Anecdotally, I have seen "off by one pixel" bugs when attempting to precisely align HTML elements as part of an animation.

    Percentages vs. other Units

    Do provided non-integer dimensions get handled differently to non-integer results of percentage-based dimensions?

    Do they round to the nearest integer, or truncate them?

    It varies. This older answer states that they are truncated, but (in Chrome 24) I see rounding (note the example fiddle). Note my earlier comment about the differences between Chrome and Safari on the same machine.

    What about non-whole values for padding and margins?

    The same rules (or lack thereof) appear to apply.

    Standards

    I haven't found a standard definition for how floating point values should be handled in all cases. The closest relevant spec I can find talks about canvas pixels:

    The handling of pixel rounding when the specified coordinates do not exactly map to the device coordinate space is not defined by this specification, except that the following must result in no visible changes to the rendering: [...list of conditions...]

    Again, this is from a section dealing specifically with canvas, but it does insinuate:

    • Browsers absolutely interact with fractional pixels.
    • Actual implementations will vary.
    • Some standardization does exist.
    • Mapping to the device's display may factor into the calculation.
    0 讨论(0)
提交回复
热议问题