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?
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.
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.
From the customiser, go to Header -> General design settings. you can choose Logo/title centered from the three options. save and publish.
#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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With