Centering - Using calc()
~10 mins
Using calc()
-
The calc() function is the part of a new syntax in
CSS3in which you can calculate (mathematically) what size/position
your element occupies by using a variety of values like pixels, percentages, etc. -
Note: Whenever you use this function, always take care of the space between two values calc(100% - 80px)
<div class="center"></div>
.center {
position: absolute;
height: 50px;
width: 50px;
background: red;
top: calc(50% - 50px / 2); /* height divided by 2*/
left: calc(50% - 50px / 2); /* width divided by 2*/
}