<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>盒子水平排布的三种方式</title>
<style>
.contain {
width: 900px;
height: 600px;
margin: 100px auto;
}
.block {
width: 100%;
height: 33.33%;
font-size: 50px;
line-height: 200px;
text-align: center;
border: 1px solid #ddd
}
/* 方法一 flex 方法 */
.block:first-child{
display: flex;
}
.inA:nth-child(1){
height: 100%;
flex: 1
}
.inA:nth-child(2){
height: 100%;
flex: 1
}
.inA:nth-child(3){
height: 100%;
flex: 1
}
.block:nth-child(2){
position: relative;
}
/* 方法二 绝对定位方法 */
.inB{
position: absolute;
height: 100%;
width: 33.333%
}
.inB:nth-child(1) {
left: 0;
}
.inB:nth-child(2) {
left: 33.33%;
}
.inB:nth-child(3) {
right: 0;
}
/* 方法三 浮动方法 */
.inC{
float: left;
width: 33.33%;
}
/* 颜色 */
.bacRed {
background: red;
}
.bacsky {
background: skyblue;
}
.bacyellow {
background: yellow
}
</style>
</head>
<body>
<div class="contain">
<div class="block">
<div class="inA bacRed">方</div>
<div class="inA bacsky">法</div>
<div class="inA bacyellow">一</div>
</div>
<div class="block">
<div class="inB bacsky">方</div>
<div class="inB bacyellow">法</div>
<div class="inB bacRed">二</div>
</div>
<div class="block">
<div class="inC bacyellow">方</div>
<div class="inC bacRed">法</div>
<div class="inC bacsky">三</div>
</div>
</div>
</body>
</html>
CSS 盒子水平排布的三种方式
最新推荐文章于 2025-02-09 15:33:12 发布