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
?
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.
Bootstrap's grid system allows up to 12 columns across the page.
Use col-auto . It will automatically take column size as per content size. It will adjust width as per content in all devices.
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With