我有一个非常简单的响应式图库,仅包含6个缩略图。在大中型设备中,它显示完美。但是,在移动视图中,缩略图变得太小。如果添加填充,则边距会“中断”,并且它们将不再对齐。有没有办法在不“破坏”边距的情况下使它们更大一些?我发布了图片,以便于理解。
在下图中,您可以看到边距对齐,但是缩略图太小。
在下面,您可以看到当我添加填充时会发生什么。缩略图更大,但是边距不再对齐。
.container {
max-width: 98%;
margin: 0 auto;
}
.image-container {
background-color: #ffffff;
padding: 10px;
}
.main-image-container img {
max-width: 100%;
height: auto;
}
.thumbnail-image-container img {
max-width: 100%;
height: auto;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<div class="container mt-4">
<div class="row">
<div class="col-lg-7 image-container">
<div class="row">
<div class="col-12 main-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
</div>
<div class="row pt-2">
<div class="col-2 thumbnail-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
<div class="col-2 thumbnail-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
<div class="col-2 thumbnail-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
<div class="col-2 thumbnail-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
<div class="col-2 thumbnail-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
<div class="col-2 thumbnail-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
</div>
</div>
答案 0 :(得分:0)
一种解决方案是向所有col-*
类添加相同的填充。 Bootstrap具有名为p-*
的填充类。下一个示例将p-1
类添加到main-image-container
和thumbnail-image-container
元素中。
.container {
max-width: 98%;
margin: 0 auto;
}
.image-container {
background-color: #ffffff;
padding: 10px;
}
.main-image-container img {
max-width: 100%;
height: auto;
}
.thumbnail-image-container img {
max-width: 100%;
height: auto;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<div class="container mt-4">
<div class="row">
<div class="col-lg-7 image-container">
<div class="row">
<div class="col-12 p-1 main-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
</div>
<div class="row pt-2">
<div class="col-2 p-1 thumbnail-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
<div class="col-2 p-1 thumbnail-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
<div class="col-2 p-1 thumbnail-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
<div class="col-2 p-1 thumbnail-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
<div class="col-2 p-1 thumbnail-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
<div class="col-2 p-1 thumbnail-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
</div>
</div>
</div>
</div>
第二种解决方案是使用正确的col-*
类将缩略图分为两行。像下面的例子一样:
.container {
max-width: 98%;
margin: 0 auto;
}
.image-container {
background-color: #ffffff;
padding: 10px;
}
.main-image-container img {
max-width: 100%;
height: auto;
}
.thumbnail-image-container img {
max-width: 100%;
height: auto;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<div class="container mt-4">
<div class="row">
<div class="col-lg-7 image-container">
<div class="row">
<div class="col-12 main-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
</div>
<div class="row pt-1">
<div class="col-4 col-md-2 mt-2 thumbnail-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
<div class="col-4 col-md-2 mt-2 thumbnail-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
<div class="col-4 col-md-2 mt-2 thumbnail-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
<div class="col-4 col-md-2 mt-2 thumbnail-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
<div class="col-4 col-md-2 mt-2 thumbnail-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
<div class="col-4 col-md-2 mt-2 thumbnail-image-container img-fluid">
<img src="https://placeholdit.co//i/1280x720?&text=Test">
</div>
</div>
</div>
</div>
</div>