How to make Responsive Navbar using HTML, CSS, and Javascript.
Navbar - 1
HTML Code :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./styles.css">
<link rel="stylesheet"
href="https://unpkg.com/ionicons-css@5.2.1/dist/icon.css">
<title>Navbar</title>
</head>
<body>
<header>
<div class="logo">
<img src="./cleverds.png" alt="Profile">
<a href="" class="header_logo">Clever Developers</a>
</div>
<nav class="nav" id="nav-menu">
<ion-icon class="icon-close" id="header_close"></ion-icon>
<ul class="nav_list">
<li class="nav_item"><a href="" class="nav_link">Home</a></li>
<li class="nav_item"><a href="" class="nav_link">About</a></li>
<li class="nav_item"><a href="" class="nav_link">Skills</a></li>
<li class="nav_item"><a href="" class="nav_link">Portfolio</a></li>
<li class="nav_item"><a href="" class="nav_link">Contact</a></li>
</ul>
</nav>
<ion-icon class="icon-menu-outline" id="header_toggle"></ion-icon>
</header>
<script src="./index.js"></script>
</body>
</html>
CSS Code :
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
background-image: url('./photo.jpg');
background-position: center;
background-size: cover;
background-repeat: no-repeat;
height: 100vh;
}
a {
text-decoration: none;
}
ul {
list-style: none;
}
header {
display: flex;
background-color: black;
justify-content: space-around;
padding: 1rem 0;
align-items: center;
}
.logo {
display: flex;
align-items: center;
}
.logo img {
width: 50px;
border-radius: 50%;
margin-right: 10px;
}
.header_logo {
color: white;
font-weight: 400;
}
.nav_list {
display: flex;
align-items: center;
}
.nav_item {
margin: 0 8px;
}
.nav_link {
padding: 10px;
color: white;
font-size: 1rem;
font-weight: 400;
border-radius: 10px;
}
.nav_link:hover {
background-color: #f32b6e;
}
#header_toggle,
#header_close {
display: none;
}
@media screen and (max-width: 768px) {
header {
height: 45px;
justify-content: space-between;
padding: 0 28px;
}
.logo a {
font-size: 1rem;
}
.logo img {
width: 30px;
}
#header_toggle {
display: inline;
color: white;
font-size: 25px;
}
#header_close{
position: absolute;
right: 25px;
display: block;
font-size: 25px;
border-radius: 50%;
}
#header_close:hover {
background-color: #f32b6e;
}
.nav {
position: fixed;
top: 0;
right: -100%;
background-color: #0abfbc;
color: white;
width: 60%;
height: 100vh;
padding: 24px 0;
z-index: 100;
transition: 0.5s;
border-radius: 0 0 0 50%;
}
.nav_list {
display: flex;
flex-direction: column;
}
.nav_item {
margin: 2rem 0;
}
.show {
right: 0;
}
}
JavaScript Code :
const navMenu = document.getElementById('nav-menu');
const toggleMenu = document.getElementById('header_toggle');
const closeMenu = document.getElementById('header_close');
toggleMenu.addEventListener('click', () => {
navMenu.classList.toggle('show');
});
closeMenu.addEventListener('click', () => {
navMenu.classList.remove('show');
});