Why are only some of my CSS Grid boxes expanding when I hover over them?
Here is a hack to get around the jittery hovering - using display: contents
to group the rows and nth-child(3n+m)
to target the columns.
Finally to show only the hovered item in a row, display
property is fiddled with - see more explanations inline in the demo below:
.container {
display: grid;
grid-gap: 10px;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
}
.row {
display: contents;
}
[class^="item"] { /* fix first column */
grid-column: 1;
}
[class^="item"]:nth-child(3n+2) { /* fix second column */
grid-column: 2;
}
[class^="item"]:nth-child(3n+3) { /* fix third column*/
grid-column: 3;
}
[class^="item"]:hover { /* span 3 columns on hover of any cell */
grid-column: 1 / 4;
display: block !important; /* Show hovered item */
}
.row:nth-child(1):hover div { /* fix row on hover first row */
grid-row: 1;
display: none;
}
.row:nth-child(2):hover div { /* fix row on hover second row */
grid-row: 2;
display: none;
}
.row:nth-child(3):hover div { /* fix row on hover third row */
grid-row: 3;
display: none;
}
/* Styling */
.container>.row>div {
border: 2px solid #0580d5;
background-color: rgba(40, 180, 240, .3);
border-radius: 5px;
}
[class^="item"] {
text-align: center;
box-sizing: border-box;
font-size: 5em;
color: #0580d5;
transition: .2s;
}
<div class="container">
<div class="row">
<div class="item-1">1</div>
<div class="item-2">2</div>
<div class="item-3">3</div>
</div>
<div class="row">
<div class="item-4">4</div>
<div class="item-5">5</div>
<div class="item-6">6</div>
</div>
<div class="row">
<div class="item-7">7</div>
<div class="item-8">8</div>
<div class="item-9">9</div>
</div>
</div>
Here is an idea where you can rely on negative margin to create the overlap effect whithout changing the structure and using display:contents
.container {
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(3, minmax(100px, auto));
grid-template-rows: repeat(3, minmax(100px, auto));
}
[class^="item"] {
text-align: center;
box-sizing: border-box;
padding: 10%;
font-size: 5em;
color: #0580d5;
transition: .2s;
}
[class^="item"]:hover {
z-index:2; /*we increase the z-index to cover the other*/
background:
/*we this to keep the initial background*/
linear-gradient(rgba(40, 180, 240, .3),rgba(40, 180, 240, .3)),
#fff;
}
[class^="item"]:nth-child(3n + 1):hover {
margin-right:calc(-200% - 20px); /* we remove (2 x grid items + 2 x gap) */
}
[class^="item"]:nth-child(3n + 2):hover {
margin-left:calc(-100% - 10px);
margin-right:calc(-100% - 10px);
}
[class^="item"]:nth-child(3n + 3):hover {
margin-left:calc(-200% - 20px);
}
.container>div {
border: 2px solid #0580d5;
background-color: rgba(40, 180, 240, .3);
border-radius: 5px;
}
@media only screen and (min-width: 789px) {
.container {
grid-template-columns: repeat(3, 1fr);
}
}
<div class="container">
<div class="item-1">1</div>
<div class="item-2">2</div>
<div class="item-3">3</div>
<div class="item-4">4</div>
<div class="item-5">5</div>
<div class="item-6">6</div>
<div class="item-7">7</div>
<div class="item-8">8</div>
<div class="item-9">9</div>
</div>