-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabsolute.html
249 lines (216 loc) · 8.21 KB
/
absolute.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<!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>CSS Position | Absolute</title>
<style>
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body{
font-family: Arial, Helvetica, sans-serif;
height: 100%;
}
.container {
margin: auto;
max-width: 900px;
padding: 1rem;
}
a, a:visited {
text-decoration: none;
}
a.current-link {
background-color: navy;
}
header.hero {
background-color: royalblue;
position: sticky;
top: 0;
right: 0;
box-shadow: 0px 0px 10px #000;
font-size: 1.2rem;
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
text-transform: uppercase;
}
.hero .container {
display: flex;
justify-content: space-between;
align-items: center;
}
.hero h1 {
font-size: 1.2rem;
letter-spacing: 0.3rem;
padding: 0.5rem;
}
.hero ul {
list-style: none;
}
.hero ul li {
display: inline-block;
margin-right: 1rem;
}
.hero ul li a {
color: #fff;
padding: 0.5rem 0.75rem;
border-radius: 0.5rem;
}
.main > p{
margin: 1rem 0 0 0;
text-align: center;
font-size: 1.1rem;
}
.section {
padding: 1rem;
margin: 2rem auto;
max-width: 400px;
background-color: lightskyblue;
/* text-align: center; */
color: white;
max-width: 100%;
}
.section h2 {
font-size: 1.3rem;
background-color: darkgreen;
padding: 1rem;
}
.section p {
font-size: 1.1rem;
background-color: darkgreen;
padding: 1rem;
}
.section .box {
background-color: navy;
width: 200px;
}
section.position-relative {
position: relative;
top: 0;
}
section.position-absolute .absolute {
position: absolute;
z-index: -1;
height: 200px;
}
section.position-absolute .absolute-two {
position: absolute;
left: 2em;
top: 0;
bottom: 0;
background-color: gray;
}
section.position-relative .absolute-three {
background-color: #ff00009a;
height: 100px;
right: 25%;
position: absolute;
bottom: 0;
}
@media screen and (max-width: 800px) {
.hero .container {
flex-direction: column;
justify-content: center;
}
.hero h1 {
margin-bottom: 1rem;
}
}
@media screen and (max-width: 600px) {
.hero h1 {
margin-bottom: 0;
}
.hero ul {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.hero ul li {
padding: 0.25 0;
margin: 1.5rem 0 0 0;
font-size: 1rem;
}
}
</style>
</head>
<body>
<header class="hero">
<div class="container">
<h1>CSS Position</h1>
<ul class="navbar">
<li><a href="./index.html">static</a></li>
<li><a href="./relative.html">relative</a></li>
<li><a href="./absolute.html" class="current-link">absolute</a></li>
<li><a href="./fixed.html">fixed</a></li>
<li><a href="./sticky.html">sticky</a></li>
</ul>
</div>
</header>
<main class="main container">
<section class="section position-absolute">
<h2><code>position: absolute;</code></h2>
<div class="box absolute"></div>
<p>The above box has <strong><code>position: absolute;</code></strong></p>
</section>
<p>The <code>div.absolute</code> element is taken <em>out</em>of the normal flow of document.</p>
<p>Then the element is placed <em>in it's original position</em> unless values of <code>top, right, left, bottom</code> are specified.</p>
<p>In the above example, the box is placed <br/>
<strong style="display: inline-block; margin-top: 0.1rem; padding: 0.5rem 1rem; background-color:yellowgreen;">
<code>
div.absolute {
position: absolute;
z-index: -1;
}
</code>
</strong>
</p>
<p><strong>NOTE:</strong> See how the other elements ignore the blue box, and behave as if it is not present.</p>
</main>
<main class="main container">
<hr />
<section class="section position-absolute">
<p>Check out the gray box in the left. It has a class <em><code>absolute-two</code></em> and has <code>position: absolute;</code>.</p>
<p>The height for this element is not mentioned, and is calculated by values from top and bottom.</p>
<p>Notice how the gray box is placed relative to the document as there is no closest positioned ancestor.</p>
<div class="box absolute-two"></div>
</section>
<p>In this example, the box is placed <br/>
<strong style="display: inline-block; margin-top: 0.1rem; padding: 0.5rem 1rem; background-color:yellowgreen;">
<code>
div.absolute-two {
position: absolute;
left: 2em
right: 0%;
bottom: 0;
}
</code>
</strong>
</p>
</main>
<main class="main container">
<hr />
<section class="section position-absolute position-relative">
<p>Check out the red box within this box. It has a class <em><code>absolute-three</code></em> and has <code>position: absolute;</code>.</p>
<p>Notice how the red box is placed relative to the parent (section with skyblue color).</p>
<div class="box absolute-three"></div>
</section>
<p>In this example, the box is placed <br/>
<strong style="display: inline-block; margin-top: 0.1rem; padding: 0.5rem 1rem; background-color:yellowgreen;">
<code>
section.position-relative {
position: relative;
}<br/>
div.absolute-three {
position: absolute;
right: 25%;
bottom: 0;
}
</code>
</strong>
</p>
</main>
</body>
</html>