Overlay CSS Grid Elements
The grid system provides the ability to overlap elements when they intersect grid areas or when negative external margins are specified. Overlapping can occur by default, but it can also be specified in a specific order for each element using the z-index and order properties, as well as their combination.
Default Grid Element Overlap
Let's say we have a table in which elements overlap each other:
<div id="parent">
<div id="elem1">1</div>
<div id="elem2">2</div>
<div id="elem3">3</div>
</div>
#parent {
display: grid;
grid: repeat(6, 1fr) / repeat(6, 1fr);
border: 2px solid #696989;
padding: 10px;
width: 400px;
height: 300px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
#elem1 {
background-color: pink;
grid-column: 1 / 4;
grid-row: 1 / 5;
}
#elem2 {
background-color: orange;
grid-column: 2 / 5;
grid-row: 3 / 6;
}
#elem3 {
background-color: green;
grid-column: 3 / 6;
grid-row: 2 / 4;
}
:
Changing the stacking order of elements using the order property
Now let's apply the order property by setting it on each child element:
<div id="parent">
<div id="elem1">1</div>
<div id="elem2">2</div>
<div id="elem3">3</div>
</div>
#parent {
display: grid;
grid: repeat(6, 1fr) / repeat(6, 1fr);
border: 2px solid #696989;
padding: 10px;
width: 400px;
height: 300px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
#elem1 {
background-color: pink;
grid-column: 1 / 4;
grid-row: 1 / 5;
order: 3;
}
#elem2 {
background-color: orange;
grid-column: 2 / 5;
grid-row: 3 / 6;
order: 2;
}
#elem3 {
background-color: green;
grid-column: 3 / 6;
grid-row: 2 / 4;
order: 1;
}
:
Stacking order of elements using the z-index property
Now let's apply the z-index property, which allows you to customize the order of placement of elements along the z-axis:
<div id="parent">
<div id="elem1">1</div>
<div id="elem2">2</div>
<div id="elem3">3</div>
</div>
#parent {
display: grid;
grid: repeat(6, 1fr) / repeat(6, 1fr);
border: 2px solid #696989;
padding: 10px;
width: 400px;
height: 300px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
#elem1 {
background-color: pink;
grid-column: 1 / 4;
grid-row: 1 / 5;
z-index: 1;
}
#elem2 {
background-color: orange;
grid-column: 2 / 5;
grid-row: 3 / 6;
z-index: 3;
}
#elem3 {
background-color: green;
grid-column: 3 / 6;
grid-row: 2 / 4;
z-index: 2;
}
:
As you can see from the obtained result, the highest value of the z-index property sets the element to be positioned above other elements with lower values.
Combining element stacking using the order and z-index properties
If we change the order of elements using z-index and order, then z-index will still take precedence. This allows us to change the order of elements without losing control over their overlap:
<div id="parent">
<div id="elem1">1</div>
<div id="elem2">2</div>
<div id="elem3">3</div>
</div>
#parent {
display: grid;
grid: repeat(6, 1fr) / repeat(6, 1fr);
border: 2px solid #696989;
padding: 10px;
width: 400px;
height: 300px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
#elem1 {
background-color: pink;
grid-column: 1 / 4;
grid-row: 1 / 5;
order: 3;
z-index: 1;
}
#elem2 {
background-color: orange;
grid-column: 2 / 5;
grid-row: 3 / 6;
order: 1;
z-index: 2;
}
#elem3 {
background-color: green;
grid-column: 3 / 6;
grid-row: 2 / 4;
order: 1;
z-index: 3;
}
:
Practical tasks
Let's say we have four elements in our grid:
<div id="parent">
<div id="elem1">1</div>
<div id="elem2">2</div>
<div id="elem3">3</div>
<div id="elem4">4</div>
</div>
#parent {
display: grid;
grid: repeat(6, 1fr) / repeat(6, 1fr);
border: 2px solid #696989;
padding: 10px;
width: 400px;
height: 300px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
#elem1 {
background-color: pink;
}
#elem2 {
background-color: orange;
}
#elem3 {
background-color: green;
}
#elem4 {
background-color: lightblue;
}
Place all elements according to the following example using the order property:
Now, to solve the previous problem, apply the z-index property.
Let's say we have four elements in our grid:
<div id="parent">
<div id="elem1">1</div>
<div id="elem2">2</div>
<div id="elem3">3</div>
<div id="elem4">4</div>
</div>
#parent {
display: grid;
grid: repeat(6, 1fr) / repeat(6, 1fr);
border: 2px solid #696989;
padding: 10px;
width: 400px;
height: 300px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
#elem1 {
background-color: pink;
}
#elem2 {
background-color: orange;
}
#elem3 {
background-color: green;
}
#elem4 {
background-color: lightblue;
}
Place all elements according to the following example using the order property:
Now, to solve the previous problem, apply the z-index property.