Professional Documents
Culture Documents
Creating a
Dropdown Menu
with HTML & CSS
31st July, 2014
Reading Time 6 minutes
157 Comments
View Demo
Take a look at the demo to test it out and see what the end
result looks like. The below HTML and CSS dropdown menu
relies on z-index for some of the functionality, which can
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 1/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
Table of Contents
1. Skip to the HTML
1 HTML Structure
Finished HTML
Advertisement
1 <div class="menu-wrap">
2 <nav class="menu">
3 <ul class="clearfix">
4 <li><a href="#">Home</a></li>
5 <li>
6 <a href="#">Movies <span class="arrow">▼</span></a>
The IP
7 <ul class="sub-menu">
8 <li><a href="#">In Cinemas Now</a></li> Newsletter
9 <li><a href="#">Coming Soon</a></li>
10 <li><a href="#">On DVD/Blu-ray</a></li> By subscribing to
11 <li><a href="#">Showtimes & Tickets</a></li> the Inspirational
12 </ul> Newsletter you’ll
13 </li> get 3 exclusive
14 <li><a href="#">T.V. Shows</a></li> freebies and a
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 2/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
Ek Mukta Font
If you would like to use the same font as I’ve used you need
a link to it in the header.
1 <div class="menu-wrap">
2 <nav class="menu">
3 <!-- NAV -->
4 </nav>
5 </div>
Advertisement
1 <ul class="clearfix">
2 <li><a href="#">Home</a></li>
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 3/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
2 CSS Styling
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 4/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
Finished CSS
1 body {
Web
Hosting
2 background:#bf5c71 url('body-bg.jpg'); for $5
3 } with
4 .clearfix:after {
Media
Temple
5 display:block;
Media
6 clear:both; Temple
7 } Shared
8 /*----- Menu Outline -----*/ Hosting
9 .menu-wrap { Advertisements
10 width:100%;
11 box-shadow:0px 1px 3px rgba(0,0,0,0.2);
12 background:#3e3436;
13 }
14 .menu {
15 width:1000px;
Let's Be
16 margin:0px auto;
17 } Social
18 .menu li {
19 margin:0px; Follow us, like us
20 list-style:none; and +1 us to
21 font-family:'Ek Mukta'; receive exclusive
22 } downloads and
23 .menu a { freebies!
24 transition:all linear 0.15s;
25 color:#919191; Alternatively you
26 } can subscribe via
27 .menu li:hover > a, .menu .current-item > a { RSS if you’re a
28 text-decoration:none; little more old
29 color:#be5b70; school.
30 }
31 .menu .arrow {
32 font-size:11px;
33 line-height:0%;
34 }
35 /*----- Top Level -----*/
36 .menu > ul > li {
37 float:left;
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 5/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
38 display:inline-block;
39 position:relative;
40 font-size:19px;
41 }
42 .menu > ul > li > a {
43 padding:10px 40px;
44 display:inline-block;
45 text-shadow:0px 1px 0px rgba(0,0,0,0.4);
46 }
47 .menu > ul > li:hover > a, .menu > ul > .current-item > a {
48 background:#2e2728;
49 }
50 /*----- Bottom Level -----*/
51 .menu li:hover .sub-menu {
52 z-index:1;
53 opacity:1;
54 }
55 .sub-menu {
56 width:160%;
57 padding:5px 0px;
58 position:absolute;
59 top:100%;
60 left:0px;
61 z-index:-1;
62 opacity:0;
63 transition:opacity linear 0.15s;
64 box-shadow:0px 2px 3px rgba(0,0,0,0.2);
65 background:#2e2728;
66 }
67 .sub-menu li {
68 display:block;
69 font-size:16px;
70 }
71 .sub-menu li a {
72 padding:10px 30px;
73 display:block;
74 }
75 .sub-menu li a:hover, .sub-menu .current-item a {
76 background:#3e3436;
77 }
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 6/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
Base Styling
We start the CSS with some base styling by setting a page
background color and setting up a clearfix. Don’t worry if
you don’t have the background image it’s only a simple
texture. You can grab it from the demo if needed.
1 body {
2 background:#bf5c71 url('body-bg.jpg');
3 }
4 .clearfix:after {
5 display:block;
6 clear:both;
7 }
Menu Setup
Now we need to set up the outline of the entire menu and
style a few ‘global’ elements (two or more elements with
the same styling).
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 7/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
12 margin:0px;
13 list-style:none;
14 font-family:'Ek Mukta';
15 }
16 .menu a {
17 transition:all linear 0.15s;
18 color:#919191;
19 }
20 .menu li:hover > a, .menu .current-item > a {
21 text-decoration:none;
22 color:#be5b70;
23 }
Dropdown Arrow
The arrow by default is quite large so we make the font-
1 .menu .arrow {
2 font-size:11px;
3 line-height:0%;
4 }
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 8/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
element.
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 9/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
The rest is the styling of the sub menu list items and
anchor links.
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 10/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
1 .sub-menu li {
2 display:block;
3 font-size:16px;
4 }
5 .sub-menu li a {
6 padding:10px 30px;
7 display:block;
8 }
9 .sub-menu li a:hover, .sub-menu .current-item a {
10 background:#3e3436;
11 }
Conclusion
Some of the CSS used may be a little hard to wrap your
head around at first, especially if you’re a beginner. If you
need help in any way create a JSFiddle of your code and
post the link to it in the comments.
Follow IP on Twitter
Like IP on Facebook
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 11/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
Advertisement
Inspirational Newsletter
Join the newsletter to get the best articles, tutorials
and exclusive freebies every two weeks. As a
subscriber you get an eBook on productivity, a
WordPress theme and a social sharing button PSD.
Find out more →
Subscribe!
Don’t worry, your email is 100% safe from spam! (Privacy Policy)
Related Posts
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 12/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
Leave a Comment
I really appreciate you taking the time to leave a comment!
If you'd like to post code, you can do so by putting your
code on a new line and wrapping it in the appropriate tags.
For example, to add HTML you would put the code between
[html] and [/html] .
* Your Name...
Website URL...
* Your Comment...
Comments Pingbacks
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 13/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
Thank you!
Best
Hi,
I tied to put the navigation bar in the center but failed to do
so. I have try to use padding 0 30% but when I resize my
window the navigation won’t be in the center. tried to use
text-align and margin 0 auto, all not working. appreciate
anyone can help.
Thanks.
Hello,
Thank you for taking the time to show us how to create this.
I am an elementary school teacher who is trying to learn to
code on my own. I am not happy with any of the websites
out there that I have found to use for my classroom. I have
decided to take it upon myself to learn to code and create a
website that all teachers can use and love.
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 15/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
But I’d like to ask, can you please show me how to make a
big title at the top-left corner of the website, just above the
drop-down menu?
Thanks again,
Tony
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 16/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
1 <!doctype html>
2 <html>
3 <head>
4 <meta charset="UTF-8">
5 <meta name="description" content="Earth, Wind, Fire & Wate
6 <title>Earth, Wind, Fire & Water</title>
7 <link rel="stylesheet" href="../../Xavier website styles.css">
8 </head>
9 <body>
10 <div>
11 <header>
12 <h1><a href="../../Xavier home:portfolio page.html">Xavier King
13 </header>
14 <div class="menu-wrap">
15 <nav class="menu">
16 <ul class="clearfix">
17 <li><a href="../../Xavier home:portfolio page.html">Portfolio&
18 <li>
19 <a>Projects <span class="arrow">▼</span></a> <ul class="s
20 <li><a href="../freedom/freedom.html">Are We Free?</a></li>
21 <li><a href="../Ben Hart/Ben Hart.html">Ben Hart</a></li>
22 <li><a href="../Daisy Bertenshaw/Daisy Bertenshaw.html">Daisy
23 <li><a href="../Decay/Decay.html">Decay</a></li>
24 <li><a href="../Earth Wind FIre and Water/Earth, Wind, FIre and
25 <li><a href="../Frozen In Time/Frozen In Time.html">Frozen In T
26 <li><a href="../The CCTV's Eye/The CCTV's Eye.html">The CCTV's
27 </ul> <li><a href="../../Xavier CV page.html">  
28 </li>
29 </ul>
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 17/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
30 </nav>
31 </div>
32 <h2>Earth, Wind, Fire, Water</h2>
33 <img class="mySlides" src="b&r air.jpg" height="500" alt="B &am
34 <img class="mySlides" src="b&y air.jpg" width="" height="500" a
35 <img class="mySlides" src="y&r air.jpg" width="" height="500" a
36 <img class="mySlides" src="b&r earth.jpg" width="" height="500
37 <img class="mySlides" src="b&y earth.jpg" width="" height="500
38 <img class="mySlides" src="y&r earth.jpg" width="" height="500
39 <img class="mySlides" src="b&r fire.jpg" width="" height="500"
40 <img class="mySlides" src="b&y fire.jpg" width="" height="500"
41 <img class="mySlides" src="y&r fire.jpg" width="" height="500"
42 <script>
43 var slideIndex = 0;
44 carousel();
45 function carousel() {
46 var i;
47 var x = document.getElementsByClassName("mySlides");
48 for (i = 0; i < x.length; i++) {
49 x[i].style.display = "none";
50 }
51 slideIndex++;
52 if (slideIndex > x.length) {slideIndex = 1}
53 x[slideIndex-1].style.display = "block";
54 setTimeout(carousel, 2000);
55 }
56 </script>
57 </div>
58 </body>
59 </html>
1 @charset "UTF-8";
2 /* CSS Document */
3 body{
4 width: 100%;
5 margin: 0, auto;
6 }
7 body {
8 min-width: 100px;
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 18/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
9 }
10 body {
11 background-color:rgba(255,255,255,1.00)
12 }
13 body, h1, h2, h3, p{
14 font-family: "Verdana",sans-serif;
15 }
16 h1{
17 font-size: 3em;
18 text-align: center;
19 font-weight:lighter;
20 position:relative;
21 margin-top: 0px;
22 margin-bottom: 0px;
23 }
24 h2, h3 {
25 font-weight: normal;
26 }
27 h2{
28 font-size: 1.3em;
29 text-align:fixed;
30 margin-right: 600px;
31 margin-bottom: 0px;
32 margin-top: 0px;
33 margin-left: 339px;
34 font-family: "Verdana",sans-serif;
35 }
36 h3, p {
37 font-size: 1em;
38 font-family: 100% "Verdana",sans-serif;
39 margin-left: 339px
40 }
41 ul{
42 font-size: 1.2em;
43 }
44 ul{
45 list-style-type: none;
46 text-align: center;
47 }
48 a, li, h2, h3, p{
49 text-decoration: none;
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 19/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
50 color: rgba(128,128,128,1.00)
51 }
52 img{
53 margin-top: 0px;
54 }
55 li{
56 margin-top: 0px;
57 }
58 header{
59 margin-top: 0px;
60 }
61 menu{
62 margin-top: 0px;
63 margin-bottom: 0px;
64 }
65 img {
66 width:inherit;
67 max-height:500px;
68 position:inherit;
69 object-fit: contain;
70 }
71 div{
72 width:100%;
73 margin-bottom: 0px;
74 margin-top: 0px;
75 border: 0px solid #FFFFFF;
76 position: relative;
77 }
78 .menu-wrap{
79 position: relative;
80 width: 1000px;
81 margin-top: 0px;
82 margin-bottom: 0px;
83 }
84 /* drop down menu */
85 .clearfix:after {
86 display:inherit;
87 clear:both;
88 margin-bottom: 0px;
89 }
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 20/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
90 .menu {
91 width: 720px;
92 min-width: 720px;
93 margin-left:259px;
94 }
95 .menu li {
96 margin: 0px;
97 list-style:none;
98 font-family:"Verdana",sans-serif;
99 }
100 .menu a {
101 transition:all linear 0.15s;
102 color:rgba(135,135,135,1.00);
103 }
104 .menu li:hover > a, .menu .current-item > a {
105 text-decoration:none;
106 color:rgba(135,135,135,1.00);
107 }
108 .menu .arrow {
109 font-size:.5em;
110 line-height:0%;
111 }
112 .menu > ul > li {
113 float:left;
114 display:inline-block;
115 position: relative;
116 font-size:19px;
117 }
118 .menu > ul > li > a {
119 padding:10px 40px;
120 display:inline-block;
121 }
122 .menu > ul > li:hover > a, .menu > ul > .current-item > a {
123 background:#DCDADA ;
124 }
125 .menu li:hover .sub-menu {
126 z-index:6;
127 opacity:2;
128 }
129 .sub-menu {
130 width: 171px;
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 21/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 22/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
Hi
And also I have added a few more menus at the end but
when I view it, these extra menu item are wrapped round
and appear on the next line,
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 23/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 24/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
.clearfix::after {
content: “”;
clear: both;
display: block;
}
yes u can
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 25/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
Hey,
I like this menu but would prefer the sub-menu to open and
close on click rather than hover. At present the sub-menu
tends to open when I’m trying to open a menu below. NB
I’m working offline. The current menu on my site will be
replaced.
James
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 27/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
https://jsfiddle.net/5zg9zd5h/
Hi Mr Kay
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 28/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
help.
Please find the code
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 29/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
18 top:100%;
19 left:0px;
20 z-index:-1;
21 opacity:0;
22 transition:opacity linear 0.15s;
23 box-shadow:0px 2px 3px rgba(0,0,0,0.2);
24 background:#2e2728;
25 }
26 .sub-menu li {
27 display:block;
28 font-size:16px;
29 }
30 .sub-menu li a {
31 padding:10px 30px;
32 display:block;
33 }
34 .sub-menu li a:hover, .sub-menu .current-item a {
35 background:#3e3436;
36 }
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 30/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
cheese
watermelon
Another great resource for learning basic HTML and
CSS coding is w3schools.com.
Dear Colleague,
I am having difficulty using the dropdown html+css code
you posted. The code you posted are separate in two so how
to merge them in to one code listing? should I add the CSS
code right after completing the HTML code. That is to say
after the ? and where shoud I add also the link
background?
do you have the complete one list code if yes could you
send as attached file? I really appreciate your time and help
that will be given to me
Best regards
Zacharie
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 31/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
In CSS it says:
.clearfix:after {
display:block;
clear:both;
}
Change it to:
.clearfix:after {
display:block;
clear:both;
content:’ ‘;
}
Hello, can you help me with the CSS program for a sub-sub-
menu? like a cascading drop down Because i have been
trying it for 2 days now and i cant make it work..
Advertisement
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 32/33
3/13/2018 Creating a Dropdown Menu with HTML & CSS – Inspirational Pixels
© 2013 - 2018 Seb Kay. All rights reserved. Proudly powered by (mt).
Archives / Contact Us / Terms of Use / Privacy Policy / Back to Top ↑
http://inspirationalpixels.com/tutorials/creating-a-dropdown-menu-with-html-css 33/33