Horje
How to Control the Background Size in Tailwind CSS ?

To control background-size in Tailwind CSS, use the bg-{size} utility classes. These classes allow you to adjust the background size, providing options like bg-cover for covering the entire element, bg-contain for fitting the content within, and custom sizes such as bg-auto or bg-50%. Tailwind simplifies background sizing, offering flexibility and ease of customization for a seamless design experience.

Using bg-cover

The “bg-cover” class in Tailwind CSS ensures full coverage of the background image within the container.

Syntax:

<element class="bg-cover">
<!-- Your content goes here -->
</element>

Example: Implementation to show bg-cover property.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0">
    <title>Full Coverage</title>
    <link rel="stylesheet" href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css">
</head>

<body class="min-h-screen flex items-center justify-center bg-gray-300">
    <div class="bg-cover w-64 h-64 border border-black">
        <p class="text-green-500 text-center">Full Coverage ( bg-cover)</p>
    </div>
</body>

</html>

Output:

Using bg-contain

The “bg-contain” class sets the background image size to fit within the container dimensions, scaling it accordingly.

<element class="bg-contain">
<!-- Your content goes here -->
</element>

Example: Implementation to show bg-contain property.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0">
    <title>Contain</title>
    <link rel="stylesheet" href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css">
</head>

<body class="min-h-screen flex items-center justify-center bg-gray-300">
    <div class="bg-contain w-64 h-64 border border-black">
        <p class="text-green-500 text-center">bg-contain</p>
    </div>
</body>

</html>

Output:

Using bg-auto

Tailwind’s “bg-auto” class adjusts the background size automatically based on its content.

<element class="bg-auto">
<!-- Your content goes here -->
</element>

Example: Implementation to show bg-auto property.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0">
    <title>Automatic Size</title>
    <link rel="stylesheet" href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css">
</head>

<body class="min-h-screen flex items-center justify-center bg-gray-300">
    <div class="bg-auto w-64 h-64 border border-black">
        <p class="text-green-500 text-center">Automatic Size (bg-auto)</p>
    </div>
</body>

</html>

Output:




Reffered: https://www.geeksforgeeks.org


CSS

Related
How to Create Animated Loading Button using Tailwind CSS ? How to Create Animated Loading Button using Tailwind CSS ?
How to Apply Opacity to a Color Variable in CSS ? How to Apply Opacity to a Color Variable in CSS ?
How to Create a Frosted Navbar with TailwindCSS ? How to Create a Frosted Navbar with TailwindCSS ?
How to Change the Direction of a Gradient in Tailwind CSS ? How to Change the Direction of a Gradient in Tailwind CSS ?
How to Add Background Color in Input and Text Fields using CSS ? How to Add Background Color in Input and Text Fields using CSS ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13