【问题标题】:Float Issue: content jumping out of parent div浮动问题:内容跳出父 div
【发布时间】:2020-06-10 14:28:16
【问题描述】:

我知道如果我浮动一个元素,如果后面的元素没有自己设置浮动或至少清除浮动,则后面的元素最终会消失在该元素后面。就像本例中的“框三”一样。但是为什么框三的内容会跳出div呢?数字 3 或盒子的任何潜在内容不应该在inside »box three«吗?

http://jsfiddle.net/7vw4Leg5/

<div class="box one">1</div>
<div class="box two">2</div>
<div class="box three">3</div>

.box {
    text-align: center;
    font-size: 30px;
    color: red;
    margin: 5px;
    width: 200px;
    height: 100px;
    background: grey;
}
.two {
    border: 2px solid red;
    float: left;
    opacity: 0.66;
}
.three {
    opacity: 0.33
}

*编辑:

这里是另一个例子来解释我不明白的问题。 为什么数字二不在蓝色框内? @Terry:好的,如果我减小第一个框的宽度,内容会向上跳一行并进入 div。但为什么一开始就不存在呢?盒子是空的,是不是没有足够的空间?

http://jsfiddle.net/utsc84pq/

<div class="box one">1</div>
<div class="box two">2</div>

.box {
    font-size: 40px;
    margin: 5px;
    width: 300px;
    height: 150px;
}
.one{
    float: left;
    border: 5px solid rgba(255, 154, 188, 0.9);
    background-color: rgba(255, 165, 0, 0.25);
}
.two {
    position: relative;
    top: 170px;
    border: 5px solid rgba(35, 154, 255, 0.5);
    background-color: rgba(100, 165, 255, 0.25);
}

【问题讨论】:

    标签: html css css-float


    【解决方案1】:

    试试这个。

    .box {
        text-align: center;
        font-size: 30px;
        display:block;
        color: red;
        margin: 5px;
        width: 200px;
        height: 100px;
        line-height:100px;
        background: grey;
    }
    
    .one{
        border:1px solid green;
        display:block;
        
    }
    .two {
        
        border: 1px solid red;
        opacity: 0.66;
    }
    .three {
     border: 1px solid yellow;
        opacity: 0.66;
    }
    <div class="box one">1</div>
    <div class="box two">2</div>
    <div class="box three">3</div>

    【讨论】:

    • 您好 Kadeem,感谢您的回答,但我认为您可能错过了这里的重点。由于故意浮动,我引发了覆盖,但对内容相对于它的 div 的位置感到困惑。如果我误解了您的回答,请提前道歉。
    • 别担心,至少你得到了答案:)
    【解决方案2】:

    是的,内容应该在框 3 内,但是由于您已将所有框设置为相同的宽度,这意味着框 3 中的内容不能被推到一边,而只能推到底部——您可以看到如何如果您减小它的宽度,float 会与框 3 的内容交互:​​

    .box {
        text-align: center;
        font-size: 30px;
        color: red;
        margin: 5px;
        width: 200px;
        height: 100px;
        background: grey;
    }
    .two {
        border: 2px solid red;
        float: left;
        opacity: 0.66;
        width: 25px;
    }
    .three {
        opacity: 0.3;
    }
    <div class="box one">1</div>
    <div class="box two">2</div>
    <div class="box three">3 random content here</div>

    【讨论】:

    • 我明白了,这意味着 div 的背景可以叠加,但浮动内容不能叠加。对吗?
    • 嘿特里我更新了我的帖子,其实我不太明白这个。
    猜你喜欢
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多