Tek Eye Logo

Tek Eye

Pure CSS Menu Examples, Includes Dropdown

On a multi-page website a menu can help visitors find other content. A menu allows different categories of web pages to be organised under different sections. Basic website menus can be implemented as a HTML list and anchor (link) tags. In an unordered list, <ul>, each list item tag, <li>, wraps an anchor, <a>, tag. This provides a list of links to the other HTML resources on a website, or other web page locations:

Simple HTML menu

Here is the code for the above simple website page with a menu:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Menu Test</title>
    </head>
    <body>
        <h1>Single unordered list</h1>
        <nav> <!-- nav tag is used to indicated a navigation section -->
            <ul>
                <li><a href="/">Home</a></li>
                <li><a href="/about">About</a></li>
                <li><a href="/projects">Projects</a></li>
                <li><a href="/blog">Blog</a></li>
            </ul>
        </nav>
    </body>
</html>

Adding some JavaScript and/or Cascading Style Sheets (CSS) to a web page allows for better formating of a list based menu. There are plenty of examples of HTML menus on the Internet that have fancy animations, colors and effects using Javascript and CSS. However, at Tek Eye the philosophy is to keep things simple. In this tutorial a pure CSS menu is used to provide a dropdown menu for HTML pages. The result will be similar to this:

Example Dropdown Menu

The CSS used to achieve the above will not use JavaScript, which means no jQuery either, simplifing the website code. If you want to jump straight into the completed CSS code, download the stylesheet for the pure CSS menu code used in this tutorial. It is available as a single CSS file, css-menu.css in css-menu.zip. See the code running on a CSS menu test page.

Try a Web Page with a Simple CSS Menu

Try out a simple CSS menu to start. First create a HTML file in a text editor (e.g. Windows Notepad or Notepad++) or in development environment (e.g. Visual Studio Code). Create the simple menu similar to the HTML code above. Save the file and run it in a browser to see the result.

Now create a .css file that will be used to change the format of the menu from vertical to horizontal, similar to the CSS code in the responsive hamburger menu. Here, the file is called css-menu.css. Using display:inline-block achieves the horizontal, along with some spacing using padding-right:1em:

/* Change unordered list items in a nav to inline */
nav ul li {display:inline-block; padding-right: 1em;}

Link the stylesheet to the HTML file:

...
    <meta charset="utf-8" />
    <title>Menu Test</title>
    <link rel="stylesheet" href="css-menu.css">
</head>
<body>
...

Test the horizontal menu in the browser:

Example Horizontal Menu

Add Another List for the Dropdown

Another unordered list is added under a <li> element which will become the drop down menu:

...
<nav> <!-- nav tag is used to indicated a navigation section -->
    <ul>
        <li><a href="/">Home</a></li>
        <li><a href="/about">About</a>
            <ul>
                <li><a href="/about/contact">Contact</a></li>
                <li><a href="/about/faqs">FAQs</a></li>
            </ul>
        </li>
        <li><a href="/projects">Projects</a></li>
        <li><a href="/blog">Blog</a></li>
    </ul>
</nav>
...

This new list will mess up the nice horizontal menu. Therefore, the CSS is changed to only affect the first child list of the nav element. This ensures the new list remains vertical:

nav > ul > li {display:inline-block; padding-right: 1em;}

It also needs to be positioned so it falls under the parent list item using position:absolute, add this line to the CSS:

nav ul li ul {position:absolute;}

Here is the result:

Horizontal and Vertical Menu

Completing the Pure CSS Menu

Notice that the horizontal list, and the dropdown, have some padding before them. Plus the dropdown has bullet points. These are removed by adding this line of CSS:

nav ul {padding:0;list-style:none;}

This is the result:

List Menu No Padding

The dropdown list is then hidden until the second horizontal item is hovered over by adding these lines of CSS:

nav ul li ul {visibility: hidden;}
nav ul li:hover > ul {visibility: visible;}

The menu can now be formatted for the desired look.

Setting the CSS Menu Colors

Here is a formatted horizontal menu and dropdown menu:

CSS Menu

The full css-menu.css for this example is:

/* Change unordered list items in a nav to inline */
nav > ul > li {display:inline-block; padding-right: 1em;}
/* Drop menu under its parent */
nav ul li ul {position:absolute;}
/* Remove lsit padding and bullet points */
nav ul {padding:0; list-style:none;}
/* Dropdown list invisible until... */
nav ul li ul {visibility:hidden;}
/* .. the parent list is hovered over */                  
nav ul li:hover > ul {visibility:visible;}
/* Format the menu's look */
nav li {background:grey;padding:6pt;}
nav a {text-decoration:none;color:white;}
nav li:hover {background:blue;}
nav ul li ul li {margin-top:3pt;}

The full HTML file for the example is:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>CSS Menu Development Test</title>
        <link rel="stylesheet" href="css-menu.css">
    </head>
    <body>
        <h1>One horizontal and one vertical list.</h1>
        <nav> <!-- nav tag is used to indicated a navigation section -->
            <ul>
                <li><a href="/">Home</a></li>
                <li><a href="/about">About</a>
                    <ul>
                        <li><a href="/about/contact">Contact</a></li>
                        <li><a href="/about/faqs">FAQs</a></li>
                    </ul>
                </li>
                <li><a href="/projects">Projects</a></li>
                <li><a href="/blog">Blog</a></li>
            </ul>
        </nav>
    </body>
</html>

A Simple Dropdown Indicator

There is a simple way to add an indicator to a menu item to show that it has a dropdown sub-menu. Modern browsers have access to many named HTML entity character references that provide hundreds of extra symbols. Here are some of the symbols that can be used as a drop down indicator:

  • ▽ - bigtriangledown
  • ▾ - blacktriangledown
  • ⊽ - barvee
  • ↡ - Darr
  • ⇓ - dArr
  • ↓ - darr
  • ⇓ - DoubleDownArrow
  • ↓ - DownArrow
  • ⇓ - Downarrow
  • ↓ - downarrow
  • ⤓ - DownArrowBar
  • ↓ - ShortDownArrow
  • ⋁ - Vee
  • ∨ - vee
  • ⊻ - veebar
  • ▽ - xdtri

Here is &ShortDownArrow; added to the menu as a dropdown indicator:

CSS Menu with Indicator

Here's the list item with the indicator in the code:

...
<li><a href="/about">About &ShortDownArrow;</a>
...

Play with the CSS code to make you own HTML dropdown menus.

See Also

Author:  Published:  

ShareSubmit to TwitterSubmit to FacebookSubmit to LinkedInSubmit to redditPrint Page

Do you have a question or comment about this article?

(Alternatively, use the email address at the bottom of the web page.)

markdown CMS Small Logo Icon ↓markdown↓ CMS is fast and simple. Build websites quickly and publish easily. For beginner to expert.


Articles on:

Android Programming and Android Practice Projects, HTML, VPS, Computing, IT, Computer History, ↓markdown↓ CMS, C# Programming, Using Windows for Programming


Free Android Projects and Samples:

Android Examples, Android List Examples, Android UI Examples



Tek Eye Published Projects