CSS Grid columns starts on the same row

Viewed 30

I'm trying to make each column of my css grid facing each other : making it look like that:

+---+---+
| A | D |
+---+---+
| B | E |
+---+---+
| C | F |
+---+---+

Instead of :

+---+---+
| A |   |
+---+---+
| B |   |
+---+---+
| C | D |
+---+---+
|   | E |
+---+---+
|   | F |
+---+---+

I've tried to play with the grid-row and I'd like to avoid placing them with an id

.container{
  display: grid;
  grid-template-columns: auto auto;
  background: yellow
}

.left {
  grid-column: 1;
  background:green;
}

.right{
  grid-column : 2;
  background: olive
}
<div class="container">
  <div class="left">A</div>
   <div class="left">B</div>
   <div class="left">C</div>
  
  <div class="right">D</div>
  <div class="right">E</div>
  <div class="right">F</div>
</div>

2 Answers

Just tell the container to flow in columns.

grid-auto-flow:column;

.container {
  display: grid;
  grid-template-columns: auto auto;
  background: yellow;
  grid-auto-flow: column;
}

.left {
  grid-column: 1;
  background: green;
}

.right {
  grid-column: 2;
  background: olive
}
<div class="container">
  <div class="left">A</div>
  <div class="left">B</div>
  <div class="left">C</div>

  <div class="right">D</div>
  <div class="right">E</div>
  <div class="right">F</div>
</div>

If you can amend your HTML, you can try this instead to put parent wrappers around left and right.

.container {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.left div { 
  background: yellow;
  border: 1px solid black;
}
.right div { 
  background: lime;
  border: 1px solid black;
}
<div class="container">
      <div class="left">
        <div>1</div>
        <div>2</div>
        <div>3</div>
      </div>
      <div class="right">
        <div>4</div>
        <div>5</div>
        <div>6</div>
      </div>
</div>

Related