I’m a beginner in Bootstrap, just a question on the following code:
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row border">
<div class="col-lg-4 border">One</div>
<div class="col-lg-4 border">Two</div>
<div class="col-lg-4 border">Three</div>
</div>
</div>
if I size my browser window to small size which doesn’t qualify for the lg breakpoint, you can see that those three elements stack each other.
But if I remove the breakpoint , just like:
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row border">
<div class="border">One</div>
<div class="border">Two</div>
<div class="border">Three</div>
</div>
</div>
those three elements are like inline elements instead of stacking each other, why? since it is actually the same code with col-lg-4 as long as I stay in the small size of window, so the lg breakpoint hasn’t been trigger.
4
Answers
The Bootstrap grid system has five classes:
They automatically trigger with sizes change
Boostrap Grid System
Try putting instead of
col-lg-4
withcol-xs-12
. This will make your “divs” with this class stay stacked.Hope this can help you.
It’s because the Bootstrap row is
display:flex
, and the default flexbox direction isflex-direction:row
. If you remove the.row
you’ll see the inner divs stack as expected.Demo: https://www.codeply.com/go/H2y9xDXLv8
Also note, the
.row
should only be used to contain grid columns (.col*
).This is the relevant CSS code from bootstrap (I commented the irrelevant parts)
You see:
.row
hasdisplay: flex
. Every direct child element of.row
is a flex item with the default value forflex-basis
ofauto
. Aauto
flex item takes only as much space as it needs (if no width is given).col-lg-4
has always a width of 100%, but on large screens (>= 992px) it get’s aflex-basis
of33.333333%
(one third).If you remove the
col-lg-4
class, those elements are still flex items with noflex-basis
set hence having implicitflex-basis: auto