要使用Bootstrap创建一个包含两列三行的布局,其中每个单元格都包含一个产品图片,并且列与列之间的间隔为10px,你可以采用类似的方法来实现,但需要对行和列进行适当的布局。
以下示例代码使用Bootstrap 5,并添加了自定义CSS来实现10px的间隔。每个.col都包含一个.product-image,这样就可以在两列三行的布局中展示六个产品图片。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bootstrap Two Column, Three Rows Example</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/5.1.3/css/bootstrap.min.css" rel="stylesheet">
<style>
/* 自定义间隔 */
.custom-gutter {
margin-right: -5px;
margin-left: -5px;
}
.custom-gutter > [class^="col"], .custom-gutter > [class*=" col"] {
padding-right: 5px;
padding-left: 5px;
}
.product-image {
width: 100%; /* 让图片填满列的宽度 */
height: auto; /* 保持图片的原始比例 */
object-fit: cover; /* 如果需要的话,可以通过这个属性裁剪图片 */
margin-bottom: 10px; /* 为图片之间添加垂直间隔 */
}
</style>
</head>
<body>
<div class="container">
<!-- 第一行 -->
<div class="row custom-gutter">
<div class="col">
<img src="path/to/your/image1.jpg" alt="Product 1" class="product-image">
</div>
<div class="col">
<img src="path/to/your/image2.jpg" alt="Product 2" class="product-image">
</div>
</div>
<!-- 第二行 -->
<div class="row custom-gutter">
<div class="col">
<img src="path/to/your/image3.jpg" alt="Product 3" class="product-image">
</div>
<div class="col">
<img src="path/to/your/image4.jpg" alt="Product 4" class="product-image">
</div>
</div>
<!-- 第三行 -->
<div class="row custom-gutter">
<div class="col">
<img src="path/to/your/image5.jpg" alt="Product 5" class="product-image">
</div>
<div class="col">
<img src="path/to/your/image6.jpg" alt="Product 6" class="product-image">
</div>
</div>
</div>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/5.1.3/js/bootstrap.bundle.min.js"></script>
</body>
</html>
请替换path/to/your/imageX.jpg路径为你的图片路径。这段代码通过.custom-gutter类来调整列之间和行之间的间隔,使其达到期望的10px。.product-image类确保每张图片都能适应其容器的宽度,同时保持自身的比例。通过为.product-image设置margin-bottom,我们为图片之间提供了垂直间隔。这样布局在较小屏幕上可能会堆叠,以适应不同尺寸的设备。