skip to Main Content

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


  1. The Bootstrap grid system has five classes:

    .col- (extra small devices - screen width less than 576px)
    .col-sm- (small devices - screen width equal to or greater than 576px)
    .col-md- (medium devices - screen width equal to or greater than 768px)
    .col-lg- (large devices - screen width equal to or greater than 992px)
    .col-xl- (xlarge devices - screen width equal to or greater than 1200px)
    

    They automatically trigger with sizes change

    Boostrap Grid System

    Login or Signup to reply.
  2. Try putting instead of col-lg-4 with col-xs-12. This will make your “divs” with this class stay stacked.

    Hope this can help you.

    Login or Signup to reply.
  3. It’s because the Bootstrap row is display:flex, and the default flexbox direction is flex-direction:row. If you remove the .row you’ll see the inner divs stack as expected.

    <div class="container">
        <div class="border">
            <div class="border">One</div>
            <div class="border">Two</div>
            <div class="border">Three</div>
        </div>
    </div>
    

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


    Also note, the .row should only be used to contain grid columns (.col*).

    Login or Signup to reply.
  4. This is the relevant CSS code from bootstrap (I commented the irrelevant parts)

    .row {
        display: -ms-flexbox;
        display: flex;
        -ms-flex-wrap: wrap;
        flex-wrap: wrap;
        /*margin-right: -15px;*/
        /*margin-left: -15px;*/
    }
    
    /*.col, .col-1, .col-10, .col-11, .col-12, .col-2, .col-3, .col-4, .col-5, .col-6, .col-7, .col-8, .col-9, .col-auto, .col-lg, .col-lg-1, .col-lg-10, .col-lg-11, .col-lg-12, .col-lg-2, .col-lg-3, */
    .col-lg-4,
    /*.col-lg-5, .col-lg-6, .col-lg-7, .col-lg-8, .col-lg-9, .col-lg-auto, .col-md, .col-md-1, .col-md-10, .col-md-11, .col-md-12, .col-md-2, .col-md-3, .col-md-4, .col-md-5, .col-md-6, .col-md-7, .col-md-8, .col-md-9, .col-md-auto, .col-sm, .col-sm-1, .col-sm-10, .col-sm-11, .col-sm-12, .col-sm-2, .col-sm-3, .col-sm-4, .col-sm-5, .col-sm-6, .col-sm-7, .col-sm-8, .col-sm-9, .col-sm-auto, .col-xl, .col-xl-1, .col-xl-10, .col-xl-11, .col-xl-12, .col-xl-2, .col-xl-3, .col-xl-4, .col-xl-5, .col-xl-6, .col-xl-7, .col-xl-8, .col-xl-9, .col-xl-auto */ {
        /*position: relative;*/
        width: 100%;
        /*min-height: 1px;*/
        /*padding-right: 15px;*/
        /*padding-left: 15px;*/
    }
    
    @media (min-width: 992px)
    .col-lg-4 {
        -ms-flex: 0 0 33.333333%;
        flex: 0 0 33.333333%;
        max-width: 33.333333%;
    }
    

    You see:
    .row has display: flex. Every direct child element of .row is a flex item with the default value for flex-basis of auto. A auto 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 a flex-basis of 33.333333% (one third).

    If you remove the col-lg-4 class, those elements are still flex items with no flex-basis set hence having implicit flex-basis: auto

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search