css - 百分比 CSS 布局中丢失的像素在哪里?

标签 css browser

<分区>

Possible Duplicate:
Evenly distributed height of child elements with CSS

假设我有一个包含 6 个 DIV 的设计,它们向左浮动,宽度为 16.666%。 因此文档大小分为 6 个部分。

现在,如果我的文档大小为 620 像素宽,这将使每个部分为 103.333 像素。因为我不知道可以显示部分像素的屏幕 :) 我想知道浏览器如何处理部分像素。

这是我的测试用例: http://jsfiddle.net/dhQh2/ (只需调整窗口大小即可获得结果)

当调整它的大小时,6 个 DIV 保持相同的大小。但它在某些情况下是不可能的。浏览器如何处理这些部分 PX 值?

最佳答案

例如,如果您使用的是 % 宽度,而确切的宽度应该是 103.333px,那么浏览器必须决定如何显示它.

不同的浏览器做出不同的决定,阅读这些链接以获取更多信息:

我特别喜欢 John Resig/David Baron 对为什么这甚至是一个问题的解释:

I was talking this over with some Mozilla developers and David Baron explained the situation quite well:

We’re trying to meet a bunch of constraints that can’t all be satisfied at the same time (the proof is left as an exercise to the reader, though I may have actually written it out once in a Bugzilla comment):

  1. 4 adjacent objects of width/height 25% (for example) starting at one edge of a container should end exactly at the other edge; there should never be an extra pixel in the container and they should never be wrapped due to being a pixel to wide

  2. objects that are logically adjacent should always touch visually; there should never be a pixel gap or a pixel of overlap due to rounding error

  3. objects given the same width should occupy the same number of pixels

  4. object boundaries should always (visually) be aliased to a specific pixel boundary in the display (they should never be blurred)

The one [Mozilla] sacrifices is typically (3), except for borders where we sacrifice (1) by rounding the widths to pixel boundaries much earlier.

有关强制一致性的 JavaScript 修复,请参阅此问题:

Evenly distributed height of child elements with CSS

另一个相关问题:

Are the decimal places in a CSS width respected?

关于css - 百分比 CSS 布局中丢失的像素在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5581973/

相关文章:

javascript - 选择链接时如何更改链接的颜色?

css - codeception 无法点击 xpath,因为它无法定位 CSS 或 Xpath

css - Bootstrap 下拉位置固定,容器边界半径在 IE 中消失在边界之外

javascript - navigator.getCurrentPosition() 在 Firefox 或 Safari 中不起作用

javascript - 打开新的网络浏览器选项卡,并重复使用同一选项卡

javascript - 我怎样才能在这里制作一个 html 解决方案而不是使用 javascript hack?

css - Bootstrap 字体覆盖标题和正文的不同字体

html - 为什么浏览器检查器下不显示流畅的 BootStrap3 属性

ios - 在iOS中从浏览器访问相机胶卷

algorithm - CSS和DOM在浏览器中是如何实现的?