JSFiddle - React, Tailwind, and code Playground
by Pankaj Kargirwar
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sliding Menu</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="menu" class="menu">
<a href="javascript:void(0)" class="close-btn" onclick="closeMenu()">×</a>
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Contact</a>
</div>
<span class="open-btn" onclick="openMenu()">☰ Open Menu</span>
<script>
function openMenu() {
document.getElementById("menu").style.width = "250px"; // Adjust width as needed
}
function closeMenu() {
document.getElementById("menu").style.width = "0";
}
</script>
</body>
</html>
CSS
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
overflow-x: hidden; /* Prevent horizontal scroll */
}
.menu {
height: 100%; /* Full height */
width: 0; /* 0 width - change this with JavaScript */
position: fixed; /* Stay in place */
z-index: 1; /* Stay on top */
top: 0;
left: 0;
background-color: #111; /* Black background color */
overflow-x: hidden; /* Disable horizontal scroll */
transition: 0.5s; /* 0.5 second transition effect to slide in the menu */
padding-top: 60px; /* Place content 60px from the top */
}
.menu a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s;
}
.menu a:hover {
color: #f1f1f1;
}
.menu .close-btn {
position: absolute;
top: 20px;
right: 25px;
font-size: 36px;
margin-left: 50px;
color: #fff;
text-decoration: none;
}
.open-btn {
font-size: 30px;
cursor: pointer;
background-color: #111;
color: white;
padding: 10px 15px;
border: none;
position: absolute;
top: 20px;
left: 20px;
transition: 0.3s;
}
.open-btn:hover {
background-color: #444;
}