Responsive Menu Tutorial for HTML Web Pages, No JQuery

HTML Menu Bar Which Switches to Dropdown for Mobile

For a web site to be successful it must tick several boxes. It needs an attractive design, great content, no pop-ups, not throw to many advertisements at visitors and work well on a variety of screen sizes. The last point is important as mobile devices are now the dominant platform for accessing the web. Achieving multi-screen support from a single web site saves time (no need to update the design more than once). Time is a precious resource for most people and businesses.

A web site needs to respond to the device it is being viewed on, hence the term responsive design. When a website uses a menu to allow the viewer to jump to other pages, that menu should work well on different screen sizes. This article is a responsive menu tutorial giving an example implementation of the Responsive Nav plugin. This provides a basic responsive web site menu without using any other frameworks or libraries. As the screen shrinks the menu switches from traditional horizontal to a button. The button is used to dropdown the menu on smaller screens.

A basic menu is achieved using the HTML tag for an unordered list, the <ul> tag. Each list item tag, <li>, in the unordered list wraps an anchor tag, <a>, to provide the link to the other resource or location. Here is an example of an unordered list with links to other items (in this case on the same web page):

For a menu this wastes space. On a desktop or wide screen (e.g. a tablet or phone held in landscape) there is redundant space, and the menu pushes content unnecessarily below the fold (the bottom of the browser or screen):

A Basic Web Site Menu

With the help of Cascading Style Sheets (CSS) the list item tags can transformed into a horizontal menu for more space efficiency, there are several ways to achieve this. The simplest is to set the <li> tag to display inline, some padding is added for a little spacing (add the style to the <head> section in the above example):

A simple responsive menu.

A similar effect is achieved by floating the <ul> and <li> elements:

On small screens and when the browser window is dragged small this horizontal menu will wrap, pushing the contents down. Using some JavaScript and a little more styling the menu can be closed to a single link. Clicking the link opens and closes the menu options. This is what the Responsive Nav plugin does.

To implement Responsive Nav download the Responsive Nav zip file, extract responsive-nav.css and responsive-nav.js and link them to the web file. Wrap the <ul> menu in a <nav> tag and give it a class name, here class=”nav-menu” is used. The Responsive Nav styling floats the menu elements. Finally at the end of  the page call the responsiveNav function passing in the given class name:

Simple Responsive Dropdown Menu

Responsive Nav supports several options, view the documentation and play with the examples to see what can be done. For example pass a string to the label setting to change the text for the default menu link:

Menu (Hamburger or Air Vent) IconIt is common for a menu to be indicated with a hamburger (menu) icon. Responsive Nav can replace the default text link to drop the menu with a custom element, like a button or img. See the demo code in the zip file.

A quick way of getting the hamburger icon is to use a similar unicode character and set that as the menu text. Unicode 2630 is appropriate, although it may not work on older devices:

Plus a little CSS:

hamburger-icon-menu-example

This tutorial did not do any fancy formatting with the menu, use CSS to do more styling. Some other interesting menu resources:

Leave a Reply

Your email address will not be published. Required fields are marked *

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.