Open In App

BootStrap 5 Grid System Variable Width Content

Improve
Improve
Like Article
Like
Save
Share
Report

 Grid system Variable width content means in a grid system we can add a setting where the column will adjust to the content inside it and occupy that space only. In this setting, we can add responsiveness by which we can specify the screen size above which that column will have variable width with content.

BootStrap 5 Grid System Variable Width Content Classes:

  • col-[i]-auto: This class is used to add the Variable width setting and the [i] in the class is replaced by a screen size such as [sm/md/lg/xl/xxl].

Syntax:

<div class="row">
    <div class="col-[i]-auto">
      content
    </div>
</div>

Example 1: The code below demonstrates how the variable width content in a grid works:

HTML




<!doctype html>
<html lang="en">
  <head>
    <link href=
    rel="stylesheet" 
    integrity=
"sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" 
    crossorigin="anonymous">
    <script src=
    integrity=
"sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" 
    crossorigin="anonymous">
</script>
 </head>
  <body>
    <h1 class="text-success">GeeksforGeeks</h1>
    <strong>BootStrap 5 Grid system Variable width content</strong>
    <pre class="m-4">
        The middle grid column takes the width of the content that is the Image
    </pre>
    <div class="container">
        <div class="row justify-content-md-center">
          <div class="col col-lg-2 bg-light border">
            First Column
          </div>
          <div class="col-lg-auto bg-light border">
            <img src=
            alt="">
          </div>
          <div class="col col-lg-2 bg-light border">
            Last Column
          </div>
        </div>
    </div>
  </body>
</html>


Output:

Bootstrap 5 Grid System Variable Width Content

Example 2: The code below demonstrates how when the image size of the carousel changes the width of the middle grid which has the Variable width content also changes:

HTML




<!doctype html>
<html lang="en">
  <head>
    <link href=
        rel="stylesheet" 
        integrity=
"sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
        crossorigin="anonymous">
    <script src=
        integrity=
"sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" 
        crossorigin="anonymous">
</script>
  </head>
  <body>
    <h1 class="text-success">GeeksforGeeks</h1>
    <strong>BootStrap 5 Grid system Variable width content</strong>
    <pre class="m-4">
    The middle grid column takes the width of the Image of the carousel
    </pre>
    <div class="container">
        <div class="row justify-content-md-center">
          <div class="col-3 bg-light border">
            First Column
          </div>
          <div id="carouselExample" 
          class="col-7 carousel slide justify-center" 
          data-bs-ride="carousel">
            <div class="carousel-inner">
              <div class="carousel-item active">
                <img src=
                class="d-block w-25">
              </div>
              <div class="carousel-item">
                <img src=
                class="d-block w-25">
              </div>
              <div class="carousel-item">
                <img src=
            class="d-block w-25">
              </div>
            </div>
          </div>
          <div class="col-2 bg-light border">
            Last Column
          </div>
        </div>
    </div>
  </body>
</html>


Output:

Bootstrap 5 Grid System Variable Width Content

Reference: https://getbootstrap.com/docs/5.0/layout/grid/#variable-width-content 



Last Updated : 25 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads