wlHy 发表于 2023-3-7 16:48:07

关于overflow和position的联动问题

目前自学html中,写了一个二级菜单的案例,遇到了一些难以解释的问题。
首先给box设置了overflow: scroll。
当没有给item和item>ul设置position定位时,鼠标滑动到二级菜单可以通过red背景色看到是box被撑开了,且overflow: scroll没有生效。见代码1。
但是当设置定位后,鼠标滑动到二级菜单可以看到overflow: scroll又生效了。见代码2
请教各位大佬,这是什么原因导致的呢?

代码1
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
      * {
            margin: 0;
            padding: 0;
      }
      .box{
            width: 400px;
            margin: 0 auto;
            background-color: red;
            overflow: scroll;
      }
      ul{
            list-style: none;
      }
      .box .item{
            float: left;
            width: 148px;
            line-height: 40px;
            text-align: center;
            border: 1px solid blue;
            background: blue;
            color: white;
      }

      .item{
            /* position: relative; */
      }
      
      .item ul{
            display: none;
            background: white;
            color: black;
            /* position: absolute;
            left: 0;
            top: 40px;
            width: 148px; */
      }
      .item:hover {
            background: lightblue;
      }
      .item:hover ul{
            display: block;
      }

      .item li:hover{
            color: bisque;
      }
      .content{
            height: 100px;
            background: yellow;
      }
    </style>
</head>

<body>
    <ul class="box">
      <li class="item">视频教程
            <ul>
                <li>全部视频教程</li>
                <li>HTML5视频教程</li>
                <li>JAVA视频教程</li>
                <li>PYTHON视频教程</li>
            </ul>
      </li>
      <li class="item">认证考试
            <ul>
                <li>pmp</li>
                <li>红帽</li>
            </ul>
      </li>
    </ul>


</body>

</html>

代码2
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
      * {
            margin: 0;
            padding: 0;
      }
      .box{
            width: 400px;
            margin: 0 auto;
            background-color: red;
            overflow: scroll;
      }
      ul{
            list-style: none;
      }
      .box .item{
            float: left;
            width: 148px;
            line-height: 40px;
            text-align: center;
            border: 1px solid blue;
            background: blue;
            color: white;
      }

      .item{
            position: relative;
      }
      
      .item ul{
            display: none;
            background: white;
            color: black;
            position: absolute;
            left: 0;
            top: 40px;
            width: 148px;
      }
      .item:hover {
            background: lightblue;
      }
      .item:hover ul{
            display: block;
      }

      .item li:hover{
            color: bisque;
      }
      .content{
            height: 100px;
            background: yellow;
      }
    </style>
</head>

<body>
    <ul class="box">
      <li class="item">视频教程
            <ul>
                <li>全部视频教程</li>
                <li>HTML5视频教程</li>
                <li>JAVA视频教程</li>
                <li>PYTHON视频教程</li>
            </ul>
      </li>
      <li class="item">认证考试
            <ul>
                <li>pmp</li>
                <li>红帽</li>
            </ul>
      </li>
    </ul>


</body>

</html>

TKXZ 发表于 2023-4-7 11:59:39

因为你为.item设置了float:left,所以此时的.item是脱离了文档流(也就是说.item悬浮在box的上面)当设置定位后产生了BFC,BFC可以有清除浮动的功能,所以下拉菜单就显示在box看不见的区域了(应该是这个意思吧???)
页: [1]
查看完整版本: 关于overflow和position的联动问题