1.container {
2 display: flex;
3 justify-content: center;
4 align-items: center;
5}
1.center {
2 position: fixed;
3 top: 50%;
4 left: 50%;
5 transform: translate(-50%, -50%);
6}
1<div class="container">
2 <div class="child"></div>
3</div>
4.container {
5 ...
6 display: flex;
7 justify-content: center;
8 align-items: center;
9}
1<!--center text-->
2<!--padding deals with margins within the element itself-->
3<!--margin deals with blank space surrounding and element-->
4<!DOCTYPE html>
5<html>
6<head>
7<style>
8.center {
9 margin: auto;
10 width: 80%;
11 border: 3px solid #73AD21;
12 padding: 10px;
13 margin: 20px;
14}
15</style>
16</head>
17<body>
18
19<h2>Center Align Elements</h2>
20<p>To horizontally center a block element (like div), use margin: auto;</p>
21
22<div class="center">
23 <p><b>Note: </b>Using margin:auto will not work in IE8, unless a !DOCTYPE is declared.</p>
24</div>
25
26</body>
27</html>
1<!DOCTYPE html>
2<html>
3<head>
4<style>
5.center {
6 margin: auto;
7 width: 80%;
8 border: 3px solid #73AD21;
9 padding: 10px; # deals with margins within the element itself
10 margin: 20px; # deals with area around outside of element
11}
12</style>
13</head>
14<body>
15
16<h2>Center Align Elements</h2>
17<p>To horizontally center a block element (like div), use margin: auto;</p>
18
19<div class="center">
20 <p><b>Note: </b>Using margin:auto will not work in IE8, unless a !DOCTYPE is declared.</p>
21</div>
22
23</body>
24</html>