Search This Blog

Logo

Follow by Email

Social Follow

Index Labels

Drop-Down Navigation: Responsive and Touch-Friendly

..

This is a backup Copy from osvaldas.info (This page is for personal usage only)

What if you need a multi-level navigation? In most cases, you design a drop-down menu using unordered lists. But what do you do to make it usable on small and / or cursorless screens? Byusable I mean being able to use hyperlinks on parental anchors and open them with a double-tap (which is a native act on touch devices), also being able to close the drop-downs by tapping anywhere outside them to avoid flashing and other huge usability faults but having a usual bulletproof drop-down menu on desktop screens at the same time. A while ago I came up with quite a simple technique. I have been successfully implementing it into my projects as there is no room for one-sided techniques anymore.

The Problem

...is a drop-down on touch screen because there is no cursor and you cannot hover on the navigation items! It gets even worse if you have links on parent menu items. Once you click them, the drop-down shows up for a second and then immediately disappears because the browser has just opened the link of an anchor you pressed! That sucks. As well as using hashsymbol for parental link href's: as a consequence, you cannot use normal URLs, the page sometimes may scroll up after the link is clicked, the drop-down may flash, you won't be able to shut it off, etc. Sorry, that is not a problem anymore – it is solved in this post! Watch my very first video montage to get the idea:

Talking about responsiveness, there is a plenty, plenty of responsive navigation solutions out there. I admit, I have hardly gone deeper into any of these, but still my favorite one is David Bushell's off-canvas navigation. It is awesome. However, as for my personal projects I fit the usual one-button responsive navigation pattern. The same one goes here as well.

Composition

The technique consists of three main parts:

Simple drop-down navigation based on HTML and CSS;

Responsiveness implementation using media queries;

Adoption for touch screen devices with the help of a super tiny jQuery plugin.

Now about everything in turn…

HTML and CSS drop-down navigation

Firstly, simple markup of multi-leveled unordered lists with a little bit of HTML5 flavor. My example consists of two levels. Two is not the limit, you may have infinite levels on your next super project.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

<navid="nav"role="navigation">

<ahref="#nav"title="Show navigation">Show navigation</a>

<ahref="#"title="Hide navigation">Hide navigation</a>

<ul>

<li><ahref="/">Home</a></li>

<li>

<ahref="/"aria-haspopup="true">Blog</a>

<ul>

<li><ahref="/">Design</a></li>

<li><ahref="/">HTML</a></li>

<li><ahref="/">CSS</a></li>

<li><ahref="/">JavaScript</a></li>

</ul>

</li>

<li>

<ahref="/"aria-haspopup="true">Work</a>

<ul>

<li><ahref="/">Web Design</a></li>

<li><ahref="/">Typography</a></li>

<li><ahref="/">Front-End</a></li>

</ul>

</li>

<li><ahref="/">About</a></li>

</ul>

</nav>

Now let's just give a sense to the HTML above with the CSS below. Presenting only the principle parts of CSS. Full code – later in the demo.

Do not be surprised by Show / Hide navigation anchors. I am going to hide these for now using#nav > a selector and flick on later in the next step.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

#nav

{

/* container */

}

#nav > a

{

display: none;

}

#nav li

{

position: relative;

}

/* first level */

#nav > ul

{

height: 3.75em;

}

#nav > ul > li

{

width: 25%;

height: 100%;

float: left;

}

/* second level */

#nav li ul

{

display: none;

position: absolute;

top: 100%;

}

#nav li:hover ul

{

display: block;

}

User interface behavior on non-touch screens is based on the result of position: relative and position: absolute manipulation. On touchscreens it still fails at solvingthe problem, but we will make it right later in the 3rd part of the post.

Responsive drop-down navigation

Let's pour some responsiveness in. 640 pixels is my choice for a major breakpoint at which with the help of a single media query, the navigation shrinks to a button (icon). Therefore, the menu can only be called out by pressing the button.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

@media only screenand ( max-width: 40em) /* 640 */

{

#nav

{

position: relative;

}

#nav > a

{

}

#nav:not( :target ) > a:first-of-type,

#nav:target > a:last-of-type

{

display: block;

}

/* first level */

#nav > ul

{

height: auto;

display: none;

position: absolute;

left: 0;

right: 0;

}

#nav:target > ul

{

display: block;

}

#nav > ul > li

{

width: 100%;

float: none;

}

/* second level */

#nav li ul

{

position: static;

}

}

CSS pseudo selector :target makes it easy to toggle menu as the anchor's href attribute coincides with CSS ID selector name. Therefore, as you might have understood, the first anchor of #nav > a is responsible for showing and the second for hiding the menu.

Touch-friendly drop-down navigation

The most important things go in the end: let's finally solve the problem described in the very first paragraphs of the post. Here comes the promised super lightweight jQuery plugin which I called DoubleTapToGo.js and it is only 550 ridiculous bytes in size when minified.

When you tap the parent item for the first time, DoubleTapToGo prevents the browser from opening a new URL but allows that if tapped once again in succession. If there is an intermediate tap(s) between the first one and the second the counter resets. Quite simple principle of operation, isn't it? It may be hard to believe but this solved all of the flaws I have mentioned before.

Considering the markup above, the plugin should be only applied to the items that are parents– in order to avoid double-tap requirement on drop-down-less items:

1

$( '#nav li:has(ul)').doubleTapToGo();

This is it! Complete fulfillment, full code and working example – all in the demo: