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(用于缩小)
  • 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 值,也存在使用 .object-fit-{breakpoint}-{value} 格式的响应式变化,适用于以下断点缩写: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

对象适应工具在我们的实用程序 API scss/_utilities.scss 中声明。了解如何使用实用程序 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,
  )
),