Back to Tailwindcss

Object Fit

src/docs/object-fit.mdx

latest3.8 KB
Original Source

import { ApiTable } from "@/components/api-table.tsx"; import { Example } from "@/components/example.tsx"; import { Figure } from "@/components/figure.tsx"; import { ResponsiveDesign } from "@/components/content.tsx"; import { Stripes } from "@/components/stripes.tsx";

export const title = "object-fit"; export const description = "Utilities for controlling how a replaced element's content should be resized.";

<ApiTable rows={[ ["object-contain", "object-fit: contain;"], ["object-cover", "object-fit: cover;"], ["object-fill", "object-fit: fill;"], ["object-none", "object-fit: none;"], ["object-scale-down", "object-fit: scale-down;"], ]} />

Examples

Resizing to cover

Use the object-cover utility to resize an element's content to cover its container:

<Figure> <Example> {

} </Example>

html
<!-- [!code classes:object-cover] -->

</Figure>

Containing within

Use the object-contain utility to resize an element's content to stay contained within its container:

<Figure> <Example> { <div className="relative mx-auto w-96"> <Stripes border className="absolute inset-0 h-full rounded-lg" />
</div>

} </Example>

html
<!-- [!code classes:object-contain] -->

</Figure>

Stretching to fit

Use the object-fill utility to stretch an element's content to fit its container:

<Figure> <Example> {

} </Example>

html
<!-- [!code classes:object-fill] -->

</Figure>

Scaling 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:

<Figure> <Example> { <div className="relative mx-auto w-96 rounded-lg"> <Stripes border className="absolute inset-0 h-full rounded-lg" />
</div>

} </Example>

html
<!-- [!code classes:object-scale-down] -->

</Figure>

Using the original size

Use the object-none utility to display an element's content at its original size ignoring the container size:

<Figure> <Example> {

} </Example>

html
<!-- [!code classes:object-none] -->

</Figure>

Responsive design

<ResponsiveDesign property="object-fit">
html
<!-- [!code classes:md:object-cover] -->

</ResponsiveDesign>