-2

I'm trying to make a layout like on the attached picture, so the column on the left span over multiple rows . At the moment my code looks like:

<div class="row my-row">
    <div class="col-6 my-col">1</div>
    <div class="col-6 my-col">
        <div class="row my-row">
            <div class="col-lg-12 my-col">2</div>
            <div class="col-lg-12 my-col">3</div>
        </div>
    </div>
</div>

Is it any other/better way to do it? As well I don't really understand how this code does what it does, so would be nice if someone can explain? Thanks.

Paulie_D
  • 107,962
  • 13
  • 142
  • 161
  • If you're open to alternatives to Bootstrap layout altogether, I'd recommend you take a look at [CSS Grid Layout](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout). – IronFlare Jul 10 '19 at 23:08

3 Answers3

0

You're doing it correctly for the most part. Bootstrap applies CSS styles to components on your page based on the class name of the component. In order to get items aligned hoizontally across the page, bootstrap uses the "row" class to create a vertical row. Inside of the "row" element, bootstrap sizes elements that are tagged with "col-#" based on the weight of the # following col. col-12 is the maximum width of the container, therefore you can create up to 12 columns (12 col-1's). In your example col-6 is a column that takes up half the page. In order to split the right half of that column further, an additional row is applied, and two more columns are created. These two columns should be col-6 in order to get each column to take exactly half of that row.

After these components you just need one more row and to get 1/3 of a row you will need three col-4 elements to create the last row. (4+4+4 = col-12 = full row)

Furthermore, Bootstrap not has the ability to automatically detect the size of a column (if you want them all equal). To take advantage of this, you don't need to put any number after col, just provide an element with a class of col inside a row and bootstrap will auto-size them to be equally spaced columns.

Bootstrap has extensive documentation for more help!

R10t--
  • 803
  • 7
  • 16
0

id use something like this

<div class="row">
    <div class="col-4">
        1
    </div>
    <div class="col-8">
        <div class="row">
            <div class="col-6">
                2
            </div>
            <div class="col-6">
                3   
            </div>
            <div class="col-6">
                4
            </div>
            <div class="col-6">
                5
            </div>

        </div>
    </div>
    <div class="col-4">
        6
    </div>
    <div class="col-4">
        7
    </div>
    <div class="col-4">
        8
    </div>
</div>

The trick is to use rows withing another column that's already there. This will let you have 2-> 5 within what looks like a column from 1.

6->8 can just be 3 col-4's in one row / the same row

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
nzimpossible
  • 318
  • 1
  • 9
0

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
  <section>
    <div class="container">
      <div class="row">
        <div class="col-12">
          <div class="row">
            <div class="col-md-4 col-lg-4 col-12 col-sm-6 d-flex align-items-center">
              1
            </div>
            <div class="col-md-8 col-lg-8 col-12 col-sm-6">
              <div class="row">
                <div class="col-md-6 col-sm-6 col-12 col-lg-6 d-flex align-items-center">
                  2
                </div>
                <div class="col-md-6 col-sm-6 col-12 col-lg-6 d-flex align-items-center">
                  3
                </div>
              </div>
              <div class="row">
                <div class="col-md-6 col-sm-6 col-12 col-lg-6 d-flex align-items-center">
                  4
                </div>
                <div class="col-md-6 col-sm-6 col-12 col-lg-6 d-flex align-items-center">
                  5
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-md-4 col-lg-4 col-sm-4 col-12 d-flex align-items-center">
          6
        </div>
        <div class="col-md-4 col-lg-4 col-sm-4 col-12 d-flex align-items-center">
          7
        </div> 
        <div class="col-md-4 col-lg-4 col-sm-4 col-12 d-flex align-items-center">
          8
        </div> 
      </div>
    </div>
  </section>
</body>
</html>

Try this out! It'll also vertically align your contents as you've showed on the image. For reference go through https://getbootstrap.com/docs/4.0/utilities/flex/ this link. Also if you want it to be centered horizontally use .justify-content-center

nazifa rashid
  • 1,469
  • 1
  • 9
  • 20