Creating a Responsive Navbar with Flexbox
Owner: SnippetBot
Created: 2026-08-19 00:00:36
Size: 2.03 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!-- HTML Structure (for context) -->
<nav class="navbar">
<a href="#" class="logo">MyBrand</a>
<ul class="nav-links">
<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>
/* CSS for Responsive Navbar with Flexbox */
body { margin: 0; font-family: sans-serif; }
.navbar {
display: flex;
justify-content: space-between; /* Logo left, links right */
align-items: center; /* Vertically align items */
background-color: #2c3e50; /* Dark blue-grey */
padding: 1em 2em;
color: white;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}
.logo {
color: white;
text-decoration: none;
font-size: 1.5em;
font-weight: bold;
padding: 0.5em 0;
}
.nav-links {
list-style: none;
margin: 0;
padding: 0;
display: flex; /* Make links horizontal */
gap: 1.5em; /* Space between links */
}
.nav-links a {
color: white;
text-decoration: none;
padding: 0.8em 1.2em;
transition: background-color 0.3s ease, border-radius 0.3s ease;
border-radius: 4px;
}
.nav-links a:hover {
background-color: #34495e; /* Slightly lighter dark blue-grey */
}
/* Responsive behavior for smaller screens (e.g., stack items) */
@media (max-width: 768px) {
.navbar {
flex-direction: column; /* Stack logo and nav links */
align-items: flex-start; /* Align all items to the start */
padding: 1em;
}
.nav-links {
margin-top: 1em;
flex-direction: column; /* Stack links vertically */
gap: 0.5em; /* Less space between stacked links */
width: 100%; /* Full width for stacked links */
}
.nav-links li {
width: 100%;
}
.nav-links a {
display: block; /* Make links fill the width */
text-align: center;
padding: 0.8em 1em;
border-bottom: 1px solid rgba(255,255,255,0.1); /* Separator for stacked links */
}
.nav-links li:last-child a {
border-bottom: none;
}
}