-
Notifications
You must be signed in to change notification settings - Fork 0
/
adv-position-z-index.html
219 lines (201 loc) · 6.13 KB
/
adv-position-z-index.html
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Position</title>
<style>
/* position: static; */
/* HTML elements are positioned static by default. */
body {
margin: 0;
font-family: Cambria;
height: 200vh;
margin: 0;
position: relative;
}
/* position: relative; */
/* setting position relative at container level */
/* provides position context for positioned children */
.parent1 {
/* margin-top: 2rem; */
padding: 0.25rem;
background-color: lightskyblue;
position: relative;
height: 30vh;
z-index: 5;
overflow: auto;
}
/* position: relative; */
/* enables top, left, right, bottom properties */
/* relative to elem's normal doc flow position */
.child1 {
padding: 0.25rem;
background-color: lightcoral;
/* position: relative; */
/* top: 5px; */
/* left: 5px; */
/* right: 5px; */
/* bottom: 5px; */
}
/* position: absolute; */
/* removes element from document flow */
/* An element with position: absolute; is positioned relative to the nearest */
/* positioned ancestor (instead of positioned relative to the viewport, like fixed).*/
/* if no positioned ancestors, then positions relative to document body */
.child2 {
padding: 0.25rem;
background-color: lightgreen;
position: absolute;
bottom: 2px;
right: 2px;
/* right: 5px; */
/* bottom: 5px; */
}
/* position: fixed; */
/* fixes the position relative to the viewport */
/* always stays in the same place, even when page is scrolled */
.child3 {
padding: 0.25rem;
background-color: peru;
color: white;
position: fixed;
/* top: 60px; */
right: 25px;
font-size: 0.5rem;
/* use z-index to keep element visible */
/* when scrolling over other elements */
/* however, limited by the stacking context of the parent */
z-index: 10;
}
/* position: sticky; */
/* An element with position: sticky; is positioned based on the user's scroll position. */
/* TIP: Great to use for headers of lists and tables */
/* NOTE: you must specify a value for one of the position properties */
/* NOTE: the parent container must have a height */
.child4 {
padding: 0.25rem;
background-color: orchid;
color: white;
position: sticky;
top: 5px;
}
/* z-index */
/* default: z-index: 0; */
/* stacking context: set by position:relative; of parent */
/* grid and flex also set their own stacking context */
.parent2 {
margin-top: 1rem;
padding: 0.25rem;
background-color: lightskyblue;
position: relative;
z-index: 6;
}
.child5 {
padding: 0.25rem;
background-color: lightcoral;
}
.child6 {
padding: 0.25rem;
background-color: lightgreen;
}
.child7 {
padding: 0.25rem;
background-color: peru;
}
.child8 {
padding: 0.25rem;
background-color: orchid;
color: white;
}
.fixed {
padding: 0.25rem;
background-color: purple;
position: fixed;
left: 30%;
right: 30%;
color: white;
z-index: 100;
text-align: center;
}
.header {
position: relative;
z-index: 1000;
height: 10vh;
color: white;
}
.nav-bar {
position: fixed;
top: 0;
/* setting left,right: 0; draws across fullscreen */
left: 0;
right: 0;
display: flex;
justify-content: space-between;
padding: 1rem;
background-color: #333;
}
.nav-list {
margin: 0;
padding: 0;
display: flex;
list-style-type: none;
}
.nav-list a {
text-decoration: none;
margin-left: 1rem;
color: white;
}
.nav-list a:hover {
color: #ccc;
}
</style>
</head>
<body>
<div class="header">
<nav class="nav-bar">
<span>JAWA</span>
<ul class="nav-list">
<li><a href="#">Help</a></li>
<li><a href="#">Docs</a></li>
<li><a class="btn" href="#">Settings</a></li>
</ul>
</nav>
</div>
<div class="fixed">Fixed, z-index above Containers</div>
<div class="parent1">
Parent
<div class="child1">1</div>
<div class="child2">2- absolute wrt container</div>
<div class="child3">
3- fixed, z-index: parent1 context <br />
<br />
note: document flow positions this fixed element *below* fixed elements defined above this element
</div>
<div class="child4">4- sticky</div>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Doloremque libero tenetur aliquam cumque adipisci
impedit natus fugiat veniam adipisci aliquam amet reprehenderit quas ullam, pariatur exercitationem porro
perferendis, eos ducimus totam ipsam?
</p>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Doloremque libero tenetur aliquam cumque adipisci
impedit natus fugiat veniam adipisci aliquam amet reprehenderit quas ullam, pariatur exercitationem porro
perferendis, eos ducimus totam ipsam?
</p>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Doloremque libero tenetur aliquam cumque adipisci
impedit natus fugiat veniam adipisci aliquam amet reprehenderit quas ullam, pariatur exercitationem porro
perferendis, eos ducimus totam ipsam?
</p>
</div>
<div class="parent2">
Parent
<div class="child5">5</div>
<div class="child6">6</div>
<div class="child7">7</div>
<div class="child8">8</div>
</div>
</body>
</html>