Using calc
suggest changeThe calc() function is the part of a new syntax in CSS3 in 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).
CSS
.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*/
}
HTML
<div class="center"></div>
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents