• 技术文章 >web前端 >css教程

    CSS中overflow-y: visible;不起作用的解决方法

    不言不言2018-09-13 17:55:11原创3074
    本篇文章给大家带来的内容是关于CSS中overflow-y: visible;不起作用的解决方法,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

    场景

    最近要做的一个需求是移动端的h5页面,要求有一排可选择的卡片, 超出容器部分可以左右滑动,同时每张卡片左上角要有一个删除按钮。如下图:

    1582159059-5b9888d3630c9_articlex.png

    大前端成长进阶课程:进入学习

    心想:so easy, 在父容器上加一个max-width: 200px; white-space: nowrap; overflow-x: auto;不就搞定了嘛。Demo如下:

    <div class="container">
      <div class="son">
        <div class="delete_btn"></div>
      </div>
      <div class="son">
        <div class="delete_btn"></div>
      </div>
      <div class="son">
        <div class="delete_btn"></div>
      </div>
    </div>
    
    .container {
      max-width: 500px;
      overflow-x: auto;
      white-space: nowrap;
    }
    
    .son {
      display: inline-block;
      width: 200px;
      height: 200px;
      background-color: lightblue;
      position: relative;
      margin-right: 20px;
    }
    
    .delete_btn {
      width: 20px;
      height: 20px;
      position: absolute;
      top: 0;
      left: 0;
      background-color: red;
      transform: translateX(-50%) translateY(-50%);
    }

    原本以为一切顺利,结果得到的结果如图:

    2327917451-5b988d283ae46_articlex.png

    看第矩形左上角的红色方块,原本为20 * 20的红色方块有一部分被隐藏了。我想应该是overflow影响的,所以想通过overflow-y: visible;来解决,结果是不行的。细心的朋友应该记得overflow的默认值就是visible。那么原因是什么呢?

    Why

    找了好久,大致了解到是如下原因

    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为scroll或者auto时,overflow-y被设置为auto,反之亦然。这就很尴尬了,那怎么解决这个问题呢。

    ps: 上面那段话说是来自于w3c的文档,但是我找了半天没找到原文,麻烦找到了的小伙伴留个链接~ [手动狗头]

    How

    终究还是想让左上角的红色方块显示完整的,那么解决方案呢,我这里采用的是在container上添加以下样式

    padding-top: 20px;
    margin-top: -20px;

    原理其实挺简单的,加了padding-top: 20px;后,绝对定位的红色方块就有空间显示了,不会超出容器体积,然后通过margin-top: -20px;抵消位置的变化.如图

    3238606406-5b98b2e9860e8_articlex.png

    ps: 第一个红色方块左边被遮住的部分同样的思路解决,即通过padding-left和margin-left来处理。

    相关推荐:

    iframe双滚动条 解决方式 CSS3 overflow-y 属性_html/css_WEB-ITnose

    HTML标签的overflow处理用应_HTML/Xhtml_网页制作

    以上就是CSS中overflow-y: visible;不起作用的解决方法的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:overflow css
    上一篇:如何用纯css画一个跳动心?(代码实例) 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• css之使table也能overflow:hidden_经验交流• 修正IE下使用CSS属性overflow的bug_经验交流• 用margin和overflow属性实现div间距的方法_经验交流• Overflow OPREA_CSS/HTML• HTML超出文本显示省略号...通过text-overflow实现_HTML/Xhtml_网页制作
    1/1

    PHP中文网