Passa al contingut principal

Responsive Menu Concepts

The following is a guest post by Tim Pietrusky. I know Tim from his prolific work on CodePen and from being a helpful community member there. He wrote to me with this guest post about responsive menus which I'm more than happy to share with you below. Not only is it a timely concept, but one of the concepts improves upon a clever CSS trick we've covered here in the past.

When it comes to responsive design we are faced with various techniques on how to best handle altering our navigation menus for small screens. The resources seem endless. That’s why I’m going to show you four main concepts and discuss the advantages and disadvantages of all of them.


Three of them are made with pure CSS and one uses a single line of JavaScript.


Before We Start


In the code presented in this article, I don’t use any vendor-prefixes to keep the CSS easier to see and understand. The more complex CSS examples use SCSS. Each example is hosted on CodePen where you can see the compiled CSS if you wish.


All menu concepts in this article are based on this simple HTML structure which I call basic menu. The role attribute is used to specify the particular concept (full-horizontal, select, custom-dropdown and off-canvas).



<nav role="">
<ul>
<li><a href="#">Stream</a></li>
<li><a href="#">Lab</a></li>
<li><a href="#">Projects</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>​


To address small screens I use the same media query on all concepts.



@media screen and (max-width: 44em) {

}


1. Full Horizontal


This is the most simple approach because you just need to make the list elements full width on small screens.



<nav role="full-horizontal">
<ul>
<li><a href="#">Stream</a></li>
<li><a href="#">Lab</a></li>
<li><a href="#">Projects</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>​



@media screen and (max-width: 44em) {
nav[role="full-horizontal"] {
ul > li {
width: 100%;
}
}
}


This is what it looks like on a small screen with a custom style.


full-horz

Advantages



  • No JavaScript

  • No extra HTML

  • Simple CSS


Disadvantages



  • Reserves too much screen-space


Demo


On CodePen:


2. Select


This concept hides the basic menu on small screens and shows a select menu instead.


To achieve this we need to extend our basic markup and add a select. To get the select working we also add some JavaScript which alters window.location.href when the onchange event on the select occurs.



<nav role="select">
<!-- basic menu goes here -->

<select onchange="if (this.value) window.location.href = this.value;">
<option value="#">Stream</option>
<option value="#">Lab</option>
<option value="#">Projects</option>
<option value="#">About</option>
<option value="#">Contact</option>
</select>
</nav>​


We hide the select on big screens.



nav[role="select"] {
> select {
display:none;
}
}


On small screens, we hide the basic menu and show the select. To help the user recognize that this is a menu we’re also adding a pseudo-element with the text “Menu”.



@media screen and (max-width: 44em) {
nav[role="select"] {
ul {
display: none;
}

select {
display: block;
width: 100%;
}

&:after {
position: absolute;
content: "Menu";
right: 0;
bottom: -1em;
}
}
}


This is what it looks like on a small screen with a custom style.


num-2

Advantages



  • Doesn’t need much space

  • Uses native controls


Disadvantages



  • Needs JavaScript

  • Duplicate content

  • Select is not styleable in every browser


Demo


On CodePen:


3. Custom Dropdown


This concept hides the basic menu on small screens and shows an input & label (to use the Checkbox Hack) instead. When the user clicks on the label, the basic menu is shown underneath.



<nav role="custom-dropdown">
<!-- Advanced Checkbox Hack (see description below) -->

<!-- basic menu goes here -->
</nav>​


Problem with the Checkbox Hack


There are two problems with the default Checkbox Hack:



  1. Doesn’t work on mobile Safari (iOS < 6.0). It’s not possible to click the label on iOS < 6.0 to toggle the input due to a bug. The only solution is to add an empty onclick to the label.

  2. Doesn’t work on the default Android browser (Android <= 4.1.2). Once upon a time there was a WebKit Adjacent/General Sibling & Pseudo Class Bug which prevented the use of pseudo-classes combined with adjacent (+) or general (~) sibling combinators.



h1 ~ p { color: black; }
h1:hover ~ p { color: red; }


This has no effect because the checkbox hack uses the pseudo-class :checked combined with the general sibling. And since this was fixed in WebKit 535.1 (Chrome 13) and the actual WebKit on Android 4.1.2 is 534.30, the normal checkbox hack doesn’t work on any Android device to date.


The best solution is to add a WebKit-only fake animation on the body element.


All stuff combined creates the Advanced Checkbox Hack:



<!-- Fix for iOS -->
<input type="checkbox" id="menu">
<label for="menu" onclick></label>



/* Fix for Android */
body {
-webkit-animation: bugfix infinite 1s;
}
@-webkit-keyframes bugfix {
from { padding: 0; }
to { padding: 0; }
}

/* default checkbox */
input[type=checkbox] {
position: absolute;
top: -9999px;
left: -9999px;
}

label {
cursor: pointer;
user-select: none;
}


Reference: Advanced Checkbox Hack


For large screens, we hide the label:



nav[role="custom-dropdown"] {
label {
display: none;
}
}


For small screens, we hide the basic menu and show the label. To help the user recognize that this is a menu we’re also adding a pseudo-element with the text “≡” (converted to “\2261” to use it as content on the pseudo-element) to the label. When the user clicks on the input, the basic menu gets shown and the list elements are expanded to full width.



@media screen and (max-width: 44em) {
nav[role="custom-dropdown"] {
ul {
display: none;
height: 100%;
}

label {
position: relative;
display: block;
width: 100%;
}

label:after {
position: absolute;
content: "\2261";
}

input:checked ~ ul {
display: block;

> li {
width: 100%;
}
}
}
}


This is what the menu looks like on a small screen with a custom style.


closed

Closed open

Open

Advantages



  • Doesn’t need much space when closed

  • Custom styling

  • No JavaScript


Disadvantages



  • Bad semantics (input / label)

  • Extra HTML


Demo


On CodePen:


4. Off Canvas


This concept hides the basic menu on small screens and shows a HTML input & label (to use the Advanced Checkbox Hack, see 3. Custom Dropdown for more infos) instead. When the user clicks on the label, the basic menu flies in from the left and the content moves to the right – the screen gets divided: menu ~80 % and content ~20 % (depends on resolution and css units).



<input type="checkbox" id="menu">
<label for="menu" onclick></label>

<!-- basic menu goes here -->

<div class="content">
<!-- content goes here -->
</div>


On large screens, we hide the label.



label {
position: absolute;
left: 0;
display: none;
}


On small screens, we hide the basic menu outside the viewport and show the label / input. To hide the menu we specify a width ($menu_width) and add a negative position to it. To help the user recognize that this is a menu we’re also adding a pseudo-element with the text “≡” (converted to “\2261” to use it as content on the pseudo-element) to the label.

When the user clicks on the input, the basic menu flies in from the left and the content moves to the right.



@media screen and (max-width: 44em) {
$menu_width: 20em;

body {
overflow-x: hidden;
}

nav[role="off-canvas"] {
position: absolute;
left: -$menu_width;
width: $menu_width;

ul > li {
width: 100%;
}
}

label {
display: block;
}

label:after {
position: absolute;
content: "\2261";
}

input:checked ~ nav[role="off-canvas"] {
left: 0;
}

input:checked ~ .content {
margin-left: $menu_width + .5em;
margin-right: -($menu_width + .5em);
}
}​


This is what the menu looks like on a small screen with a custom style.


offcanvas-closed

Closed offcanvas-open

Open

Advantages



  • Doesn’t need much space when closed

  • Custom styling

  • No JavaScript

  • Convention from Facebook / Google+ app


Disadvantages



  • Bad semantics (input / label)

  • Extra HTML

  • Absolute position to the body = Feels like fixed position


Demo


On CodePen:


Does it Work on IE?


All of the techniques used above have one goal: Create responsive menus for modern browsers! And because there is no IE 8 or lower on any mobile device we don’t need to worry about it.


Responsive Menu Concepts is a post from CSS-Tricks






via CSS-Tricks http://css-tricks.com/responsive-menu-concepts/

Comentaris

Entrades populars d'aquest blog

Learn Composition from the Photography of Henri Cartier-Bresson

“Do you see it?” This question is a photographic mantra. Myron Barnstone , my mentor, repeats this question every day with the hopes that we do “see it.” This obvious question reminds me that even though I have seen Cartier-Bresson’s prints and read his books, there are major parts of his work which remain hidden from public view. Beneath the surface of perfectly timed snap shots is a design sensibility that is rarely challenged by contemporary photographers. Henri Cartier-Bresson. © Martine Franck Words To Know 1:1.5 Ratio: The 35mm negative measures 36mm x 24mm. Mathematically it can be reduced to a 3:2 ratio. Reduced even further it will be referred to as the 1:1.5 Ratio or the 1.5 Rectangle. Eyes: The frame of an image is created by two vertical lines and two horizontal lines. The intersection of these lines is called an eye. The four corners of a negative can be called the “eyes.” This is extremely important because the diagonals connecting these lines will form the breakdown ...

El meu editor de codi preferit el 2024, que això ja se sap que va canviant 😄

Visual Code Visual Code és un editor de codi font lleuger, però potent que s’executa al teu escriptori i està disponible per a Windows, macOS i Linux. Compta amb suport integrat per a JavaScript, TypeScript i Node.js i té un ric ecosistema d’extensions per a altres llenguatges i entorns d’execució (com C++, C#, Java, Python, PHP, Go, .NET).  És una eina ideal per a desenvolupar i depurar aplicacions web i en el núvol. Per què Visual Code? Visual Code té molts avantatges com a editor de codi font, com per exemple: És gratuït, ràpid i fàcil d’instal·lar i actualitzar. Té un ampli ecosistema d’extensions que et permeten afegir funcionalitats i personalitzar la teva experiència de desenvolupament. Té un suport integrat per a molts llenguatges i entorns d’execució, i et permet depurar i executar el teu codi des del mateix editor. Té una interfície senzilla i elegant, amb diferents temes i modes de visualització. Té un sistema de sincronització de configuracions que et permet guardar les...

Las Mejores Aplicaciones Gratis para iPad de 2012

Las Mejores Aplicaciones Gratis para iPad de 2012 : ¿No tienes ni un duro? No te preocupes, pues hoy os traemos una extensa selección de las mejores apps gratuitas que puedes conseguir en la App Store para que llenes tu iPad de calidad, sin gastar nada de nada.   ¿Estás buscando juegos o apps gratis para tu iPad? En la App Store hay más de 500,000 apps y juegos, y una gran cantidad de ellos está disponible de forma totalmente gratuita. Aquí vamos con la selección de las mejores Apps gratis para iPad (todos los modelos), organizada por categoría. ¿Estás preparado? Las Mejores Apps Gratis de Redes Sociales para iPad Nombre Facebook Gratis Categoría Redes sociales Facebook es la red social más famosa del mundo , con casi mil millones de usuarios. Su app para iPad ha tardado, pero aquí está. Nombre Twitter Gratis Categoría Redes sociales Twitter es la red de microblogging por excelencia. La forma más rápida y directa de informar y mantenerse informado de las cosa...