跳到主要内容

关于将图片设置为响应式行为(使其宽度永远不会超过父元素)以及向其添加轻量级样式的文档和示例——所有操作都通过类完成。

响应式图片

Bootstrap 中的图像通过 .img-fluid 实现响应式。这会将 max-width: 100%; 和 height: auto; 应用于图像,使其随父元素宽度缩放。

PlaceholderResponsive image
HTML
<img src="..." class="img-fluid" alt="...">

图片缩略图

除了我们的 border-radius 工具 外,你还可以使用 .img-thumbnail 给图片一个圆角 1px 边框的效果。

A generic square placeholder image with a white border around it, making it resemble a photograph taken with an old instant camera200x200
HTML
<img src="..." class="img-thumbnail" alt="...">

对齐图像

使用辅助浮动类或文本对齐类对齐图像。block级图像可以使用.mx-auto边距工具类居中。

Placeholder200x200 Placeholder200x200
HTML
<img src="..." class="rounded float-start" alt="...">
<img src="..." class="rounded float-end" alt="...">
Placeholder200x200
HTML
<img src="..." class="rounded mx-auto d-block" alt="...">
Placeholder200x200
HTML
<div class="text-center">
  <img src="..." class="rounded" alt="...">
</div>

图片

如果你正在使用 <picture> 用于指定多个的元素 <source> 特定的元素 <img>,确保添加 .img-* 到……的类 <img> 而不是给 <picture> 标签。

<picture>
  <source srcset="..." type="image/svg+xml">
  <img src="..." class="img-fluid img-thumbnail" alt="...">
</picture>

CSS

Sass 变量

图像缩略图可用变量。

$thumbnail-padding:                 .25rem;
$thumbnail-bg:                      var(--#{$prefix}body-bg);
$thumbnail-border-width:            var(--#{$prefix}border-width);
$thumbnail-border-color:            var(--#{$prefix}border-color);
$thumbnail-border-radius:           var(--#{$prefix}border-radius);
$thumbnail-box-shadow:              var(--#{$prefix}box-shadow-sm);