Using calc

suggest change

The 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>

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents