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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值