HTML CSS Navbar Template code
index.html
1
<nav class="navbar">
2
<div class="container">
3
4
<div class="navbar-header">
5
<button class="navbar-toggler" data-toggle="open-navbar1">
6
<span></span>
7
<span></span>
8
<span></span>
9
</button>
10
<a href="#">
11
<h4>Your<span>Logo</span></h4>
12
</a>
13
</div>
14
15
<div class="navbar-menu" id="open-navbar1">
16
<ul class="navbar-nav">
17
<li class="active"><a href="#">Home</a></li>
18
<li class="navbar-dropdown">
19
<a href="#" class="dropdown-toggler" data-dropdown="my-dropdown-id">
20
Categories <i class="fa fa-angle-down"></i>
21
</a>
22
<ul class="dropdown" id="my-dropdown-id">
23
<li><a href="#">Category 1</a></li>
24
<li class="separator"></li>
25
<li><a href="#">Category 2</a></li>
26
<li class="separator"></li>
27
<li><a href="#">Category 3</a></li>
28
</ul>
29
</li>
30
31
<li><a href="#">About</a></li>
32
<li><a href="#">Contact</a></li>
33
<li><a href="#">Signin</a></li>
34
</ul>
35
</div>
36
</div>
37
</nav>
styles.css
1
@import url("https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap");
2
* {
3
box-sizing: border-box;
4
padding: 0;
5
margin: 0;
6
}
7
8
body {
9
font-family: "Roboto", sans-serif;
10
font-size: 0.925rem;
11
}
12
13
a {
14
text-decoration: none;
15
}
16
17
.container {
18
width: 1170px;
19
position: relative;
20
margin-left: auto;
21
margin-right: auto;
22
padding-left: 15px;
23
padding-right: 15px;
24
}
25
26
.navbar,
27
.navbar > .container {
28
width: 100%;
29
display: flex;
30
flex-wrap: wrap;
31
align-items: center;
32
justify-content: space-between;
33
}
34
@media (max-width: 768px) {
35
.navbar,
36
.navbar > .container {
37
display: block;
38
}
39
}
40
41
.navbar {
42
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
43
background-color: #fff;
44
padding: 1rem 1.15rem;
45
border-bottom: 1px solid #eceef3;
46
/*
47
|-----------------------------------
48
| Start navbar logo or brand etc..
49
|-----------------------------------
50
*/
51
/*
52
|-----------------------------------
53
| Start navbar menu
54
|-----------------------------------
55
*/
56
}
57
@media (min-width: 576px) {
58
.navbar .container {
59
max-width: 540px;
60
}
61
}
62
@media (min-width: 768px) {
63
.navbar .container {
64
max-width: 720px;
65
}
66
}
67
@media (min-width: 992px) {
68
.navbar .container {
69
max-width: 960px;
70
}
71
}
72
@media (min-width: 1200px) {
73
.navbar .container {
74
max-width: 1140px;
75
}
76
}
77
.navbar .navbar-header {
78
display: flex;
79
align-items: center;
80
}
81
@media (max-width: 768px) {
82
.navbar .navbar-header {
83
width: 100%;
84
display: flex;
85
align-items: center;
86
justify-content: space-between;
87
flex-direction: row-reverse;
88
}
89
}
90
.navbar .navbar-header .navbar-toggler {
91
cursor: pointer;
92
border: none;
93
display: none;
94
outline: none;
95
}
96
@media (max-width: 768px) {
97
.navbar .navbar-header .navbar-toggler {
98
display: block;
99
}
100
}
101
.navbar .navbar-header .navbar-toggler span {
102
height: 2px;
103
width: 22px;
104
background-color: #929aad;
105
display: block;
106
}
107
.navbar .navbar-header .navbar-toggler span:not(:last-child) {
108
margin-bottom: 0.2rem;
109
}
110
.navbar .navbar-header > a {
111
font-weight: 500;
112
color: #3c4250;
113
}
114
.navbar .navbar-menu {
115
display: flex;
116
align-items: center;
117
flex-basis: auto;
118
flex-grow: 1;
119
}
120
@media (max-width: 768px) {
121
.navbar .navbar-menu {
122
display: none;
123
text-align: center;
124
}
125
}
126
.navbar .navbar-menu.active {
127
display: flex !important;
128
}
129
.navbar .navbar-menu .navbar-nav {
130
margin-left: auto;
131
flex-direction: row;
132
display: flex;
133
padding-left: 0;
134
margin-bottom: 0;
135
list-style: none;
136
}
137
@media (max-width: 768px) {
138
.navbar .navbar-menu .navbar-nav {
139
width: 100%;
140
display: block;
141
border-top: 1px solid #EEE;
142
margin-top: 1rem;
143
}
144
}
145
.navbar .navbar-menu .navbar-nav > li > a {
146
color: #3c4250;
147
text-decoration: none;
148
display: inline-block;
149
padding: 0.5rem 1rem;
150
}
151
.navbar .navbar-menu .navbar-nav > li > a:hover {
152
color: #66f;
153
}
154
@media (max-width: 768px) {
155
.navbar .navbar-menu .navbar-nav > li > a {
156
border-bottom: 1px solid #eceef3;
157
}
158
}
159
.navbar .navbar-menu .navbar-nav > li.active a {
160
color: #66f;
161
}
162
.navbar .navbar-menu .navbar-nav .navbar-dropdown .dropdown {
163
list-style: none;
164
position: absolute;
165
top: 150%;
166
left: 0;
167
background-color: #fff;
168
padding-top: 0.5rem;
169
padding-bottom: 0.5rem;
170
min-width: 160px;
171
width: auto;
172
white-space: nowrap;
173
box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.1);
174
z-index: 99999;
175
border-radius: 0.75rem;
176
display: none;
177
}
178
@media (max-width: 768px) {
179
.navbar .navbar-menu .navbar-nav .navbar-dropdown .dropdown {
180
position: relative;
181
box-shadow: none;
182
}
183
}
184
.navbar .navbar-menu .navbar-nav .navbar-dropdown .dropdown li a {
185
color: #3c4250;
186
padding: 0.25rem 1rem;
187
display: block;
188
}
189
.navbar .navbar-menu .navbar-nav .navbar-dropdown .dropdown.show {
190
display: block !important;
191
}
192
.navbar .navbar-menu .navbar-nav .dropdown > .separator {
193
height: 1px;
194
width: 100%;
195
margin-top: 9px;
196
margin-bottom: 9px;
197
background-color: #eceef3;
198
}
199
.navbar .navbar-dropdown {
200
position: relative;
201
}
202
203
.navbar .navbar-header > a span {
204
color: #66f;
205
}
206
207
.navbar .navbar-header h4 {
208
font-weight: 500;
209
font-size: 1.25rem;
210
}
211
@media (max-width: 768px) {
212
.navbar .navbar-header h4 {
213
font-size: 1.05rem;
214
}
215
}
main.js
1
let dropdowns = document.querySelectorAll('.navbar .dropdown-toggler')
2
let dropdownIsOpen = false
3
4
// Handle dropdown menues
5
if (dropdowns.length) {
6
dropdowns.forEach((dropdown) => {
7
dropdown.addEventListener('click', (event) => {
8
let target = document.querySelector(`#${event.target.dataset.dropdown}`)
9
10
if (target) {
11
if (target.classList.contains('show')) {
12
target.classList.remove('show')
13
dropdownIsOpen = false
14
} else {
15
target.classList.add('show')
16
dropdownIsOpen = true
17
}
18
}
19
})
20
})
21
}
22
23
// Handle closing dropdowns if a user clicked the body
24
window.addEventListener('mouseup', (event) => {
25
if (dropdownIsOpen) {
26
dropdowns.forEach((dropdownButton) => {
27
let dropdown = document.querySelector(`#${dropdownButton.dataset.dropdown}`)
28
let targetIsDropdown = dropdown == event.target
29
30
if (dropdownButton == event.target) {
31
return
32
}
33
34
if ((!targetIsDropdown) && (!dropdown.contains(event.target))) {
35
dropdown.classList.remove('show')
36
}
37
})
38
}
39
})
40
function handleSmallScreens() {
41
document.querySelector('.navbar-toggler')
42
.addEventListener('click', () => {
43
let navbarMenu = document.querySelector('.navbar-menu')
44
45
if (!navbarMenu.classList.contains('active')) {
46
navbarMenu.classList.add('active')
47
} else {
48
navbarMenu.classList.remove('active')
49
}
50
})
51
}
52
53
handleSmallScreens()
54