Skip to main content Skip to docs navigation
Added in v5.3

对象拟合

使用对象适合工具来修改 被替换的元素 的内容(例如 <img><video>)应如何调整大小以适合其容器。

怎么运行的

¥How it works

使用我们的响应式 object-fit 工具类更改 object-fit 属性 的值。此属性告诉内容以多种方式填充父容器,例如保留纵横比或拉伸以占用尽可能多的空间。

¥Change the value of the object-fit property with our responsive object-fit utility classes. This property tells the content to fill the parent container in a variety of ways, such as preserving the aspect ratio or stretching to take up as much space as possible.

object-fit 值的类使用 .object-fit-{value} 格式命名。从以下值中选择:

¥Classes for the value of object-fit are named using the format .object-fit-{value}. Choose from the following values:

  • contain

  • cover

  • fill

  • scale(用于缩小)

    ¥scale (for scale-down)

  • none

示例

¥Examples

object-fit-{value} 类添加到 被替换的元素 中:

¥Add the object-fit-{value} class to the replaced element:

Placeholder : Object fit contain Placeholder : Object fit cover Placeholder : Object fit fill Placeholder : Object fit scale down Placeholder : Object fit none
html
<img src="..." class="object-fit-contain border rounded" alt="...">
<img src="..." class="object-fit-cover border rounded" alt="...">
<img src="..." class="object-fit-fill border rounded" alt="...">
<img src="..." class="object-fit-scale border rounded" alt="...">
<img src="..." class="object-fit-none border rounded" alt="...">

响应式

¥Responsive

对于使用 .object-fit-{breakpoint}-{value} 格式的每个 object-fit 值,也存在响应变化,对于以下断点缩写:smmdlgxlxxl。可以根据需要组合类以获得各种效果。

¥Responsive variations also exist for each object-fit value using the format .object-fit-{breakpoint}-{value}, for the following breakpoint abbreviations: sm, md, lg, xl, and xxl. Classes can be combined for various effects as you need.

Placeholder : Contain on sm Placeholder : Contain on md Placeholder : Contain on lg Placeholder : Contain on xl Placeholder : Contain on xxl
html
<img src="..." class="object-fit-sm-contain border rounded" alt="...">
<img src="..." class="object-fit-md-contain border rounded" alt="...">
<img src="..." class="object-fit-lg-contain border rounded" alt="...">
<img src="..." class="object-fit-xl-contain border rounded" alt="...">
<img src="..." class="object-fit-xxl-contain border rounded" alt="...">

视频

¥Video

.object-fit-{value} 和响应式 .object-fit-{breakpoint}-{value} 工具也适用于 <video> 元素。

¥The .object-fit-{value} and responsive .object-fit-{breakpoint}-{value} utilities also work on <video> elements.

<video src="..." class="object-fit-contain" autoplay></video>
<video src="..." class="object-fit-cover" autoplay></video>
<video src="..." class="object-fit-fill" autoplay></video>
<video src="..." class="object-fit-scale" autoplay></video>
<video src="..." class="object-fit-none" autoplay></video>

CSS

Sass 工具 API

¥Sass utilities API

对象拟合工具在 scss/_utilities.scss 中的工具 API 中声明。Learn how to use the utilities API.

¥Object fit utilities are declared in our utilities API in scss/_utilities.scss. Learn how to use the utilities API.

"object-fit": (
  responsive: true,
  property: object-fit,
  values: (
    contain: contain,
    cover: cover,
    fill: fill,
    scale: scale-down,
    none: none,
  )
),