3

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:

Image1

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:

Image2

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;
    }

Image3

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?

mark
  • 4,678
  • 7
  • 36
  • 46
  • possible duplicate of : https://stackoverflow.com/a/54535961/8620333 / https://stackoverflow.com/a/55407828/8620333 – Temani Afif Oct 19 '19 at 01:23

0 Answers0