0

With CSS grid, how can I style three divs to wrap at the same time when the screen gets smaller ?

That means to go from this:

+---------+--+---------+--+---------+
|   div   |  |   div   |  |   div   |
+---------+--+---------+--+---------+

to this:

+-----+
| div |
+-----+
|     |
+-----+
| div |
+-----+
|     |
+-----+
| div |
+-----+

without first going through this intermediary step:

+---------+--+---------+
|   div   |  |   div   |
+---------+--+---------+
|         |  |         |
+---------+--+---------+
|   div   |  |         |
+---------+--+---------+

With that starter code:

.wrapper {
  display: grid;
  grid-gap: 10px;
}
<div class="wrapper">
  <div class="box">box 1</div>
  <div class="box">box 2</div>
  <div class="box">box 3</div>
</div>

https://jsfiddle.net/jLz0aqmj/1/

Nicolas Marshall
  • 4,186
  • 9
  • 36
  • 54

2 Answers2

1

Use a media query to switch the number of columns from three to one.

revised jsFiddle demo

.wrapper {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-gap: 10px;
}

@media ( max-width: 500px ) {
  .wrapper {
    grid-template-columns: 1fr;
  }
}

.box {
  background-color: lightgreen;
}
<div class="wrapper">
  <div class="box">box 1</div>
  <div class="box">box 2</div>
  <div class="box">box 3</div>
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
0

Hello Check below fiddle

https://jsfiddle.net/jLz0aqmj/8/

    <div class="wrapper">
  <div class="box">box 1</div>
  <div class="box">box 2</div>
  <div class="box">box 3</div>
</div>

and css

.wrapper{max-width:600px; font-size:0;}
.box{display:inline-block; max-width:200px; width:33%; font-size:13px; background:#ccc;}
@media screen and (max-width: 600px) {.box{display:block; max-width:100%}}