css - 如何强制 Chrome 使用 `box-sizing: content-box` ? (2 个嵌套 DIV,外部显示为 `table-cell` 。)

标签 css google-chrome

目标

See the fiddle对于下面的代码。 我希望根据 the W3C specification of box-sizing: content-box 调整 inner div 的大小,无需修改或删除任何 non-empty CSS declarations , selectorsHTML elements .(我希望当前的 Chrome 和 Firefox 版本以及 Internet Explorer 11 得到支持。)

HTML:

<div id="outer">
    <div id="inner"></div>
</div>

CSS:

#outer
{
    background:red;
    display:table-cell;
    height:300px;
    width:300px;
}
#inner
{
    background:yellow;
    height:50%;
    padding:75px;
    width:50%;
}

视觉效果:

300px by 300px all yellow box

内部 div 的盒子模型:

content: 150x150, padding: 75, border: 0, margin: 0

问题

Chrome(版本:35.0.1916.153 m,操作系统:Windows 7)中使用了错误的大小调整方法。

视觉效果:

top half yellow, bottom half red of 300px by 300px rectangle

内部 div 的盒子模型:

content: 150x0, padding: 75, border: 0, margin: 0

附加信息

  • Chrome 中 inner div 的垂直大小与 the W3C specification of box-sizing: border-box 相同
  • 显式设置 box-sizing: content-box 不能解决问题
  • FF 和 IE11 都可以正常工作,只有 Chrome 不行
  • 仅当 % 用作 inner div 的 height 单位和 outer 时才会出现尺寸问题div 显示为 table-cell
  • 说明垂直尺寸问题的 GIF(没有立即增长):

GIF illustrating the vertical sizing problem

最佳答案

解决方案

float 外部 div;和 add a wrapper div displayed as table , 如果 your original layout得到 messed up by floating . See the fiddle对于下面的代码。

HTML:

<div id="wrapper">
    <div id="outer">
        <div id="inner"></div>
    </div>
</div>

CSS:

#wrapper
{
    display:table;
}
#outer
{
    background:red;
    display:table-cell;
    float:left;
    height:300px;
    width:300px;
}
#inner
{
    background:yellow;
    height:50%;
    padding:75px;
    width:50%;
}

视觉效果:

300px by 300px all yellow box

内部 div 的盒子模型:

content: 150x150, padding: 75, border: 0, margin: 0

替代解决方案

使用#outer {position: absolute} , 或 #outer {position: fixed} ,如果上面的解决方案(#outer {float: left},或#outer {float: right})不符合您的需求。

附加信息

关于css - 如何强制 Chrome 使用 `box-sizing: content-box` ? (2 个嵌套 DIV,外部显示为 `table-cell` 。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24394105/

相关文章:

c++ - 如何获取 Chrome 当前版本的当前 URL

javascript - Chrome 扩展 - 通过刷新页面保存整个网站的设置

javascript - 使用其 CSS 样式表将 javascript 插入本地 html?

html - 什么是 CSS 中的 &::before, &::after?

css - iPhone 6 plus 上的字体缩放看起来像 Revolution Slider 中的光栅化缩放

html - 阻止 float DIV 重叠

security - 是否可以制作一个具有离线敏感数据存储功能的本地 Chrome Web 应用程序?

javascript - 谷歌浏览器图像天花板?

html - 绝对位置但相对于父级

javascript - Chrome 上的 XML 错误但在 FF 上没问题?