Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for vertically centering logo and menu items in header [closed]

Tags:

css

I constantly find myself having to vertically center both the logo and main menu in a full-width header. Is there a widely accepted method to handle this?

enter image description here

like image 821
drake035 Avatar asked May 16 '16 11:05

drake035


People also ask

How do you center a vertical header?

Set the position for the parent to "relative", and for the child, set it to "absolute". Set the top, bottom, left, and right properties for the child. Set the margin to "auto" to make all margins equal and make the child <div> to be centered vertically as well as horizontally.

How do you center a vertical label?

Select the text that you want to center. in the Page Setup group, and then click the Layout tab. In the Vertical alignment box, click Center.

How do you center a logo in a header?

From the customiser, go to Header -> General design settings. you can choose Logo/title centered from the three options. save and publish.


2 Answers

#header {
  box-sizing: border-box;
  background: #ffc301;
  padding: 10px 15px;
  display: table;
  width: 100%;
  height: 70px;
}

.logo {
    background: #000;
    text-align: center;
    width: 70px;
    color: #fff;
}

.logo,
.menu {
  vertical-align: middle;
  display: table-cell;
}

.menu ul {
  text-align: right;
}

.menu ul li {
  display: inline-block;
  vertical-align: top;
}
<header id="header">
  <div class="logo">Logo</div>
  <nav class="menu">
    <ul>
      <li><a href="#">Item 1</a></li>
      <li><a href="#">Item 2</a></li>
      <li><a href="#">Item 3</a></li>
      <li><a href="#">Item 4</a></li>
    </ul>
  </nav>
</header>
like image 183
Mohammad Usman Avatar answered Dec 11 '22 07:12

Mohammad Usman


You can use flexbox to get it with justify-content: center; and align-items: center; properties.

#header {
  box-sizing: border-box;
  background: grey;
  padding: 10px 15px;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 70px;
}

.logo {
    background: black;
    text-align: center;
    width: 70px;
    padding: 15px;
    color: #fff;
}

.menu{
  flex: 1;
  text-align: right;
}

.menu ul li{
  display: inline-block;
  text-decoration: none;
}

a{
  text-decoration: none;
}
<div id="header">
  <div class="logo">logo</div>
  <nav class="menu">
    <ul>
      <li><a href="#">item#1</a></li>
      <li><a href="#">item#2</a></li>
      <li><a href="#">item#3</a></li>
    </ul>
  </nav>
</div>
like image 38
Francisco Romero Avatar answered Dec 11 '22 06:12

Francisco Romero