Is there a way of making a child in a grid layout stretch full width with CSS

Viewed 1843

I'm trying to create the following grid layout which will reveal a child on click.

So,

[ link1 ] [ link2 ] [ link3 ]

[ link4 ] [ link4 ] [ link4 ]

Will become,

[ link1 ] [ link2 ] [ link3 ]

[---------link2 content---------]

[ link4 ] [ link4 ] [ link4 ]

Ie, clicking on link2 has revealed content relating to link2 in the grid beneath it.

I had attempted to do this semantically, by grouping the link and content together in a dl..

<div style="display: flex">

    <dl>
        <dt><a href="#accordion1">Link1</a></dt>
        <dd>Content 1</dd>
    </dl>

    <dl>
        <dt><a href="#accordion2">Link2</a></dt>
        <dd>Content 2</dd>
    </dl>

    <dl>
        <dt><a href="#accordion3">Link3</a></dt>
        <dd>Content 3</dd>
    </dl>

    ..etc

</div>

With this layout, am I right in thinking that i wont be able to make the corresponding 's full width because of the adjacent blocks ??

And if that is the case, do i need to break up the layout in to a series of div's (at the cost of making it less semantic) ?

Any help appreciated.

Many thanks in advance,

1 Answers

CSS Grid can do this but only with a a change in the HTML.

Here's one I have which uses hover but could be adapted to clicks very simply.

Essentially, the "description" is hidden until required and the forced to the lower row and the other elements adapt due to the dense value in the auto-flow.

.container {
  width: 80%;
  margin: auto;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-flow: row dense;
  grid-gap: 1em;
}

.item,
.desc {
  border: 1px solid grey;
  height: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 30px;
}

.item {
  background: lightblue;
  transition: background 0.25s ease;
}

.item:hover {
  background: rebeccapurple;
}

.item:hover+.desc {
  display: block;
}

.desc {
  background: palegoldenrod;
  display: none;
  grid-column: 1 / -1;
}

@media (min-width: 700px) {
  .container {
    grid-template-columns: repeat(4, 1fr);
  }
}
<div class="container">
  <div class="item">1</div>
  <div class="desc">Description 1</div>
  <div class="item">2</div>
  <div class="desc">Description 2</div>
  <div class="item">3</div>
  <div class="desc">Description 3</div>
  <div class="item">4</div>
  <div class="desc">Description 4</div>
  <div class="item">5</div>
  <div class="desc">Description 5</div>
  <div class="item">6</div>
  <div class="desc">Description 6</div>

</div>

Related