Mobile-First Navigation
Part of the Practical Frontend section of Coddy's HTML journey — lesson 8 of 35.
Mobile-first navigation means starting with a simple, touch-friendly menu for small screens, then enhancing it for larger screens.
Start with a basic mobile navigation structure: inside the <nav> element, include a button and a list.
<nav class="main-nav">
<button class="menu-toggle">Menu</button>
<ul class="nav-list">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>Apply mobile styling first. On small screens → show a “menu” button and hide the list.
.menu-toggle {
display: block;
//some more styles
}
.nav-list {
display: none;
//some more styles
}Then use media queries for larger screens. On larger screens → show the menu as a horizontal list at the top and hide a “menu” button.
@media (min-width: 768px) {
.menu-toggle {
display: none;
}
.nav-list {
display: flex;
}
}This approach ensures the navigation is optimized for mobile devices first, then enhanced for larger screens.
Challenge
EasyGiven an HTML file with a navigation containing a menu button and a list, currently the list is always visible.
Your task is to fix the mobile-first navigation style by:
- Making the navigation list hidden by default on mobile screens.
- Making the navigation list visible larger screens (
display: flex).
Cheat sheet
Mobile-first navigation starts with a simple, touch-friendly menu for small screens, then enhances it for larger screens.
Basic mobile navigation structure:
<nav class="main-nav">
<button class="menu-toggle">Menu</button>
<ul class="nav-list">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>Mobile styling (show button, hide list):
.menu-toggle {
display: block;
}
.nav-list {
display: none;
}Desktop styling with media queries (hide button, show horizontal list):
@media (min-width: 768px) {
.menu-toggle {
display: none;
}
.nav-list {
display: flex;
}
}Try it yourself
<!DOCTYPE html>
<html>
<head>
<title>Mobile-First Navigation</title>
<style>
:root {
--primary-color: #4a6de5;
--text-color: #333333;
}
.nav-container {
display: flex;
justify-content: space-between;
align-items: center;
}
.logo {
font-weight: bold;
color: var(--primary-color);
text-decoration: none;
font-size: 1.5rem;
}
.menu-toggle {
display: block;
background-color: var(--primary-color);
color: white;
border: none;
padding: 0.5rem 1rem;
cursor: pointer;
}
.nav-list {
list-style: none;
padding: 0;
margin: 1rem 0 0 0;
width: 100%;
}
.nav-list li a {
color: var(--text-color);
text-decoration: none;
display: block;
padding: 0.5rem 0;
}
@media (min-width: 768px) {
.menu-toggle {
display: none;
}
.nav-list {
margin: 0;
width: auto;
}
.nav-list li {
margin: 0 0 0 1.5rem;
}
}
</style>
</head>
<body>
<header class="site-header">
<nav class="nav-container">
<a href="#" class="logo">MySite</a>
<button class="menu-toggle">Menu</button>
<ul class="nav-list">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
</body>
</html>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Practical Frontend
2Mobile-First Strategy
What “mobile-first” means Mobile-First TypographyMobile-First NavigationMobile-First ImagesMobile-First FormsRecap Challenge