Drop-down menus in HTML and CSS

This tutorial explains how to create a drop-down menu on your website using HTML and CSS code. The drop-down menu will look like the example, but you can personalise it as you like.

dropdown

To create a drop-down menu using the method in this tutorial, you will need to create a div that contains a button as well as another div which contains the drop-down menu. The drop-down menu will be hidden until the mouse cursor hovers over the main button.

Step 1 – HTML code

Create a new HTML file and copy the code below. Each drop-down menu button will be contained inside a div with a class name of ‘dropdown_div‘. The button will have a class name of ‘dropdown_button‘ and the actual drop-down menu will be inside a div called ‘dropdown_content‘.

Change the text on the button as well as the links and link text inside the drop-down menu to match the links on your site. You can change the links by replacing the # hashtags with your own links eg. about.html or products.html. There are three drop-down menus below. You can add more or remove some if you wish. Once you have set up the links, save the HTML file.

Note that this HTML file is linked to an external CSS stylesheet called ‘theme.css‘ which will contain the CSS code for the menu.

HTML Code:

<!DOCTYPE html>
<html>
<head>
  <title>Drop-down menu</title>
  <link rel="stylesheet" href="theme.css" type="text/css"/>
</head>
<body>

<h2>Dropdown menu example</h2>
<p>This is an example of a drop-down menu. Hover your mouse cursor over the button to reveal the dropdown menu. You can add more buttons and drop-down menus by modifying the HTML and CSS code. Replace the link text and the # hashtags with the actual links.</p>

<div class="dropdown_div">
  <button class="dropdown_button">Dropdown button</button>
  <div class="dropdown_content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>


<div class="dropdown_div">
  <button class="dropdown_button">Dropdown button 2</button>
  <div class="dropdown_content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>


<div class="dropdown_div">
  <button class="dropdown_button">Dropdown button 3</button>
  <div class="dropdown_content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>

</body>
</html>

Step 2 – CSS code

Create a new CSS file called ‘theme.css‘ and save it in the same folder as the HTML file. Copy the code below and change the colours, font size and family properties, and other properties as you wish. Save your work.

CSS Code:

.dropdown_div {
    position: relative;
    display: inline-block;
}

.dropdown_button {
    background-color: #5961D4; /* button background colour */
    color: #ffffff;
    padding: 14px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

.dropdown_content {
    display: none; /* remove this to show all links before hovering */
    position: absolute;
    background-color: #f9f9f9; /* dropdown items - background colour */
    width: 100%; /* can remove this if you want to make the menu wider/narrower  */
    /* min-width: 180px; can add this if you need to change the minimu width of menu */
    box-shadow: 0px 6px 14px 0px rgba(0,0,0,0.3);
}

.dropdown_content a {
    font-family:Arial;
    color: #000000;
    padding: 12px 18px;
    text-decoration: none;
    display: block;
}

.dropdown_content a:hover {
    background-color: #9594A1; /* dropdown items hover - change background colour */
}

.dropdown_div:hover .dropdown_content {
    display: block;
}

.dropdown_div:hover .dropdown_button {
    background-color: #492FA1; /* button hover - change background colour */
}

p{
    font-family:Arial;
}

h2{
    font-family:Arial;
}

Make sure you have saved both files and then open the HTML file in your web browser. That’s it! You should have a nice drop-down menu on your page :-)