Overlapping Grid Items for Layered UI Elements
Owner: SnippetBot
Created: 2026-10-22 00:00:32
Size: 1.55 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
.grid-container {
display: grid;
/* Define 3 columns and 2 rows, each 1fr wide/tall */
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(2, 1fr);
width: 500px;
height: 300px;
background-color: #f0f8ff; /* Alice Blue */
border: 2px dashed #a0d9ff;
position: relative; /* Essential for z-index to work relative to container */
font-family: sans-serif;
font-size: 1.2em;
font-weight: bold;
color: white;
}
.grid-item {
display: flex;
justify-content: center;
align-items: center;
padding: 10px;
border: 1px solid rgba(255,255,255,0.5);
border-radius: 5px;
}
/* Item A: Spans columns 1 to 3 (covers first two columns) and row 1 */
.item-a {
grid-column: 1 / 3;
grid-row: 1 / 2;
background-color: rgba(255, 99, 71, 0.8); /* Tomato */
z-index: 1; /* Bring to front relative to other items in the same grid area */
}
/* Item B: Spans columns 2 to 4 (covers last two columns) and rows 1 to 3 (covers both rows) */
.item-b {
grid-column: 2 / 4;
grid-row: 1 / 3;
background-color: rgba(60, 179, 113, 0.8); /* Medium Sea Green */
z-index: 2; /* Bring even further to front */
}
/* Item C: Standard placement, but will be partially hidden by Item B due to z-index */
.item-c {
grid-column: 1 / 2;
grid-row: 2 / 3;
background-color: rgba(70, 130, 180, 0.8); /* Steel Blue */
z-index: 0;
}
/* Example HTML structure:
<div class="grid-container">
<div class="grid-item item-a">Layer 1 (Item A)</div>
<div class="grid-item item-b">Layer 2 (Item B)</div>
<div class="grid-item item-c">Layer 3 (Item C)</div>
</div>
*/