1. 布局
  2. 对象适合

Quick reference

属性
object-containobject-fit: contain;
object-coverobject-fit: cover;
object-fillobject-fit: fill;
object-noneobject-fit: none;
object-scale-downobject-fit: scale-down;

基本用法(Basic usage)

调整大小以覆盖容器(Resizing to cover a container)

使用 object-cover 工具将元素的内容调整为覆盖其容器。

🌐 Use the object-cover utility to resize an element’s content to cover its container.

<div class="bg-indigo-300 ...">
  <img class="object-cover h-48 w-96 ...">
</div>

包含一个元素(Containing an element)

使用 object-contain 工具调整元素内容的大小,以保持其在容器内。

🌐 Use the object-contain utility to resize an element’s content to stay contained within its container.

<div class="bg-purple-300 ...">
  <img class="object-contain h-48 w-96 ...">
</div>

拉伸以适应容器(Stretching to fit a container)

使用 object-fill 工具将元素的内容拉伸以适应其容器。

🌐 Use the object-fill utility to stretch an element’s content to fit its container.

<div class="bg-sky-300 ...">
  <img class="object-fill h-48 w-96 ...">
</div>

如果太大则缩小(Scaling down if too large)

使用 object-scale-down 工具以元素的原始大小显示内容,但在必要时将其缩小以适应容器。

🌐 Use the object-scale-down utility to display an element’s content at its original size but scale it down to fit its container if necessary.

<div class="bg-cyan-300">
  <img class="object-scale-down h-48 w-96 ...">
</div>

使用元素的原始大小(Using an element’s original size)

使用 object-none 工具显示元素的内容,按原始大小显示,忽略容器大小。

🌐 Use the object-none utility to display an element’s content at its original size ignoring the container size.

<div class="bg-yellow-300">
  <img class="object-none h-48 w-96 ...">
</div>

有条件申请

悬停、聚焦及其他状态

Tailwind 允许你使用变体修饰符在不同状态下有条件地应用工具类。例如,使用hover:object-scale-down 仅在 hover 时应用 object-scale-down 工具。

<img class="object-contain hover:object-scale-down">

有关所有可用状态修饰符的完整列表,请查看悬停、聚焦、以及其他状态 文档。

断点和媒体查询

你还可以使用变体修饰符来定位媒体查询,例如响应式断点、暗黑模式、首选减少运动等。例如,使用 md:object-scale-down 仅在中等屏幕尺寸及以上时应用 object-scale-down 工具。

<img class="object-contain md:object-scale-down">

要了解更多信息,请查看有关 响应式设计暗黑模式、以及 其他媒体查询修饰符 的文档。