CSS grid is located on a page with a max-width:
body {
outline: 1px solid black;
}
.max-width {
max-width: 400px;
margin: 0 auto;
}
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
}
.first-grid-item {
grid-column: 1 / -1;
background-color: #ccc;
}
.second-grid-item {
grid-column: 1 / -1;
background-color: tomato;
}
@media (min-width: 576px) {
.max-width {
max-width: 500px;
}
.first-grid-item {
grid-column: 1 / 9;
}
.second-grid-item {
grid-column: 9 / 13;
}
}
<div class="max-width">
<div class="grid">
<div class="first-grid-item">First</div>
<div class="second-grid-item">Second</div>
</div>
</div>
On smaller screens, the layout changes and the second grid item is placed below the first one:
Now, a new requirement comes in to add a full-width background color to the second item on the smaller screens so it looks like this:
How would you achieve this?
Notes:
- The grid can appear in the middle of a page somewhere, it's not necessarily the only element on the page. Example: here
- Grid items can have variable height which isn't known in advance. Example: here
- When the screen size is:
- below 400px - the grid should occupy the full screen size.
- between 400px and 575px (inclusive) - the grid should occupy exactly
400px(which is themax-widthon smaller screens). - at least 576px - the grid should occupy exactly
500px(which is themax-widthon larger screens).
- You are allowed to change the HTML if you think there is a more natural way to implement this. Just make sure that elements don't move from where they need to be.


