I'm trying to create dropdown navigation with pure CSS and I want to show dropdown menu on ul click. Problem is that simple ul:focus > ul doesn't work even though there is anchor in it. Selectors :hover and :active are working without problems.
HTML
<li>Menu 1
<ul>
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
<li>Sub Menu 4
</ul>
</li>
CSS
#primary_nav_wrap ul li:hover > ul
{
display:block
}
Here's the code:
https://codepen.io/anon/pen/vgpQWV
Your selector needs to be #primary_nav_wrap ul li > a:focus + ul.
Breaking it down:
When the <a> directly inside the <li> has focus, apply these styles to the immediately adjacent <ul>.
Fixed example:
#primary_nav_wrap {
margin-top: 15px
}
#primary_nav_wrap ul {
list-style: none;
position: relative;
float: left;
margin: 0;
padding: 0
}
#primary_nav_wrap ul a {
display: block;
color: #333;
text-decoration: none;
font-weight: 700;
font-size: 12px;
line-height: 32px;
padding: 0 15px;
font-family: "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, sans-serif
}
#primary_nav_wrap ul li {
position: relative;
float: left;
margin: 0;
padding: 0
}
#primary_nav_wrap ul li.current-menu-item {
background: #ddd
}
#primary_nav_wrap ul li:hover {
background: #f6f6f6
}
#primary_nav_wrap ul ul {
display: none;
position: absolute;
top: 100%;
left: 0;
background: #fff;
padding: 0
}
#primary_nav_wrap ul ul li {
float: none;
width: 200px
}
#primary_nav_wrap ul ul a {
line-height: 120%;
padding: 10px 15px
}
#primary_nav_wrap ul ul ul {
top: 0;
left: 100%
}
#primary_nav_wrap ul li > a:focus + ul {
display: block
}
<h1>Testing menu</h1>
<nav id="primary_nav_wrap">
<ul>
<li class="current-menu-item">Home
</li>
<li>Menu 1
<ul>
<li>Sub Menu 1
</li>
<li>Sub Menu 2
</li>
<li>Sub Menu 3
</li>
<li>Sub Menu 4
<ul>
<li>Deep Menu 1
<ul>
<li>Sub Deep 1
</li>
<li>Sub Deep 2
</li>
<li>Sub Deep 3
</li>
<li>Sub Deep 4
</li>
</ul>
</li>
<li>Deep Menu 2
</li>
</ul>
</li>
<li>Sub Menu 5
</li>
</ul>
</li>
<li>Menu 2
<ul>
<li>Sub Menu 1
</li>
<li>Sub Menu 2
</li>
<li>Sub Menu 3
</li>
</ul>
</li>
<li>Menu 3
<ul>
<li class="dir">Sub Menu 1
</li>
<li class="dir">Sub Menu 2 THIS IS SO LONG IT MIGHT CAUSE AN ISSEUE BUT MAYBE NOT?
<ul>
<li>Category 1
</li>
<li>Category 2
</li>
<li>Category 3
</li>
<li>Category 4
</li>
<li>Category 5
</li>
</ul>
</li>
<li>Sub Menu 3
</li>
<li>Sub Menu 4
</li>
<li>Sub Menu 5
</li>
</ul>
</li>
<li>Menu 4
</li>
<li>Menu 5
</li>
<li>Menu 6
</li>
<li>Contact Us
</li>
</ul>
</nav>
Related
I have tried my best to do it myself, but I haven't succeeded. I want the categories (archive, topic etc.) to be able to disappear on my phone when i'm clicking on it the second time. So basically when I click on "Archive" etc. I want it to close again after opening it when I click on it. How do I do that?
My codes:
#primary_nav_wrap
{
margin-top:0;
width: 100%;
}
#primary_nav_wrap ul
{
list-style:none;
position:relative;
float:left;
margin:0;
padding:2.5% 0 0 0;
}
#primary_nav_wrap ul a
{
display:block;
color:#5e5e5e;
text-decoration:none;
font-size:70%;
line-height:32px;
padding:5px 15px;
font-family:'Source Sans Pro', sans-serif;
text-transform: uppercase;
letter-spacing: 3px;
}
#primary_nav_wrap ul li
{
position:relative;
float:left;
margin:0;
padding:0
}
#primary_nav_wrap ul li.current-menu-item
{
background:#FFF;
}
#primary_nav_wrap ul li:hover
{
background:none;
}
#primary_nav_wrap ul ul
{
display:none;
position:absolute;
top:100%;
left:0;
background:#fff;
padding:0;
margin:0;
border: 1px solid #eaeaea;
}
#primary_nav_wrap ul ul li
{
float:none;
width:200px;
}
#primary_nav_wrap ul ul a
{
line-height:120%;
padding:10px 15px;
letter-spacing: 1px !important;
}
#primary_nav_wrap ul ul ul
{
top:0;
left:100%
}
#primary_nav_wrap ul li:hover > ul
{
display:block
}
<nav id="primary_nav_wrap">
<ul>
<li class="current-menu-item">Topic</li>
<li>Archive
<ul>
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
<li>Sub Menu 4
<ul>
<li>Deep Menu 1
<ul>
<li>Sub Deep 1</li>
<li>Sub Deep 2</li>
<li>Sub Deep 3</li>
<li>Sub Deep 4</li>
</ul>
</li>
<li>Deep Menu 2</li>
</ul>
</li>
<li>Sub Menu 5</li>
</ul>
</li>
<li>Menu 2
<ul>
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
</ul>
</li>
<li>Menu 3
<ul>
<li class="dir">Sub Menu 1</li>
<li>Sub Menu 3</li>
<li>Sub Menu 4</li>
<li>Sub Menu 5</li>
</ul>
</li>
</ul>
</nav>
I'm trying to create a submenu in html/css. I already have a piece of code, but i would like to have a submenu in block below each menu item, but i can't figure out how to do it. Anyone would like to help?
.mainmenu {
margin-left: auto;
margin-right: auto;
}
.mainmenu ul {
width: 940px;
;
overflow: hidden;
font-family: 'swis721_btroman';
position: relative;
background-image: url(../img/01.png);
list-style: none;
font-size: 16px;
color: #000;
}
.mainmenu ul li {
float: left;
text-transform: uppercase;
font-family: 'swis721_btroman';
padding: 0 20px 0 20px;
display: table-column;
font-size: 16px;
color: #000;
}
.mainmenu ul li:first-child {
background: none;
}
.mainmenu ul li a {
float: left;
text-decoration: none;
padding: 9px 0 9px 0;
font-family: 'swis721_btroman';
font-weight: bold;
font-size: 16px;
color: #292F51;
}
.mainmenu ul li a:hover {
text-decoration: none;
color: #FFF;
overflow: hidden;
padding: 9px 9px 9px 9px;
position: relative;
background-color: #292F51;
}
.mainmenu ul ul {
list-style-type: none;
display: none;
}
.mainmenu ul:hover ul {
display: block;
position: relative;
}
<div class="mainmenu">
<ul>
<li class="operador chefe">
operador chefe
<ul>
<li>menu 1
</li>
<li>menu 2
</li>
<li>menu 3
</li>
<li>menu 4
</li>
<li>menu 5
</li>
</ul>
</li>
<li class="safety">
safety
<ul>
<li>menu 1
</li>
<li>menu 2
</li>
<li>menu 3
</li>
<li>menu 4
</li>
<li>menu 5
</li>
</ul>
</li>
<li class="security">
security
<ul>
<li>menu 1
</li>
<li>menu 2
</li>
<li>menu 3
</li>
<li>menu 4
</li>
<li>menu 5
</li>
</ul>
</li>
<li class="atendimento">
atendimento
<ul>
<li>menu 1
</li>
<li>menu 2
</li>
<li>menu 3
</li>
<li>menu 4
</li>
<li>menu 5
</li>
</ul>
</li>
<li class="apoio unidades comerciais">
apoio unidades comerciais
<ul>
<li>menu 1
</li>
<li>menu 2
</li>
<li>menu 3
</li>
<li>menu 4
</li>
<li>menu 5
</li>
</ul>
</li>
</ul>
</div>
Right now, when i hove over a menu item, all the submenu items appear at once, and in inline-block, which is not what i want.
You have two main problems here:
First this selector .mainmenu ul:hover ul it's wrong since is showing all ul elements when you hover the main ul.
Change to this:
.mainmenu li:hover > ul
Making visible just the sub ul element inside a li.
Also those submenus need to be absolute positioned so they don't disturb the layout of the upper menu:
.mainmenu ul li {
position:relative;
}
.mainmenu ul li > ul {
display: none;
position: absolute;
z-index:10;
}
Check this Snippet
.mainmenu ul {
padding:0;
width: 940px;
position: relative;
list-style: none;
font-size: 16px;
color: #000;
}
.mainmenu ul li {
float: left;
text-transform: uppercase;
padding: 0 20px 0 20px;
font-size: 16px;
color: #000;
position:relative;
}
.mainmenu ul li a {
display:block;
text-decoration: none;
padding: 9px 0 9px 0;
font-weight: bold;
font-size: 16px;
color: #292F51;
}
.mainmenu ul li a:hover {
text-decoration: none;
color: #FFF;
background-color: #292F51;
}
.mainmenu ul li > ul {
display: none;
position: absolute;
z-index:10;
}
.mainmenu li:hover > ul {
display: block;
}
<div class="mainmenu">
<ul>
<li class="operador chefe">
operador chefe
<ul>
<li>menu 1
</li>
<li>menu 2
</li>
<li>menu 3
</li>
<li>menu 4
</li>
<li>menu 5
</li>
</ul>
</li>
<li class="safety">
safety
<ul>
<li>menu 1
</li>
<li>menu 2
</li>
<li>menu 3
</li>
<li>menu 4
</li>
<li>menu 5
</li>
</ul>
</li>
<li class="security">
security
<ul>
<li>menu 1
</li>
<li>menu 2
</li>
<li>menu 3
</li>
<li>menu 4
</li>
<li>menu 5
</li>
</ul>
</li>
<li class="atendimento">
atendimento
<ul>
<li>menu 1
</li>
<li>menu 2
</li>
<li>menu 3
</li>
<li>menu 4
</li>
<li>menu 5
</li>
</ul>
</li>
<li class="apoio unidades comerciais">
apoio unidades comerciais
<ul>
<li>menu 1
</li>
<li>menu 2
</li>
<li>menu 3
</li>
<li>menu 4
</li>
<li>menu 5
</li>
</ul>
</li>
</ul>
</div>
This will work fine.
The most important rule is that when you hover a li it should show its child ul.
.main-menu,
.sub-menu {
font-size: 0px;
margin: 0px;
list-style: none;
padding: 0px;
font-family: 'Helvetica';
}
.sub-menu {
display: none;
}
.menu-item,
.sub-item {
background: navy;
color: white;
font-size: 14px;
text-align: center;
min-width: 80px;
padding: 4px;
}
.menu-item {
display: inline-block;
vertical-align: top;
}
.menu-item:hover,
.sub-item:hover {
background: blue;
}
.menu-item:hover > ul {
display: block;
}
<ul class="main-menu">
<li class="menu-item">Item 1</li>
<li class="menu-item">Item 2 (With sub-items!)
<ul class="sub-menu">
<li class="sub-item">Item 3</li>
<li class="sub-item">Item 4</li>
</ul></li>
<li class="menu-item">Item 5</li>
<li class="menu-item">Item 6 (With sub-items!)
<ul class="sub-menu">
<li class="sub-item">Item 7</li>
<li class="sub-item">Item 8</li>
</ul></li>
</ul>
I don't really like submenu coded from scratch (from zero). You can today create some menu "without" any CSS.
You can use Bootstrap class to realize menu like that:
http://jsfiddle.net/fm571dsd/4/
html:
<body>
<div class="container">
<div class="row">
<nav class="navbar navbar-default">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#" target="_blank">Logo</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="dropdown">
operador chefe
<ul class="dropdown-menu">
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
</li>
<li class="dropdown">
safety
<ul class="dropdown-menu">
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
</li>
<li class="dropdown">
security
<ul class="dropdown-menu">
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
</li>
<li class="dropdown">
atendimento
<ul class="dropdown-menu">
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
</li>
<li class="dropdown">
apoio unidades comerciais
<ul class="dropdown-menu">
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
</li>
</ul>
</div>
</nav>
</div>
</div>
</body>
js:
(function($){
$(document).ready(function(){
$('ul.dropdown-menu [data-toggle=dropdown]').on('click', function(event) {
event.preventDefault();
event.stopPropagation();
$(this).parent().siblings().removeClass('open');
$(this).parent().toggleClass('open');
});
});
})(jQuery);
I've got a nav bar configured to hide drop down elements (using display:none) until hovered over when it will show as a drop down (using display:block at a:hover). It all works great except the "divs" below it are pushed down when drop down displays. How can I get the "divs" to remain in place regardless of the display of the drop down items?
Thanks!
I hope it helps you
<h1>Simple Pure CSS Drop Down Menu</h1>
<ul class="nav">
<li>Menu1
<ul>
<li>Sub menu1</li>
<li>Sub menu2</li>
<li>Sub menu3</li>
<li>Sub menu4
<ul>
<li>Deep menu1</li>
<li>Deep menu2
<ul>
<li>Sub deep menu1</li>
<li>Sub deep menu2</li>
<li>Sub deep menu3</li>
</ul>
</li>
<li>Deep menu3</li>
</ul>
</li>
<li>Sub menu5</li>
</ul>
</li>
<li>Menu2
<ul>
<li>Sub menu1</li>
<li>Sub menu2</li>
<li>Sub menu3</li>
</ul>
</li>
<li>Menu3
<ul>
<li>Sub menu1</li>
<li>Sub menu2
<ul>
<li>Category1</li>
<li>Category2</li>
<li>Category3</li>
<li>Category4</li>
<li>Category5</li>
</ul>
</li>
<li>Sub menu3</li>
<li>Sub menu4</li>
<li>Sub menu5</li>
</ul>
</li>
</ul>
.nav {
margin: 15px 0 0;
}
.nav li {
display: inline-block;
width: 150px;
position: relative;
text-align: center;
}
.nav li:hover {
background: #f6f6f6;
}
.nav li a {
display: block;
padding: 10px 15px;
}
.nav ul {
display: none;
position: absolute;
}
.nav ul li {
display: block;
}
.nav li:hover ul {
display: block;
}
.nav li:hover ul li ul {
display: none;
}
.nav li ul li:hover ul {
display: block;
left: 100%;
top: 0;
}
.nav li ul li:hover ul li ul {
display: none;
}
.nav li ul li ul li:hover ul {
display: block;
}
fiddle
The <div> elements which contain the dropdown parts have to have position: absolute. That way they don't occupy any space in the surrounding containers. Tune their position n relation to their parent elements with the top and left (or right) settings.
The parent elements of those DIVs need to have position: relative to enable the absolute positioning of their children elements.
By the way, position: fixed wouldn't work that well, since this positions an element absolutely to the window, which in most cases you don't want (exception: if you have a fixed navbar).
I need your help.
How can the CSS markup below be modified such that the color white is present thoughout the users selection? As it is now, when a section has been hovered, the text remains blue except for the last part selected. Notice that the blue color is not white throughout the user's selection.
See a picture below:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<style type="text/css">
#navigation {
width: 150px;
font-size: 0.75em;
}
#navigation ul {
margin: 0px;
padding: 0px;
}
ul.top-level {
background: rgb(238,238,238);
}
#navigation li {
list-style: none;
}
ul.top-level li {
border-bottom: #fff solid;
border-top: #fff solid;
border-width: 1px;
}
#navigation a {
color: rgb(41,83,118);
cursor: pointer;
display:block;
height:25px;
line-height: 25px;
text-indent: 10px;
text-decoration:none;
width:100%;
}
#navigation a:hover{
text-decoration:underline;
color: #fff;
}
#navigation li:hover {
background: rgb(85,85,85);
position: relative;
}
ul.sub-level {
display: none;
}
li:hover .sub-level {
border: #fff solid;
border-width: 1px;
display: block;
position: absolute;
left: 150px;
top: 0px;
}
ul.sub-level li {
border: none;
float:left;
width:150px;
}
#navigation .sub-level {
background: rgb(238,238,238);
}
#navigation .sub-level .sub-level {
background: rgb(238,238,238);
}
li:hover .sub-level .sub-level {
display:none;
}
.sub-level li:hover .sub-level {
display:block;
position: absolute;
left: 150px;
}
</style>
</head>
<body>
<div id="navigation">
<ul class="top-level">
<li>Home
<ul class="sub-level">
<li>Sub Menu Item 1
</li>
<li>
Sub Menu Item 2
<ul class="sub-level">
<li>Sub Sub Menu Item 1</li>
<li>Sub Sub Menu Item 2</li>
<li>Sub Sub Menu Item 3</li>
<li>Sub Sub Menu Item 4</li>
</ul>
</li>
<li>Sub Menu Item 3</li>
<li>Sub Menu Item 3</li>
</ul>
</li>
<li>About</li>
<li>Contact</li>
<li>FAQ
<ul class="sub-level">
<li>Sub Menu Item 1</li>
<li>
Sub Menu Item 3
<ul class="sub-level">
<li>Sub Sub Menu Item 1</li>
<li>Sub Sub Menu Item 2</li>
<li>Sub Sub Menu Item 3</li>
<li>Sub Sub Menu Item 4</li>
</ul>
</li>
</ul>
</li>
<li>
News
<ul class="sub-level">
<li>Sub Menu Item 1
<ul class="sub-level">
<li>Sub Sub Menu Item 1</li>
<li>Sub Sub Menu Item 2</li>
<li>Sub Sub Menu Item 3</li>
<li>Sub Sub Menu Item 4</li>
</ul>
</li>
<li>Sub Menu Item 2</li>
<li>Sub Menu Item 3</li>
</ul>
</li>
</ul>
</div>
</body>
</html>
Change #navigation a:hover to #navigation li:hover > a. The <a> is not parents of the sublist. You will remain hovered over the list while hovered over the sublist. The child selector (>) is necessary or else all a descendants will be white in the sublists which is undesirable.
http://jsfiddle.net/nvEza/
Add the following at the end of your css: (Working jsFiddle)
#navigation li:hover > a{ /* Thanks #ExplosionPills */
color:white;
}
I have seen it where the anchor wraps the entire sub-menu, like so:
<a href="#">
News
<ul class="sub-level">
...
</ul>
</a>
This way, when you are hovering over a sub-menu, the parent anchor is still active, as well.
Use the hover on the parent to set the child colors in that state.
#navigation .top-level:hover li a{color:#fff;}
I did my research and was able to replicate what I'm looking for, well kind of--I just need help with a more specific vertical, pure CSS, menu.
I want my sub-menu popup to appear 10px to the left of the a not the li attribute like most example found on the internet. I'm also looking for the most simple, pure CSS, type of menu--Nothing fancy.
Here's what I've done so far:
<div id="nav">
<ul class="top-level">
<li>This is a long text
<ul class="sub-level">
<li>Sub Menu Item 1</li>
<li>Sub Menu Item 2</li>
<li>Sub Menu Item 3</li>
<li>Sub Menu Item 3</li>
</ul>
</li>
<li>About</li>
<li>Contact me here</li>
<li>Help
<ul class="sub-level">
<li>Sub Menu Item 1</li>
<li>Sub Menu Item 2</li>
<li>Sub Menu Item 3</li>
</ul>
</li>
</ul>
my css:
#nav {border:1px solid cyan;}
/* top level menu */
#nav ul.top-level {border:1px solid red;}
#nav ul.top-level li {position:relative;}
/* sub level menu */
#nav ul.sub-level {border:1px solid yellow;}
#nav ul.sub-level {display:none;} /* hide */
/* hover the sub menu*/
#nav ul.top-level li:hover .sub-level {display: block; position:absolute; top:5px;}
How do I make it so the sub level menu pops up when I hover the a HTML anchor, not the li, and 10px to the left of the clicked a anchor?
Thanks.
Try this one and I think it will help
HTML
<div id="nav">
<ul class="top-level">
<li>This is a long text
<ul class="sub-level">
<li>Sub Menu Item 1</li>
<li>Sub Menu Item 2</li>
<li>Sub Menu Item 3</li>
<li>Sub Menu Item 3</li>
</ul>
</li>
<li>About</li>
<li>Contact me here</li>
<li>Help
<ul class="sub-level">
<li>Sub Menu Item 1</li>
<li>Sub Menu Item 2</li>
<li>Sub Menu Item 3</li>
</ul>
</li>
</ul>
</div>
CSS
#nav {font-size:0.75em; width:150px;}
#nav ul {margin:0px; padding:0px;}
#nav li {list-style: none;}
ul.top-level {background:#FFFFFF;}
ul.top-level li {
border: #FF0000 solid;
border-width: 1px;
}
#nav ul.sub-level {border:1px solid yellow;}
#nav a {
color: #000000;
cursor: pointer;
display:block;
height:25px;
line-height: 25px;
text-indent: 10px;
text-decoration:none;
width:100%;
}
#nav a:hover{
text-decoration:underline;
}
#nav li:hover {
background: #f90;
position: relative;
}
ul.sub-level {
display: none;
}
li:hover .sub-level {
background: #999;
border: #fff solid;
border-width: 1px;
display: block;
position: absolute;
left: 75px;
top: 5px;
}
ul.sub-level li {
border: none;
float:left;
width:150px;
}
#nav .sub-level {
background: #FFFFFF;
}