图片

让图片具备响应式行为(使其宽度永远不会超过父元素)并为其添加轻量样式的文档和示例——全部通过类实现。

Layer: content

响应式图片

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

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

图片缩略图

除了我们的边框圆角工具类外,你还可以使用 .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>

图注

每当你需要显示一段内容时——例如带有可选标题的图片,考虑使用 <figure> 元素。图注在 Bootstrap 中是 Flex 容器,因此你可以使用我们的 Flex 布局工具类轻松修改布局和对齐方式。

使用包含的 .figure 和 .figure-caption 类为 HTML5 <figure> 和 <figcaption> 元素提供一些基础样式。图注中的图片没有明确的尺寸,因此请务必为 <img> 添加 .img-fluid 类使其具备响应式。

Placeholder400x300
A caption for the above image.
HTML
<figure class="figure">
  <img src="..." class="img-fluid rounded-3" alt="...">
  <figcaption class="figure-caption">A caption for the above image.</figcaption>
</figure>

以及反向对齐:

Placeholder400x300
A caption for the above image.
HTML
<figure class="figure align-items-end">
  <img src="..." class="img-fluid rounded-3" alt="...">
  <figcaption class="figure-caption">A caption for the above image.</figcaption>
</figure>

Picture

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

为 <img> 提供描述图片的 alt 文本,或当它仅为装饰性时提供空的 alt=""。下面示例中的 alt="..." 是该文本的占位符。

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

CSS

变量

Images use local CSS variables on .img-thumbnail for real-time customization. Values for the CSS variables are generated from Sass maps unique to each component and applied to the aforementioned class.

// stylelint-disable-next-line scss/dollar-variable-default
$thumbnail-tokens: defaults(
  (
    --thumbnail-padding: .25rem,
    --thumbnail-bg: var(--bg-body),
    --thumbnail-border-width: var(--border-width),
    --thumbnail-border-color: var(--border-color),
    --thumbnail-border-radius: var(--radius-5),
    --thumbnail-box-shadow: var(--box-shadow-sm),
  ),
  $thumbnail-tokens
);

图注也是如此:

// stylelint-disable-next-line scss/dollar-variable-default
$figure-tokens: defaults(
  (
    --figure-gap: calc(var(--spacer) * .5),
    --figure-caption-font-size: var(--font-size-sm),
    --figure-caption-color: var(--fg-3),
  ),
  $figure-tokens
);