javascript - 边距底部不适用于包含三个浮动 div 的 div
问题描述
* {
box-sizing: border-box;
}
.box {
float: left;
width: 33.33%;
padding: 50px;
}
<h2>Grid of Boxes</h2>
<p>Float boxes side by side:</p>
<div class="big-box" style="margin-bottom:30px;">
<div class="box" style="background-color:#bbb">
<p>Some text inside the box.</p>
</div>
<div class="box" style="background-color:#ccc">
<p>Some text inside the box.</p>
</div>
<div class="box" style="background-color:#ddd">
<p>Some text inside the box.</p>
</div>
</div>
<p style="clear:both;">Note that we also use the clearfix hack to take care of the layout flow, and that add the box-sizing property to make sure that the box doesn't break due to extra padding. Try to remove this code to see the effect.</p>
在上面的代码中margin-bottom:30px
不起作用。为什么???我还需要建议如何在这种场景中应用边距底部。我对 CSS 很陌生,这可能是一个非常愚蠢的问题,或者代码可能包含一些愚蠢的错误。div
class = big-box
解决方案
要回答您的问题:
- 您的大框的边距底部正在工作,但与 3 个浮动框相比,30px 仍然很小。所以当你看它时,它看起来不像是大盒子包裹着 3 个盒子。
有2个解决方案:
- 指定一个大于或至少等于 3 个浮动框的高度。
- 从你的盒子类中删除 float:left 并给出大盒子 display:flex 和 100% 宽度
下面的示例是第二种解决方案。
* {
box-sizing: border-box;
}
.box{
width:33.33%;
padding:50px;
}
.big-box{
display:flex;
margin-bottom:30px;
width:100%;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Grid of Boxes</h2>
<p>Float boxes side by side:</p>
<div class="big-box" >
<div class="box" style="background-color:#bbb">
<p>Some text inside the box.</p>
</div>
<div class="box" style="background-color:#ccc">
<p>Some text inside the box.</p>
</div>
<div class="box" style="background-color:#ddd">
<p>Some text inside the box.</p>
</div>
</div>
<p style="clear:both;">
Note that we also use the clearfix hack to take care of the layout flow,
and that add the box-sizing property to make sure that the box doesn't
break due to extra padding. Try to remove this code to see the effect.
</p>
</body>
</html>
希望这是您正在寻找的答案。
推荐阅读
- amazon-web-services - AWS 上的 Kubernetes HTTP 到 HTTPS 重定向,ELB 终止 SSL
- c# - XML 命名空间“使用:Microsoft.Toolkit.Uwp.UI.Controls”中的未知类型“UniformGrid”
- c# - ASP.NET Core 2.1 与 Windows 身份验证和 Active Directory 的集成测试
- pandas - 删除多索引数据框中的行
- javascript - 剑道 jQuery 电子表格日期值保存
- mysql - 如何在mysql中查找下一个日期
- regex - 正则表达式:在引号内查找第一次出现
- vba - 工作簿和 vlookup 之间的 ms-Excel 工作表传输
- python-3.x - Python - 进行搜索并从搜索引擎检索一定数量的图像
- android - 单击listView项目时如何在Searchview中设置文本