0

I have just a simple html:

<body>
    <header id="cabecalho">
        <nav id="menu">
            <ul>
            <li>Home</li>
            <li>Projects</li>
            <li>Contact</li>
            </ul>
        </nav>
    </header>
</body>

CSS :

<style>
          *{
              padding: 0;
              margin: 0;    
          }
          html{
              width: 100%;
              height: 100%;
          }
          body{
              width: 100%;
              height: 100%;
          }
          header#cabecalho{
            width: 100%;
            height: 5%;
            background-color: rgb(60, 92, 86); 
            overflow: hidden;
          }
          nav#menu{
            display: block;
            height: 50%;
            width: 100%;
            overflow: hidden;
            margin-top: auto;
            margin-bottom: auto;
          }
          nav#menu ul{
            height: 100%;
            width: 100%;
            list-style: none;
          }
          nav#menu ul li{
              display:inline-block;
              color: white;  
          }


      </style>

I just want to vertical centralize the menu in the header, but it keeps on the top of it. obs: using margin: x Pixels works, but i don't think this is the best way to deal with that.

Pedro leal
  • 174
  • 2
  • 8

1 Answers1

0

There are multiple ways to tackle this.

I would target the nav and header and give it a position: relative;. Then I would give the menu a top: 50%; and transform: translate(-50%);

This moves the menu 50% down the header and pulls it back up 50% of its own height. Resulting in a centred menu.

If you want to centre the text inside the menu, I would suggest doing the same for the ul.

You can look at this for reference JS fiddle, but keep in mind that I have added a background to the menu and made the header larger for easy viewing.

Mats
  • 223
  • 1
  • 5
  • 15