Is it possible, using bootstrap 4 grid, to change the layout of my page for mobile in this specific way?

Viewed 70

I currently have the following bootstrap grid set-up like this

<div class="container">
  <div class="row">
    <div class="col-4">
      <div class="A">
        A
      </div>
    </div>
    <div class="col-8">
      <div class="B">
        B
      </div>
      <div class="C">
        C
      </div>
    </div>
  </div>  
</div>

And this produces something similar to this effect

enter image description here

I've been now trying to edit the bootstrap layout so that on a extra small screen the grid collapses to this

enter image description here

but I have been unsuccessful. Solutions I have tried are mainly around using the Order class but I can only switch the order between the two columns and not the 3 blocks individually. I have also tried splitting up the second column into separate rows but then Order seemed to only work within the columns. Is something like this possible within bootstrap grid or would I have to use flex classes?

3 Answers

See below. You need to see the page both in full and non-full size.

.row > div {
  display: grid;
}

.A {
  order: 2;
}

.B {
  order: 1;
}

.C {
  order: 3;
}

@media (min-width: 1024px) {
  .A {
    order: 1;
    grid-row-start: 1;
    grid-row-end: 3;
  }
  
  .B {
    order: 2;
    grid-column-start: 2;
    grid-column-end: 3;
    grid-row-start: 1;
    grid-row-end: 2;
  }
  
  .C {
    grid-column-start: 2;
    grid-column-end: 3;
    grid-row-start: 2;
    grid-row-end: 3;
}
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row">
    <div class="col-12">
      <div class="A">
        A
      </div>
      <div class="B">
        B
      </div>
      <div class="C">
        C
      </div>
    </div>
  </div>
</div>

custon class and display:contents could help for a few browsers:

example

@media screen and (max-width:569px) {
  .d-sm-contents {
    display: contents
  }
  .order-top {
    order: -1;
  }
}

[class^="col"]>div {
  border: solid;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row ">
    <div class="col-sm-4 ">
      <div class="A">
        A
      </div>
    </div>
    <div class="col-sm-8 d-sm-contents">
      <div class="B col order-top">
        B
      </div>
      <div class="C col ">
        C
      </div>
    </div>
  </div>
</div>

You can try this tecnique:

.A{ background-color:blue; height:100%}
.B{ background-color:red; }
.C{ background-color:yellow; }

div.row [class^="col"] {
    display:inline-block;
    padding:0px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" />

<div class="container">
    <div class="row">
        <div class="col-sm-4">
            <div class="A">A</div>
        </div>
        <div class="col-sm-8">
            <div class="B">B</div>
            <div class="C">C</div>
        </div>
    </div>
</div>

Related