Open In App

Tailwind CSS Object Fit

Last Updated : 11 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

This class accepts more than one value in Tailwind CSS. All the properties are covered in class form. It is the alternative to the CSS object-fit property. This class is used to specify how an image or video should be resized to fit its content box for controlling a replaced element’s content resizing.

Object fit Classes

  • object-contain 
  • object-cover 
  • object-fill 
  • object-none 
  • object-scale-down

object-contain Class

This class is used to replace the image and preserves the aspect ratio of the original image while fitting within the content box. Resize an element’s content to stay contained within its container.

Syntax:

<element class="object-contain">...</element>

Example 1: This example demonstrates the use of the .object-contain class.

<!DOCTYPE html>
<html>

<head>
    <link href=
"https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" 
        rel="stylesheet">
</head>

<body class="text-center">
    <h1 class="text-green-600 text-5xl font-bold">
        GeeksforGeeks
    </h1>

    <p><b>Tailwind CSS object Class</b></p>

    <div class="bg-green-300 w-full h-full">
        <img class="object-contain h-48 w-full" src=
"https://media.geeksforgeeks.org/wp-content/uploads/20190807114330/GFG115.png">
    </div>
</body>

</html>

Output:

object-cover Class

This class preserves the aspect ratio of the original image as the replaced image while fitting in the content box. Sometimes it is clipped to fit when the aspect ratio of the original image doesn’t match with the aspect ratio of the content box. Resize an element’s content to cover its container using this class.

Syntax:

<element class="object-cover">...</element>

Example 2: This example demonstrates the use of the .object-cover class.

<!DOCTYPE html>
<html>

<head>
    <link href=
"https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" 
        rel="stylesheet">
</head>

<body class="text-center">
    <h1 class="text-green-600 text-5xl font-bold">
        GeeksforGeeks
    </h1>
    <b>Tailwind CSS object Class</b>
    <div class="bg-green-300 w-full h-full">
        <img class="object-cover h-48 w-full" src=
"https://media.geeksforgeeks.org/wp-content/uploads/20190807114330/GFG115.png">
    </div>
</body>

</html>

Output:

object-fill Class

This class replaced image is stretched to fit the content box. The replaced image will completely fill the box needless of its aspect ratio. Stretch an element’s content to fit its container using the class.

Syntax:

<element class="object-fill">...</element>

Example 3: This example demonstrates the use of the .object-fill class.

<!DOCTYPE html>
<html>

<head>
    <link href=
"https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" 
        rel="stylesheet">
</head>

<body class="text-center">
    <h1 class="text-green-600 text-5xl font-bold">
        GeeksforGeeks
    </h1>

    <p><b>Tailwind CSS object Class</b></p>

    <div class="bg-green-300 w-full h-full">
        <img class="object-fill h-48 w-full" src=
"https://media.geeksforgeeks.org/wp-content/uploads/20190807114330/GFG115.png">
    </div>
</body>

</html>

Output:

object-none Class

In this class, the replaced image will ignore the aspect ratio of the original image. Hence, it is not resized. The class displays an element’s content at its original size ignoring the container size.

Syntax:

<element class="object-none">...</element>

Example 4: This example demonstrates the use of the .object-none class.

<!DOCTYPE html>
<html>

<head>
    <link href=
"https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css"
        rel="stylesheet">
</head>

<body class="text-center">
    <h1 class="text-green-600 text-5xl font-bold">
        GeeksforGeeks
    </h1>

    <p><b>Tailwind CSS object Class</b></p>

    <div class="bg-green-300 w-full h-full">
        <img class="object-none h-48 w-full" src=
"https://media.geeksforgeeks.org/wp-content/uploads/20190807114330/GFG115.png">
    </div>
</body>

</html>

Output:

object-scale-down

This class replaced image is resized as if none or contain were specified and results in the smallest object size. The class displays an element’s content at its original size but scale it down to fit its container.

Syntax:

<element class="object-scale-down">...</element>

Example 5: This example demonstrates the use of the .object-scale-down class.

<!DOCTYPE html>
<html>

<head>
    <link href=
"https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" 
        rel="stylesheet">
</head>

<body class="text-center">
    <h1 class="text-green-600 text-5xl font-bold">
        GeeksforGeeks
    </h1>
    <b>Tailwind CSS object Class</b>
    <div class="bg-green-300 w-full h-full">
        <img class="object-scale-down h-48 w-full" src=
"https://media.geeksforgeeks.org/wp-content/uploads/20190807114330/GFG115.png">
    </div>
</body>

</html>

Output:



Next Article

Similar Reads

three90RightbarBannerImg