Table of Contents
Creating a responsive header for your website is essential for a good user experience. Using CSS Flexbox makes it easy to design a header that adapts to different screen sizes. In this tutorial, we’ll build a header that includes a logo, navigation menu, and a search bar, all arranged using Flexbox.
Setting Up the HTML Structure
Start by creating a container for your header. Inside, add three main sections: one for the logo, one for the navigation menu, and one for the search bar.
Here’s a simple HTML structure:
<header class="site-header">
<div class="logo">Logo</div>
<nav class="navigation">Navigation Menu</nav>
<div class="search">Search Bar</div>
</header>
Applying Flexbox Styles
Use CSS Flexbox to arrange these sections horizontally and ensure they are responsive. Assign the header a display of flex and distribute space accordingly.
Example CSS:
.site-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 20px;
background-color: #f8f9fa;
}
And style the inner elements for better appearance:
.logo { font-size: 1.5em; font-weight: bold; }
.navigation { display: flex; gap: 15px; }
.search { }
Making the Header Responsive
To ensure the header adapts to smaller screens, add media queries. For example, stack the navigation and search vertically on mobile devices.
Example CSS for responsiveness:
@media (max-width: 768px) {
.site-header { flex-direction: column; align-items: flex-start; }
.navigation { flex-direction: column; width: 100%; }
.search { width: 100%; margin-top: 10px; }
}
Conclusion
Using Flexbox, you can create a clean, responsive header that includes a logo, navigation, and search. Customize the styles further to match your website’s design, and test on different devices to ensure responsiveness.