Skip to content

Instantly share code, notes, and snippets.

@glueckpress
Created January 1, 2014 15:37
Show Gist options
  • Select an option

  • Save glueckpress/8208901 to your computer and use it in GitHub Desktop.

Select an option

Save glueckpress/8208901 to your computer and use it in GitHub Desktop.
CSS-only off-canvas menu, no javascript at all. Requires browser support for :checked pseudo-class and adjacent sibling selector. Fiddle at http://jsfiddle.net/glueckpress/3psut/.
* {
margin: 0;
padding: 0;
}
html,
body,
#page {
height: 100%;
}
p, ul {
margin: 0 1em 1.5em;
}
ul {
list-style: none;
padding-left: 0;
}
h1 {
background: #212121;
color: #fff;
font-family: sans-serif;
font-size: 1em;
font-weight: normal;
line-height: 36px; /* measure against label */
position: absolute;
text-align: center;
top: 0;
width: 100%;
z-index: 1;
}
#page {
overflow: hidden;
padding-top: 36px;
position: relative;
}
label[for="menu-toggle"] {
border-top: 12px double #fff;
border-bottom: 4px solid #fff;
cursor: pointer;
height: 4px;
left: 8px;
overflow: hidden;
text-indent: 100%;
width: 36px;
position: absolute;
top: 8px;
z-index: 1;
}
#menu-toggle { /* goes off the canvas */
left: -100%;
position: absolute;
top: -100%;
-webkit-appearance: none;
}
nav,
main {
box-sizing: border-box;
display: block;
float: left;
overflow: hidden;
padding: 1em 0;
position: relative;
-webkit-transition: all .25s ease-in;
}
nav {
background: #212121;
clear: left;
color: #fff;
min-height: 100%;
width: 30%;
}
nav a {
color: #fff;
}
main {
width: 70%;
}
/*
* For decent browsers only.
* Delete to test fallback.
*/
#menu-toggle:not(:checked) ~ nav {
margin-left: -30%;
}
#menu-toggle:checked ~ nav {
margin-left: 0;
}
#menu-toggle:not(:checked) ~ nav ~ main {
width: 100%;
}
#menu-toggle:checked ~ nav ~ main {
width: 70%;
}
<div id="page">
<h1>Site Title</h1>
<label for="menu-toggle">Menu</label>
<input id="menu-toggle" type="checkbox" name="menu-toggle">
<nav>
<ul>
<li><a href="#">Menu item</a></li>
<li><a href="#">Menu item</a></li>
<li><a href="#">Menu item</a></li>
</ul>
</nav>
<main>
<p>Some fancy web site content goes here. But behold, press the three bars at the top and there shall be a menu!</p>
</main>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment