Open In App

Bootstrap 5 Sizing

Last Updated : 04 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Bootstrap 5 Sizing utility classes adjust element dimensions relative to the viewport or parent container. Options include percentage-based widths, and viewport-relative dimensions, facilitating responsive design for flexible layouts across various screen sizes.

Bootstrap 5 Sizing Classes

Class NameDescription
Relative to the ParentSets the height and width of the element relative to its parent.
Relative to the ViewportSets the height and width of the element relative to the viewport.
SassSass sizing utilities declared in the Utility API.

Syntax:

<div class="w-25">
    ...
</div> 

Examples of Bootstrap 5 Sizing

Example 1: In this example, we used the sizing classes relative to the parent to set the height and width of the element.

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" 
        content="IE=edge">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>Bootstrap 5 Sizing</title>

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" />

    <!-- Bootstrap Javascript -->   
    <script src=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js">
    </script>
</head>

<body>
    <div class="container">
        <div class="my-4">
            
            <strong>Bootstrap 5 Sizing</strong>
        </div>

        <h5>Relative to Parent Width</h5>
        <div class="bg-dark">
            <div class="w-100 p-2 mb-2 
                text-bg-success">
                Width = 100% of the parent
            </div>
            <div class="w-auto p-2 mb-2 text-bg-success">
                Width = auto</div>
            <div class="w-25 p-2 mb-2 text-bg-success">
                Width = 25% of the parent</div>
            <div class="w-50 p-2 mb-2 text-bg-success">
                Width = 50% of the parent</div>
            <div class="w-75 p-2 mb-2 text-bg-success">
                Width = 75% of the parent</div>
        </div>

        <h5>Relative to Parent Height</h5>
        <div class="bg-dark" style="height:200px;">
            <div class="h-100 w-auto p-3 text-bg-success 
                d-inline-block">100% Height</div>
            <div class="h-50 w-auto p-3 text-bg-success 
                d-inline-block">50% Height</div>
            <div class="h-25 w-auto p-3 text-bg-success 
                d-inline-block">25% Height</div>
            <div class="h-auto w-auto p-3 text-bg-success 
                d-inline-block">Auto Height</div>
            <div class="h-75 w-auto p-3 text-bg-success 
                d-inline-block">75% Height</div>
        </div>
    </div>
</body>

</html>

Output:

BootstrapSizing-(1)
Bootstrap 5 Sizing Example Output

Example 2: In this example, we used the sizing classes relative to the viewport to set the height and width of an element.

HTML
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta
            http-equiv="X-UA-Compatible"
            content="IE=edge"
        />
        <meta
            name="viewport"
            content="width=device-width, 
                     initial-scale=1.0"
        />
        <title>Bootstrap 5 Sizing</title>

        <!-- Bootstrap CSS -->
        <link
            rel="stylesheet"
            href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
        />

        <!-- Bootstrap Javascript -->
        <script src=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js">

        </script>
    </head>

    <body>
        <div class="container">
            <div class="my-4">
                <strong
                    >Bootstrap 5 Sizing</strong
                >
            </div>

            <h5>
                Relative to Viewport Height And
                Width
            </h5>

            <div
                class="min-vw-100 
                       p-3 
                       text-bg-success 
                       d-inline-block"
            >
                Min VW 100
            </div>
            <div
                class="vw-100 mt-3 
                       p-3 text-bg-success 
                       d-inline-block"
            >
                VW 100
            </div>
            <div
                class="min-vh-100 
                       mt-3 p-3 
                       text-bg-success 
                       d-inline-block"
            >
                Min VH 100
            </div>
            <div
                class="vh-100 p-3 
                       text-bg-success 
                       d-inline-block"
            >
                VH 100
            </div>
        </div>
    </body>
</html>

Output:

Bootstrap 5 Sizing Example Output

Similar Reads