Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the size of a nested row with multiple columns inside col-auto more than content width?

If I understand it correctly, the col-auto classes in Bootstrap is supposed to only use the natural width of the content. When I use col-lg-auto in the code below, the width becomes larger than the content.

I've created this Codepen that demonstrates the issue.

<div class="row">
  <div class="col">
    <div class="row">
      <div class="col-12 col-sm-6 col-lg-4">Col</div>
      <div class="col-12 col-sm-6 col-lg-4">Col</div>
      <div class="col-12 col-sm-6 col-lg-4">Col</div>
      <div class="col-12 col-sm-6 col-lg-4">Col</div>
      <div class="col-12 col-sm-6 col-lg-4">Col</div>
      <div class="col-12 col-sm-6 col-lg-4">Col</div>
      <div class="col-12 col-sm-6 col-lg-4">Col</div>
      <div class="col-12 col-sm-6 col-lg-4">Col</div>
      <div class="col-12 col-sm-6 col-lg-4">Col</div>
    </div>
  </div>
  <!-- Replace col-lg-auto with col-lg and add class flex-grow-0 -->
  <div class="col-12 col-lg-auto">
    <div class="row">
      <div class="col-6 col-lg-12">Content</div>
      <div class="col-6 col-lg-12">Content</div>
      <div class="col-6 col-lg-12">Content</div>
      <div class="col-6 col-lg-12">Content</div>
      <div class="col-6 col-lg-12">Content</div>
      <div class="col-6 col-lg-12">Content</div>
    </div>
  </div>
</div>

If the row inside col-lg-auto only has one column child, then it works as expected. I'm also able to achieve the layout I want if i replace the class col-lg-auto with the classes col-lg and flex-grow-0.

Can someone please explain why the width of col-lg-auto does become larger than the content width in this scenario? Can i not use a row with multiple columns inside a column that has the class col-auto?

like image 297
andbjer Avatar asked Oct 03 '18 10:10

andbjer


People also ask

What will happen if more than 12 columns are placed within a single row while designing a Bootstrap grid layout?

If more than 12 columns are placed within a single row, each group of extra columns will, as one unit, wrap onto a new line.

How many columns does Bootstrap's responsive grid consist of?

Bootstrap's grid system allows up to 12 columns across the page.

What does COL auto do?

Use col-auto . It will automatically take column size as per content size. It will adjust width as per content in all devices.

Is nested columns possible with Bootstrap grid system?

You can nest columns once you have a container, row, and column set up. To do this, add a new row <div> within the parent column's code, then add your nested columns. It will function as if the area inside the new row is its own grid system.


1 Answers

I don't think it's a "bug". The columns inside the col-lg-auto are full width col-lg-12 so they're filling 100% width as expected. If you want the inner columns to have consume min width, use flex-lg-column (flex-direction:column) on the inner row...

<div class="container-fluid">
    <div class="row">
        <div class="col">
            <div class="row">
                <div class="col-12 col-sm-6 col-lg-4">Col</div>
                <div class="col-12 col-sm-6 col-lg-4">Col</div>
                <div class="col-12 col-sm-6 col-lg-4">Col</div>
                <div class="col-12 col-sm-6 col-lg-4">Col</div>
                <div class="col-12 col-sm-6 col-lg-4">Col</div>
                <div class="col-12 col-sm-6 col-lg-4">Col</div>
                <div class="col-12 col-sm-6 col-lg-4">Col</div>
                <div class="col-12 col-sm-6 col-lg-4">Col</div>
                <div class="col-12 col-sm-6 col-lg-4">Col</div>
            </div>
        </div>
        <!-- Replace col-lg-auto with col-lg and add class flex-grow-0 -->
        <div class="col-12 col-lg-auto">
            <div class="row flex-lg-column">
                <div class="col-6 col-lg-12 bg-blue">Content</div>
                <div class="col-6 col-lg-12 bg-blue">Content</div>
                <div class="col-6 col-lg-12 bg-blue">Content</div>
                <div class="col-6 col-lg-12 bg-blue">Content</div>
                <div class="col-6 col-lg-12 bg-blue">Content</div>
                <div class="col-6 col-lg-12 bg-blue">Content</div>
            </div>
        </div>
    </div>
</div>

Demo: https://www.codeply.com/go/l5kv3hu9Av

Note: For IE 11 you'll need to add:

/* IE 11 helper */
.flex-lg-column > .col-lg-12 {
    flex: 1 1 auto;
}
like image 128
Zim Avatar answered Oct 28 '22 12:10

Zim