<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">
<title>Sidebar Menu HTML5/CSS/JS</title>
<script src="https://kit.fontawesome.com/433b24fc29.js" crossorigin="anonymous"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="sidebar">
<div class="sidebar_header">
<i class="fa-solid fa-dragon fa-2x"></i>
</div>
<div class="sidebar_menu">
<a href="#" class="sidebar_item">
<div class="sidebar_icon">
<i class="fa-solid fa-house"></i>
</div>
<h4>HOME</h4>
</a>
<a href="#" class="sidebar_item">
<div class="sidebar_icon">
<i class="fa-brands fa-steam"></i>
</div>
<h4>STEAM</h4>
</a>
<a href="#" class="sidebar_item">
<div class="sidebar_icon">
<i class="fa-brands fa-playstation"></i>
</div>
<h4>SONY</h4>
</a>
<a href="#" class="sidebar_item">
<div class="sidebar_icon">
<i class="fa-brands fa-xbox"></i>
</div>
<h4>XBOX</h4>
</a>
<a href="#" class="sidebar_item">
<div class="sidebar_icon">
<i class="fa-brands fa-twitch"></i>
</div>
<h4>TWITCH</h4>
</a>
</div>
</div>
<div class="container">
<header class="header">
<button class="btn-toggle"><i class="fa-solid fa-bars"></i></button>
</header>
<div class="content">
<i class="fa-brands fa-fantasy-flight-games fa-10x"></i>
</div>
</div>
<script src="js/script.js"></script>
</body>
</html>
/* ////////////////////
////////// CSS Variables
//////////////////// */
:root {
--padding: 0.5rem;
--margin-left: 0.5rem;
--smoky-black: #12100e;
--pine-tree: #30321c;
--blue: #2191fb;
--white: #ffffff;
--font-size: 0.8rem;
}
/* ////////////////////
////////// General Styles
//////////////////// */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
text-decoration: none;
}
body {
font-family: "Roboto", sans-serif;
}
/* ////////////////////
////////// Main container
//////////////////// */
.container {
width: auto;
min-height: 100vh;
transition: margin-left 0.3s linear;
}
/* ////////////////////
////////// Sidebar Menu
//////////////////// */
.sidebar {
width: 0;
height: 100%;
position: fixed;
top: 0;
left: 0;
overflow: hidden;
z-index: 1;
background-color: var(--pine-tree);
transition: width 0.3s linear;
}
.sidebar_header {
width: 100%;
height: 50px;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
color: var(--white);
}
.sidebar_menu {
width: 100%;
height: 100%;
}
.sidebar_item {
width: 100%;
padding: var(--padding);
font-size: var(--font-size);
background-color: var(--pine-tree);
color: #fff;
display: block;
position: relative;
white-space: nowrap;
}
.sidebar_item::before {
content: "";
width: 70%;
height: 4px;
position: absolute;
bottom: 0;
left: 0;
background-color: var(--blue);
transform: scale(0);
transform-origin: left;
transition: all 0.3s linear;
}
.sidebar_item:hover::before {
transform: scale(1);
}
.sidebar_item:hover {
background-color: #fff;
color: var(--smoky-black);
transition: all 0.3s linear;
}
.sidebar_item:active {
transform: translateY(0.2rem);
}
.sidebar_icon {
width: 20%;
display: inline-block;
text-align: center;
}
.sidebar_item h4 {
width: 80%;
display: inline-block;
}
.active {
width: 150px;
}
.active_margin_left {
margin-left: 150px;
}
/* ////////////////////
////////// HEADER
//////////////////// */
.header {
width: 100%;
height: 50px;
display: flex;
justify-content: start;
align-items: center;
background-color: var(--pine-tree);
}
/* Sidebar Toggle Button */
.btn-toggle {
width: 50px;
height: 25px;
border: none;
border-radius: 2px;
display: flex;
justify-content: center;
align-items: center;
padding: var(--padding);
background-color: var(--white);
color: var(--blue);
margin-left: var(--margin-left);
cursor: pointer;
transition: all 0.3s linear;
}
.btn-toggle:active {
box-shadow: 2px 2px 5px #fff;
outline: none;
}
/* ////////////////////
////////// Main content
//////////////////// */
.content {
height: calc(100vh - 50px);
background-color: var(--white);
padding: var(--padding);
display: flex;
justify-content: center;
align-items: center;
}
window.onload = load;
function load() {
let btnToggle = document.querySelector(".btn-toggle");
btnToggle.addEventListener("click", (e) => {
let sidebar = document.querySelector(".sidebar");
let container = document.querySelector(".container");
sidebar.classList.toggle("active");
container.classList.toggle("active_margin_left");
});
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.