Simple, responsive nav
by csswizardry
HTML
<nav class="responsive-nav" id="js-responsive-nav">
<ul class="responsive-nav__list">
<li class="responsive-nav__item">
<a href="http://csswizardry.com/" class="responsive-nav__link">Home</a>
</li>
<li class="responsive-nav__item">
<a href="http://csswizardry.com/about" class="responsive-nav__link">About</a>
</li>
<li class="responsive-nav__item">
<a href="http://csswizardry.com/work" class="responsive-nav__link">Work</a>
</li>
<li class="responsive-nav__item">
<a href="https://twitter.com/csswizardry" class="responsive-nav__link" target="_blank"><b>@csswizardry</b></a>
</li>
</ul>
</nav>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus.</p>
SCSS
/**
* Housekeeping. Nothing to see here...
*/
html {
font-size: 12px;
line-height: 1.5;
font-family: sans-serif;
}
a {
text-decoration: none;
&:hover {
text-decoration: underline;
}
}
/**
* Very simple, pared-back, mobile-first, responsive nav.
*/
$responsive-nav-hit-area: 44px;
$responsive-nav-icon-size: 16px;
$responsive-nav-color: #fff;
$responsive-nav-background: #f43059;
$responsive-nav-borders: lighten($responsive-nav-background, 10%);
/**
* An element (`nav`) in which to house our dropdown.
*
* 1. Make sure we can’t actually see the absolutely positioned dropdown.
*/
.responsive-nav {
position: relative;
z-index: 1;
width: $responsive-nav-hit-area;
height: $responsive-nav-hit-area;
color: $responsive-nav-color;
background-color: $responsive-nav-background;
overflow: hidden; /* [1] */
border-radius: 3px;
cursor: pointer;
/**
* Make the wrapper element lie horizontally on wider viewports.
* Nasty RWD makes us undo a lot of stuff :(
*/
@media screen and (min-width: 720px) {
position: static;
width: auto;
height: auto;
background: none;
color: $responsive-nav-background;
}
/**
* Place a hamburger (don’t hate the player, hate the game...) icon
* over the top of our nav element.
*/
&:after {
content: "☰";
font-size: $responsive-nav-icon-size;
width: 1em;
line-height: 1;
text-align: center;
position: absolute;
top: 50%;
left: 50%;
margin-top: -($responsive-nav-icon-size / 2);
margin-left: -($responsive-nav-icon-size / 2);
/**
* Remove the hamburger on wider viewports.
*/
@media screen and (min-width: 720px) {
content: normal;
content: none;
}
}
/**
* When we add this class, we simply allow the absolutely positioned
*...
JavaScript
/**
* We need to add a class of `responsive-nav--open` to
* `#js-responsive-nav` ONLY when the nav is clicked AND
* when we’re on a viewport of less than `responsiveNavBreakpoint`.
* It needs removing when someone re-clicks the nav icon, or
* when the viewport no longer matches `responsiveNavBreakpoint`.
*/
var responsiveNav = document.getElementById('js-responsive-nav');
var responsiveNavBreakpoint = 720;
responsiveNav.addEventListener('click', function(){
if(window.innerWidth < responsiveNavBreakpoint){
responsiveNav.classList.toggle("is-open");
}
});