Centering a logo in a navigation bar is a popular design pattern, especially on modern, minimal websites. Whether you’re building a landing page or a full site header, placing the logo image at the center of the nav bar can add visual balance and brand focus.
In this blog post, we’ll explore how to center a logo in a navbar using clean HTML and CSS, with multiple layout approaches based on your needs.
✅ Basic HTML Structure
Start with a basic navigation bar structure:
<nav class="navbar">
<ul class="nav-left">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
</ul>
<div class="logo">
<img src="logo.png" alt="Site Logo">
</div>
<ul class="nav-right">
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
This layout places the logo between two ul
sections: left and right navigation links.
🎯 CSS Method 1: Flexbox for Perfect Centering
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 20px;
height: 80px;
background-color: #fff;
position: relative;
}
.navbar ul {
list-style: none;
display: flex;
gap: 20px;
margin: 0;
padding: 0;
}
.navbar .logo {
position: absolute;
left: 50%;
transform: translateX(-50%);
}
.navbar .logo img {
height: 50px;
}
Explanation:
display: flex
spaces items horizontally.- Left and right
ul
s are aligned normally. - The
.logo
is taken out of the normal flow usingposition: absolute
and centered withleft: 50%
andtransform: translateX(-50%)
.
Benefit:
This ensures the logo is perfectly centered regardless of content on either side.
✅ CSS Method 2: Center Using Flex justify-content: center
If you want a simple nav with only a centered logo (no side links):
<nav class="navbar">
<div class="logo">
<img src="logo.png" alt="Site Logo">
</div>
</nav>
.navbar {
display: flex;
justify-content: center;
align-items: center;
height: 80px;
background-color: #f9f9f9;
}
.logo img {
height: 60px;
}
Use case:
- Landing pages or single-brand experiences.
- Nav bars without side menus.
✅ CSS Method 3: CSS Grid for More Control
For full layout control using CSS Grid:
.navbar {
display: grid;
grid-template-columns: 1fr auto 1fr;
align-items: center;
padding: 0 20px;
height: 80px;
background: #ffffff;
}
.navbar .nav-left,
.navbar .nav-right {
display: flex;
gap: 20px;
}
.logo img {
height: 50px;
}
Why use Grid?
- The center column (
auto
) ensures the logo stays in the center. - Left and right sections grow to fill space equally (
1fr
).
👇 Common Pitfalls to Avoid
- Don’t rely on
text-align: center
alone — it won’t center block-level elements like<div>
or<img>
inside flex/grid layouts. - Avoid absolute positioning without a
relative
parent — it breaks layout consistency. - Check responsiveness — ensure logo centering works across screen sizes.
📱 Making It Responsive
Wrap your layout in a media query or use flex-wrap
or grid-template-areas
to adjust the position of the logo on smaller screens.
Example:
@media (max-width: 768px) {
.navbar {
flex-direction: column;
justify-content: center;
}
.nav-left, .nav-right {
display: none; /* Or stack vertically */
}
.logo {
position: static;
transform: none;
}
}
✅ Summary
To center a logo in a navigation bar using HTML and CSS:
Method | Technique | Best For |
---|---|---|
Flexbox | position: absolute with transform | Balanced nav links left and right |
Flex Center | justify-content: center | Logo-only navbars |
CSS Grid | grid-template-columns: 1fr auto 1fr | Advanced layouts with equal spacing |