html - 图像在较小的设备上移动到下一行

标签 html css bootstrap-4

预期行为

图像应该缩小/放大并保持在同一行

实际行为

我创建了一行图像,这些图像应该在较小的设备上按比例缩小,但它们却分成了新的一行。


我正在使用 Bootstrap 4。

代码

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<!-- src omitted -->

<div style="margin:auto; display:table;">
  <img class="img-fluid" />  
  <img class="img-fluid" />  
  <img class="img-fluid" />
  <img class="img-fluid" />
  <img class="img-fluid" />
  <img class="img-fluid" /> 
</div>

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

最佳答案

使用flexbox并保持默认的nowrap

.img-fluid {
  min-width: 0;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<div class="d-flex justify-content-center">
  <img class="img-fluid" src="https://picsum.photos/200/200?image=1069">
  <img class="img-fluid" src="https://picsum.photos/200/200?image=1065">
  <img class="img-fluid" src="https://picsum.photos/200/200?image=1063">
  <img class="img-fluid" src="https://picsum.photos/200/200?image=1062">
  <img class="img-fluid" src="https://picsum.photos/200/200?image=1063">
  <img class="img-fluid" src="https://picsum.photos/200/200?image=1062">
</div>

要在两个方向上缩放,您可以为图像添加另一个容器:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<div class="d-flex justify-content-center">
  <div><img class="img-fluid" src="https://picsum.photos/200/200?image=1069"></div>
  <div><img class="img-fluid" src="https://picsum.photos/200/200?image=1065"></div>
  <div><img class="img-fluid" src="https://picsum.photos/200/200?image=1063"></div>
  <div><img class="img-fluid" src="https://picsum.photos/200/200?image=1062"></div>
  <div><img class="img-fluid" src="https://picsum.photos/200/200?image=1063"></div>
  <div><img class="img-fluid" src="https://picsum.photos/200/200?image=1062"></div>
</div>

关于html - 图像在较小的设备上移动到下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51274334/

相关文章:

javascript - Jquery UI Resizable with box-sizing : "border-box" issue

css - Weebly CSS 编辑导航菜单

html - 如何从 BootstrapCDN 自定义 Bootstrap 4.3

html - 将 <div> 与图像放在同一行

html - 水平滚动时保留一列

php - 上传文件为空 Laravel 5.4

javascript - 使用特殊字符 "-"调用 <div> 元素

html - 通过 css 模糊没有背景图像的元素

javascript - Bootstrap 弹出窗口不适用于 html 注入(inject)

javascript - Bootstrap 4 使用 JSON 进行模态验证