1text-align: center;
2vertical-align: middle;
3line-height: 90px; /* The same as your div height */
4
1/* For horizontal align: */
2parent-element {text-align:center;}
3/* For horizontal and vertical align: */
4parent-element {position: relative;}
5element-to-be-centered {
6 position: absolute;
7 left: 50%;
8 top: 50%;
9 transform: translate(-50%, -50%);
10}
11
12/* See https://www.w3schools.com/css/css_align.asp for more info */
1.container{
2 margin: 0;
3 position: absolute;
4 top: 50%;
5 left: 50%;
6 -ms-transform: translate(-50%, -50%);
7 transform: translate(-50%, -50%);
8 width: /* Define the width here; */
9 height: /* Define the height here; */
10}
11
12/*You have to define the width and height! */
1<style>
2.container {
3 height: 200px;
4 position: relative;
5 border: 3px solid green;
6}
7
8.center {
9 margin: 0;
10 position: absolute;
11 top: 50%;
12 left: 50%;
13 -ms-transform: translate(-50%, -50%);
14 transform: translate(-50%, -50%);
15}
16</style>
17
18<div class="container">
19 <div class="center">
20 <p>I am vertically and horizontally centered.</p>
21 </div>
22</div>
1/* To center text, you need to use text-align. */
2
3.centerText {
4 text-align: center; /* This puts the text into the center of the
5 screen. */
6}
7
8/* There are also other things that you can use in text-align:
9left, right, and justify. Left and right make the test align to the
10right or left of the screen, while justify makes the text align both
11sides by spreading out spaces between some words and squishing others. */