Skip to content

Instantly share code, notes, and snippets.

@Giladx
Created August 24, 2023 20:32
Show Gist options
  • Save Giladx/d03bd21e1a0991a9f5666f497517f172 to your computer and use it in GitHub Desktop.
Save Giladx/d03bd21e1a0991a9f5666f497517f172 to your computer and use it in GitHub Desktop.
Off Canvas Menu CSS-only
<nav class="site-navbar">
<input type="checkbox" id="toggle_menu" class="toggle-menu-checkbox" />
<label for="toggle_menu" class="toggle-menu-label uppercase"></label>
<input type="checkbox" id="toggle_overlay" class="toggle-overlay-checkbox" />
<label for="toggle_menu" class="toggle-overlay-label uppercase"></label>
<ul class="site-menu">
</ul>
</nav>

Off Canvas Menu CSS-only

Just like you know it from all the mobile and responsive websites, just with pure CSS and not a single line of JavaScript.

A Pen by gilad on CodePen.

License.

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
/* =Variables + Placeholder
----------------------------------------------------- */
$toggle-stripe-2: 4px;
$toggle-stripe-3: 8px;
$navbar-background: #000;
$navbar-color: #fff;
$transition: .2s ease-in-out;
$menu-width: 240px;
$menu-shadow: 4px 0 16px -4px rgba(0, 0, 0, .2);
%transition {
-webkit-transition: $transition;
-moz-transition: $transition;
transition: $transition;
}
/* =Basic styles
----------------------------------------------------- */
body {
background: #eee;
font: 16px/1.4 "Roboto", Arial, sans-serif;
}
a {
text-decoration: none;
}
/* =Navbar
----------------------------------------------------- */
.site-navbar {
background: $navbar-background;
font-size: 14px;
height: 60px;
left: 20px;
position: fixed;
right: 20px;
top: 20px;
z-index: 11;
}
/* =Menu
----------------------------------------------------- */
.site-menu {
@extend %transition;
background: #fff;
bottom: 0;
font-size: 14px;
height: 100%;
left: -$menu-width;
list-style: none;
margin: 0;
padding: 0;
position: fixed;
top: 0;
width: $menu-width;
}
.site-menu-item {
> a {
color: #ccc;
display: block;
margin-bottom: 1px;
padding: 10px 18px;
&:hover,
&:active {
color: #333;
}
}
}
.site-menu-item.current > a {
color: #333;
}
/* =Overlay toggle
----------------------------------------------------- */
.toggle-overlay-checkbox {
display: none;
}
.toggle-overlay-label {
@extend %transition;
bottom: 0;
left: 0;
opacity: 0;
pointer-events: none; // Important to prevent unwanted triggering
position: fixed;
right: 0;
top: 0;
z-index: -1;
}
/* Display menu overlay */
.toggle-menu-checkbox:checked ~ .toggle-overlay-label {
background: rgba(0, 0, 0, .5);
opacity: 1;
pointer-events: auto;
z-index: 1;
}
/* =Menu toggle
----------------------------------------------------- */
.toggle-menu-checkbox,
.toggle-menu-label {
display: none;
}
.toggle-menu-checkbox:checked ~ .site-menu {
left: 0;
z-index: 2;
box-shadow: $menu-shadow;
-moz-box-shadow: $menu-shadow;
-webkit-box-shadow: $menu-shadow;
}
.toggle-overlay-checkbox:checked ~ .site-menu {
left: -$menu-width;
}
.toggle-menu-label {
cursor: pointer;
display: block;
padding: 0 0 0 20px;
position: absolute;
left: 20px;
top: 24px;
user-select: none;
-webkit-touch-callout: none;
-webkit-user-select: none;
&:before {
background: $navbar-color;
content: "";
height: 2px;
position: absolute;
left: 0;
top: 1px;
width: 1em;
box-shadow: 0 $toggle-stripe-2 0 0 $navbar-color, 0 $toggle-stripe-3 0 0 $navbar-color;
-moz-box-shadow: 0 $toggle-stripe-2 0 0 $navbar-color, 0 $toggle-stripe-3 0 0 $navbar-color;
-webkit-box-shadow: 0 $toggle-stripe-2 0 0 $navbar-color, 0 $toggle-stripe-3 0 0 $navbar-color;
}
&:after {
color: $navbar-color;
content: "Menu";
display: inline-block;
line-height: 1;
margin-top: -9px;
padding-right: 1.4em;
text-align: center;
text-transform: uppercase;
vertical-align: middle;
border-radius: 2px 2px 2px 2px;
}
}
.toggle-menu-checkbox:checked ~ .toggle-menu-label {
&:after {
content: "Menu";
color: #ccc !important;
}
&:before {
background: #ccc;
box-shadow: 0 $toggle-stripe-2 0 0 #ccc,
0 $toggle-stripe-3 0 0 #ccc;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment