盒子水平布局方式简单一览
- 使用浮动 < div >;< ul >;< ol >
- 使用弹性盒子 display: flex;
- 使用网格布局 display: grid;
1 - 浮动
< div >
用div盒子进行大盒子套小盒子进行布局,使用浮动进行水平排列,元素间距用“外间距-margin”和“内间距-padding”来控制
<style>
.parent {
width: 510px;
height: 400px;
background: #f7f7f7;
padding-left: 20px;
}
.children {
width: 150px;
height: 280px;
float: left;
background: #dddddd;
margin: 20px 20px 0 0;
}
</style>
-------
<body>
<div class="parent">
<div class="children"></div>
<div class="children"></div>
<div class="children"></div>
</div>
</body>
这样布局缺点也很明显,不够灵活,在间距控制上并不方便,如果只是简单的几个元素布局可以用一下。ps:如果浮动影响了后面的布局,可以用 “clear: both;” 清除浮动。
< ul >或者< ol >
使用列表跟直接用div作用相差并不是很大,用法也跟div的用法差不多,只不过在列表里除了浮动还可用display进行布局。
使用浮动float;
<style>
.parent {
width: 510px;
height: 400px;
background: #f7f7f7;
padding-left: 20px;
list-sty