Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created August 21, 2024 17:27
Show Gist options
  • Save thinkphp/d412eaadab6e149552653f1e2a7af998 to your computer and use it in GitHub Desktop.
Save thinkphp/d412eaadab6e149552653f1e2a7af998 to your computer and use it in GitHub Desktop.
responsive design
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Hamburger Menu</title>
<style>
body {
margin: 0;
padding: 0;
}
nav {
display: flex;
align-items: center;
justify-content: space-between;
padding: 20px;
background-color: #333;
color: #fff;
}
.menu-icon {
display: none;
flex-direction: column;
justify-content: space-between;
cursor: pointer;
width: 30px;
height: 20px;
}
.bar {
width: 30px;
height: 3px;
background-color: #fff;
}
.menu {
display: flex;
list-style: none;
margin: 0;
padding: 0;
}
.menu li {
margin-right: 20px;
}
.menu a {
text-decoration: none;
color: #fff;
}
@media screen and (max-width: 768px) {
.menu {
display: none;
}
.menu-icon {
display: flex;
}
.menu.active {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.8);
padding: 20px;
}
.menu.active li {
margin-bottom: 20px;
}
}
</style>
</head>
<body>
<nav>
<div class="menu-icon">
<div class="bar"></div>
<div class="bar"></div>
<div class="bar"></div>
</div>
<ul class="menu">
<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>
<div class="content">
</div>
<script>
const menuIcon = document.querySelector('.menu-icon');
const menu = document.querySelector('.menu');
menuIcon.addEventListener('click', () => {
menu.classList.toggle('active');
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment