First an explanation, then some code.
Vertical centering is a classic css issue. In this case, the vh block is very popular. Combined with stock (and possibly calc) its now a solvable thing.
Horizontal centering is quite simple, and you get it. Just set the width and set margin: 0 auto , and you're good to go.
With vertical centering, the key thing to remember is that you center your element, so half is above the middle, half is under the middle. With this we can do margin: calc(50vh-40px) auto 0 for your element 80px high and presto, in the middle vertically.
One more step:
Like horizontal centering, you seem to already have dynamic width using % . For dynamic vertical size, we can again refer to vh . It's nice that this saves us from the css calc function. Just subtract half the height from the 50vh field and you will get your stock. So for height: 20vh edge is margin: 40vh auto 0
Here is jsfiddle
and here is some code:
CSS
*{ margin: 0; padding: 0; } html, body{ width: 100vw; height: 100vh; } .nav-colors{ width: 80%; height: 20vh; margin: calc(40vh) auto 0; } .nav-colors div{ width: 18%; margin: 0 0 0 1%; height: 100%; display: inline-block; } .home-link{background-color:#00c08b;} .portfolio-link{background-color:#ea5080;} .social-link{background-color:#53bfe2;} .contact-link{background-color:#f8c54d;} .blog-link{background-color:#df6dc2;}
HTML:
<div class="nav-colors"> <div class="home-link"></div> <div class="portfolio-link"></div> <div class="social-link"></div> <div class="contact-link"></div> <div class="blog-link"></div> </div>
to use.
source share