实现一个父元素包裹多个子元素,且每行只能有三个宽度不固定的子元素。父元素内部边距是32px,相邻两个B元素的间距是16px。
<!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>
.parent {
box-sizing: border-box;
min-width: 1000px;
padding: 32px;
display: flex;
flex-wrap: wrap;
border: 1px solid #000;
}
.client-b {
/** 考虑到border 设置盒子模型 **/
box-sizing: border-box;
width: calc((100% - 32px) / 3);
/* flex: 0 0 32%; */
height: 100px;
border: 1px solid #999;
}
.client-b:nth-child(3n + 2) {
margin: 0 16px;
}
</style>
</head>
<body>
<div class="parent">
<div class="client-b"></div>
<div class="client-b"></div>
<div class="client-b"></div>
<div class="client-b"></div>
<div class="client-b"></div>
<div class="client-b"></div>
<div class="client-b"></div>
</div>
</body>
</html>
如果是4个呢 ?
<!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>
.parent {
box-sizing: border-box;
min-width: 1000px;
padding: 32px;
display: flex;
flex-wrap: wrap;
border: 1px solid #000;
}
.client-b {
margin-right: 16px;
box-sizing: border-box;
width: calc((100% - 48px) / 4);
/* flex: 0 0 32%; */
height: 100px;
border: 1px solid #999;
}
.client-b:nth-child(4n) {
margin-right: 0;
}
</style>
</head>
<body>
<div class="parent">
<div class="client-b"></div>
<div class="client-b"></div>
<div class="client-b"></div>
<div class="client-b"></div>
<div class="client-b"></div>
<div class="client-b"></div>
<div class="client-b"></div>
</div>
</body>
</html>