-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSS-position-element-center.html
79 lines (74 loc) · 1.77 KB
/
CSS-position-element-center.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Position an element center</title>
<style>
h1 {
color: chocolate;
text-align: center;
}
code {
color: rgb(17, 189, 17);
font-weight: bolder;
text-align: center;
display: block;
}
.parent {
width: 600px;
height: 600px;
background: red;
/*Centering element */
position: relative;
left: 0;
right: 0;
margin: 0 auto;
}
.child img {
/* display: inline-block; */
border: none;
height: 300px;
width: 300px;
/*Centering the element*/
position: absolute;
left: 50%;
margin-left: -150px; /*Negative Half width of the element*/
top: 50%;
margin-top: -150px; /*negative Half height of the element */
/*transform: translate(-50%, -50%); if width/height is unknown*/
}
</style>
</head>
<body>
<h1>Center element(s) using CSS Position</h1>
<div class="parent">
<div class="child">
<img src="img/birds.png" alt="" />
</div>
</div>
<code>
<h3>Method: one</h3>
position: relative; <br />
left: 0; <br />
right: 0; <br />
margin: 0 auto; <br />
*Outer Element
</code>
<hr />
<div class="parent">
<div class="child">
<img src="img/pencil.png" alt="" />
</div>
</div>
<code>
<h3>Method: Two</h3>
position: absolute; <br />
left: 50%; <br />
margin-left: -150px; <br />
top: 50%; <br />
margin-top: -150px; <br />
*Inner Element
</code>
</body>
</html>