overflow-x和overflow-y其中一个设置为visible时的奇怪现象

当overflow-x和overflow-y其中一个设置为visible时,如果另一个不是visible,那么它会被自动重置为auto

看看效果先:

第一次遇到这个问题时,我还以为是chrome的一个bug,结果测试了一下,所有浏览器都是这样的,

看一下效果

.div1 {
    width: 100px;
    height: 100px;
    background: #eee;
    position: relative;
    overflow-x: hidden;
    overflow-y: visible;
}
.div2 {
    width: 100px;
    height: 100px;
    background: #f00;
    position: absolute;
    left: 50px;
    top: 50px;
}
<div class="div1">
    <div class="div2"></div>
</div>

理想中的效果是

但是结果却是

我只能说,这太不合理了,对于这个问题,w3c规范是这么说的

The computed values of ‘overflow-x’ and ‘overflow-y’ are the same as their specified values, except that some combinations with ‘visible’ are not possible: if one is specified as ‘visible’ and the other is ‘scroll’ or ‘auto’, then ‘visible’ is set to ‘auto’. The computed value of ‘overflow’ is equal to the computed value of ‘overflow-x’ if ‘overflow-y’ is the same; otherwise it is the pair of computed values of ‘overflow-x’ and ‘overflow-y’.

翻译过来就是:

overflow-x和overflow-y的计算值跟给定的值相同,除了某些跟'visible'值的不合理组合:如果一个其中一个属性的值被赋为'visible',而另一个被赋值为'scroll'或'auto',那么'visible'会被重置为'auto'。overflow的计算值与overflow-x相等(如果overflow-y相同的话);否则就是一对overflow-x和overflow-y的计算值

其实另一个值设置为hidden的时候,visible也会被重置为auto

查了很久,就是不知道当一个属性设置为visible的时候,另一个设置为scroll这些值有啥不合理的地方

今天是2014-4-2,我觉得我知道为啥它会不合理了

咱们都知道overflow的非visible值会使一个块级元素形成一个bfc(块级格式化上下文)

overflow-x设置为visible,overflow-y设置为非visible,那究竟是触发bfc还是不触发bfc呢?此处冲突,所以充值了overflow-x,使其成为一个bfc

计算值,应该不仅仅是overflow的值,还包括一些附带属性,比如此处是否生成一个bfc

参考资料:
https://developer.mozilla.org/zh-CN/docs/CSS/overflow-x

原文地址:https://www.cnblogs.com/strayling/p/3610446.html