css浮动问题-左右两盒子浮动,右盒子跑到了左盒子的下方

问题文字描述:
大盒子-> .box
两个子盒子-> .left + ul
两个子盒子均左浮动,ul中的li左浮动,li的高度为左盒子的一半,当li的个数增加至ul宽度极限时,应该在ul中换行,且ul处于.left的右侧,现实却是跑到了.left的下方

代码:

<!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;
            list-style: none;
        }

        .box {
            width: 1200px;
            margin: 0 auto;
            background-color: hotpink;
        }

        .left {
            width: 300px;
            height: 300px;
            background-color: orange;
            float: left;
        }

        .box ul {
            float: left;
        }

        .box ul li {
            float: left;
            width: 300px;
            background-color: green;
            height: 150px;
        }

        .clearfix::before,
        .clearfix::after {
            content: '';
            clear: both;
            display: table;
        }
    </style>
</head>

<body>
    <div class="box clearfix">
        <div class="left"></div>
        <ul class="clearfix">
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
</body>

</html>
当li标签个数增加时,整个ul会跑到.left盒子的下方!

这是为什么呢?

阅读 3.5k
3 个回答

没有限制ul最大宽度,导致ul会因为li个数增加被无限加宽,当ul+left宽度超出父容器box设定宽度时,ul被换行到left正下方了,想达到ul内自换行效果的话,ul增加个max-width: 900px就行

  1. 原因不太确定,隔得太久了
  2. 现在有布局专用的样式,需要横向布局就用 display:flex
  3. 不要乱用样式,比如不要用 display:table
<!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;
                list-style: none;
            }

            .box {
                width: 1200px;
                margin: 0 auto;
                background-color: hotpink;
            }

            .left {
                width: 300px;
                height: 300px;
                background-color: orange;
                float: left;
            }

            .box ul {
                width: 900px;
                float: left;
            }

            .box ul li {
                float: left;
                width: 300px;
                background-color: green;
                height: 150px;
            }

            .clearfix::before,
            .clearfix::after {
                content: "";
                clear: both;
                display: table;
            }
        </style>
    </head>

    <body>
        <div class="box clearfix">
            <div class="left"></div>
            <ul class="clearfix">
                <li></li>
                <li></li>
            </ul>
        </div>
    </body>
</html>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题