Skip to content

Instantly share code, notes, and snippets.

@devluis
Last active June 9, 2021 14:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save devluis/a899448a605e6ee951d9 to your computer and use it in GitHub Desktop.
Save devluis/a899448a605e6ee951d9 to your computer and use it in GitHub Desktop.
Responsive Multilevel Horizontal Menu only with CSS
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Responsive Multilevel Horizontal Menu</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">
<style>
#menu ul {
margin: 0;
padding: 0;
}
#menu .main-menu {
display: none;
}
#tm:checked + .main-menu {
display: block;
}
#menu input[type="checkbox"],
#menu ul span.drop-icon {
display: none;
}
#menu li,
#toggle-menu,
#menu .sub-menu {
border-style: solid;
border-color: rgba(0, 0, 0, .05);
}
#menu li,
#toggle-menu {
border-width: 0 0 1px;
}
#menu .sub-menu {
background-color: #444;
border-width: 1px 1px 0;
margin: 0 1em;
}
#menu .sub-menu li:last-child {
border-width: 0;
}
#menu li,
#toggle-menu,
#menu a {
position: relative;
display: block;
color: #000000;
/*text-shadow: 1px 1px 0 rgba(0, 0, 0, .125);*/
}
#menu,
#toggle-menu {
background-color: #09c;
}
#toggle-menu,
#menu a {
padding: 1em 1.5em;
}
#menu a {
transition: all .125s ease-in-out;
-webkit-transition: all .125s ease-in-out;
}
#menu a:hover {
background-color: white;
color: #09c;
}
#menu .sub-menu {
display: none;
}
#menu input[type="checkbox"]:checked + .sub-menu {
display: block;
}
#menu .sub-menu a:hover {
color: #444;
}
#toggle-menu .drop-icon,
#menu li label.drop-icon {
position: absolute;
right: 1.5em;
top: 1.25em;
}
#menu label.drop-icon, #toggle-menu span.drop-icon {
border-radius: 50%;
width: 1em;
height: 1em;
text-align: center;
background-color: rgba(0, 0, 0, .125);
text-shadow: 0 0 0 transparent;
color: rgba(255, 255, 255, .75);
}
#menu .drop-icon {
line-height: 1;
}
@media only screen and (max-width: 64em) and (min-width: 52.01em) {
#menu li {
width: 33.333%;
}
#menu .sub-menu li {
width: auto;
}
}
@media only screen and (min-width: 52em) {
#menu .main-menu {
display: block;
}
#toggle-menu,
#menu label.drop-icon {
display: none;
}
#menu ul span.drop-icon {
display: inline-block;
}
#menu li {
float: left;
/*border-width: 0 1px 0 0;*/
}
#menu .sub-menu li {
float: none;
}
#menu .sub-menu {
border-width: 0;
margin: 0;
position: absolute;
top: 100%;
left: 0;
width: 12em;
z-index: 3000;
}
#menu .sub-menu,
#menu input[type="checkbox"]:checked + .sub-menu {
display: none;
}
#menu .sub-menu li {
border-width: 0 0 1px;
}
#menu .sub-menu .sub-menu {
top: 0;
left: 100%;
}
#menu li:hover > input[type="checkbox"] + .sub-menu {
display: block;
}
}
</style>
</head>
<body>
<nav id="menu">
<label for="tm" id="toggle-menu">Navigation <span class="drop-icon"></span></label>
<input type="checkbox" id="tm">
<ul class="main-menu clearfix">
<li><a href="#">Parent Menu 1</a></li>
<li><a href="#">Parent Menu 2
<span class="drop-icon"></span>
<label title="Toggle Drop-down" class="drop-icon" for="sm1"></label>
</a>
<input type="checkbox" id="sm1">
<ul class="sub-menu">
<li><a href="#">Item 2.1</a></li>
<li><a href="#">Item 2.2
<span class="drop-icon"></span>
<label title="Toggle Drop-down" class="drop-icon" for="sm2"></label>
</a>
<input type="checkbox" id="sm2">
<ul class="sub-menu">
<li><a href="#">Item 2.2.1</a></li>
<li><a href="#">Item 2.2.2</a></li>
<li><a href="#">Item 2.2.3</a></li>
</ul>
</li>
<li><a href="#">Item 3.4</a></li>
</ul>
</li>
<li><a href="#">Parent Menu 3</a></li>
</ul>
</nav>
</body>
</html>
@Hagay
Copy link

Hagay commented Dec 24, 2017

Good job, one issue:
On small screens each sub menu that opens, stays open even if another one has been selected.

@CBallard71
Copy link

Hi,

When I use this code all works great, however when view it on a mobile the arrow at the end of the text to show there is a menu under the text is within the text, is there a way of moving the arrow to the left of the text?
cheers

@devluis
Copy link
Author

devluis commented Jul 19, 2019

Hi,

When I use this code all works great, however when view it on a mobile the arrow at the end of the text to show there is a menu under the text is within the text, is there a way of moving the arrow to the left of the text?
cheers

Most likely, you have a style sheet that is causing this conflict, I tried the code and the arrow stays out of the text, do you have a screenshot?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment