chrome 6 的 jquery 边距问题

标签 jquery css google-chrome

我对 jquery (1.4.2) 和 chrome 6 有疑问

margin-left:auto
margin-right:auto

$(id).css('margin-left');
$(id).css('margin-right');

都返回一个 px 值(例如:327px)

最佳答案

This happens because when specifying margin:auto, Chrome (and also Safari, as they both use WebKit) sets computed value of margin-left and margin-right to an equal sized used values.

While Firefox and IE sets computed value of margin-left and margin-right to 0px. So if you have a 1000px wide page and your element takes 50% width (i.e.: 500px) and has margin:auto, then Chrome will set element's computed value of margin-left and margin-right to 250px (so it fills all the empty space between element's border edge and its parent element's content edge with margins and by so horizontally centers the element).

However, Firefox and IE will set computed value of margin-left and margin-right to 0px, so there is no margins between the element's border edge and its parent element's content edge but it is still centered. You can actually see it happening using Firebug in Firefox and Developer Tools in Chrome. In depth explanation can be found here:

http://www.3d3r.com/simon/marginAutoComputedValue

来自 smnhjQuery position page 下回答


请记住,您可以使用 .position().offset() 更好地控制尺寸

$(id).position().left

或者您真的想要 MarginLeft 吗?

关于chrome 6 的 jquery 边距问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3649630/

相关文章:

css - 浏览器在同一属性上加载带有 px 的速记 CSS 和带有 rem 的特定 css?

javascript - jQuery 事件委托(delegate)不适用于点击 div

css - 将文本和幻灯片并排放置在一个框中

javascript - 为什么我的 WebGl 帧率在 Chrome 中缓慢下降?

css - 尝试使用 CSS 在输入框上定位跨度

javascript - 如何在 Bootstrap 下拉菜单上编辑 CSS?

google-chrome - 如何在 Linux 上测试 google chrome 工作区?

javascript - unslider滑动图片问题

jquery - 折叠和展开列表

javascript - 浏览器不呈现表格元素 | jsFiddle 示例