Using calc
suggest changeThe calc() function calculates a length from mixed units such as pixels and percentages. Keep a space on both sides of + and -: calc(100% - 80px). The offsets below are relative to the element's containing block.
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