Suppose I have a 3x3 CSS grid, defined by:
.grid-container {
display: grid;
grid-template-rows: repeat(3, 100px);
grid-template-columns: repeat(3, 100px);
grid-gap: 10px;
}
.cell {
border: 1px solid black;
}
And I have the markup
<div class='grid-container'>
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
<div class="cell">4</div>
<div class="cell">5</div>
<div class="cell">6</div>
<div class="cell">7</div>
<div class="cell">8</div>
<div class="cell">9</div>
</div>
Which results in:
But now, I want to replace the second row (items 4, 5, 6), with a React component, which will be encapsulated in its own <div>
, which renders cells 4, 5, 6. Let's assume that React component gets the class name middle-component
.
However, I still want the items inside of middle-component
to align to the outer grid. How can I accomplish this?
For example, the markup would look like:
<div class='grid-container'>
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
<div class='middle-row'>
<div class="cell">4</div>
<div class="cell">5</div>
<div class="cell">6</div>
</div>
<div class="cell">7</div>
<div class="cell">8</div>
<div class="cell">9</div>
</div>
How can I make items 4, 5, 6 align to the outer grid? If I don't change any of the CSS, the result is this:
I can fix it by repeating some of the original CSS again in .middle-row
:
.middle-row {
grid-row: 2 / span 1;
grid-column: 1 / span 3;
display: grid;
grid-template-rows: repeat(3, 100px);
grid-template-columns: repeat(3, 100px);
grid-gap: 10px;
}
But now I've repeated the same CSS that I defined on .grid-container
.
Is there a more elegant way to align items within .middle-column
to the grid defined in .grid-container
?