在div中包含每3个div,可以使用CSS的Flexbox或Grid布局来实现。以下是两种方法的示例代码:
<!DOCTYPE html>
<html>
<head><style>
.container {
display: flex;
flex-wrap: wrap;
}
.item {
flex: 1 0 calc(33.333% - 20px);
background-color: lightblue;
margin: 10px;
padding: 10px;
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
<div class="item">Item 6</div>
<div class="item">Item 7</div>
<div class="item">Item 8</div>
<div class="item">Item 9</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head><style>
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 20px;
}
.item {
background-color: lightblue;
padding: 10px;
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
<div class="item">Item 6</div>
<div class="item">Item 7</div>
<div class="item">Item 8</div>
<div class="item">Item 9</div>
</div>
</body>
</html>
这两种方法都可以实现在div中包含每3个div的效果,可以根据实际需求选择合适的方法。
领取专属 10元无门槛券
手把手带您无忧上云