How to Flex Grid 2 Columns using CSS

0 min read 174 words

If you would like to flex grid 2 columns in CSS then you need three (3) divs.

Step 1 – Set your HTML template

Create some HTML with this layout.

<div class="Parent">
  <div class="child1">
      <h1>Left</h1>
  </div>
  <div class="child2">
      <h1>RIGHT</h1>
  </div>
</div>

Step 2 – Container Div

Create a Parent div that uses the Flexbox display model.

.Parent {
  display: flex;
  flex-direction: row;
}

Step 3 – Child Divs

Create two (2) divs that are both 50% of the parent container.

Below we set the height to 100% of the viewport height.

.child1 {
  width: 50%;
  height: 100vh;
  background-color: green;
}

.child2 {
  width: 50%;
  height: 100vh;
  background-color: red;
}

Step 4 – Some gotchas to be aware of

You should also set the padding and margin to `` to make sure there is no overlap.

.body {
  padding: 0;
  margin: 0;
}

It is also good practice to set the border-box as follows:

box-sizing: border-box;

A good overall way to set these are as follows:

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags

Recent Posts