Object Fit

Utilities for controlling how a replaced element’s content should be resized.

Class reference

Class
Properties
.object-containobject-fit: contain;
.object-coverobject-fit: cover;
.object-fillobject-fit: fill;
.object-noneobject-fit: none;
.object-scale-downobject-fit: scale-down;

Contain

Resize an element’s content to stay contained within its container using .object-contain.

Object Fit - 图1

  1. <div class="bg-gray-400">
  2. <img class="object-contain h-48 w-full ...">
  3. </div>

Cover

Resize an element’s content to cover its container using .object-cover.

Object Fit - 图2

  1. <div class="bg-gray-400">
  2. <img class="object-cover h-48 w-full ...">
  3. </div>

Fill

Stretch an element’s content to fit its container using .object-fill.

Object Fit - 图3

  1. <div class="bg-gray-400">
  2. <img class="object-fill h-48 w-full ...">
  3. </div>

None

Display an element’s content at its original size ignoring the container size using .object-none.

Object Fit - 图4

  1. <div class="bg-gray-400">
  2. <img class="object-none h-48 w-full ...">
  3. </div>

Scale Down

Display an element’s content at its original size but scale it down to fit its container if necessary using .object-scale-down.

Object Fit - 图5

  1. <div class="bg-gray-400">
  2. <img class="object-scale-down h-48 w-full ...">
  3. </div>

Responsive

To control how a replaced element’s content should be resized only at a specific breakpoint, add a {screen}: prefix to any existing object fit utility. For example, adding the class md:object-scale-down to an element would apply the object-scale-down utility at medium screen sizes and above.

For more information about Tailwind’s responsive design features, check out the Responsive Design documentation.

Object Fit - 图6

all

Object Fit - 图7

sm

Object Fit - 图8

md

Object Fit - 图9

lg

Object Fit - 图10

xl

  1. <div class="bg-gray-400">
  2. <img class="object-contain sm:object-cover md:object-fill lg:object-none xl:object-scale-down ..." src="...">
  3. </div>

Object Fit - 图11

Customizing

Responsive and pseudo-class variants

By default, only responsive variants are generated for object-fit utilities.

You can control which variants are generated for the object-fit utilities by modifying the objectFit property in the variants section of your tailwind.config.js file.

For example, this config will also generate hover and focus variants:

  1. // tailwind.config.js
  2. module.exports = {
  3. variants: {
  4. // ...
  5. - objectFit: ['responsive'],
  6. + objectFit: ['responsive', 'hover', 'focus'],
  7. }
  8. }

Disabling

If you don’t plan to use the object-fit utilities in your project, you can disable them entirely by setting the objectFit property to false in the corePlugins section of your config file:

  1. // tailwind.config.js
  2. module.exports = {
  3. corePlugins: {
  4. // ...
  5. + objectFit: false,
  6. }
  7. }

← Clear   Object Position →