Issues with custom navigation and displaying submenu on hover - html

I've been trying to get this menu system to work for a good while now and am after looking at a number of different solutions but nothing seems to be working for me.
At the moment the submenu is appearing when hovering over the top level menu but if I try to move the mouse down to the submenu it disappears again as soon as my mouse leaves the top level.
Also when it does show it is showing behind some of the other page content, I think his might be because I have the page header which contains the navigation in a seperate include() file.
Any help with this would be greatly appreciated, my code is as follows:
HTML:
<div id="nav">
<ul>
<li>Menu
<ul>
<li>category</li>
<li>category</li>
<li>category</li>
</ul></li>
<li>About Us</li>
<li>Contact Us</li>
</ul>
<ul>
<li>Hello,</li>
<li>Sign in</li>
<li>country</li>
<li>cart</li>
</ul>
</div>
CSS:
#nav{
height:50px;
}
#nav > ul{
list-style:none;
padding:0px;
margin:0px;
vertical-align:top;
}
#nav > ul:nth-child(1){
float:left;
height:50px;
line-height:50px;
font-weight:bold;
}
#nav > ul:nth-child(2){
float:right;
height:50px;
line-height:50px;
}
#nav > ul > li {
display: inline-block;
vertical-align:top;
}
#nav > ul:nth-child(1) > li{
width:100px;
}
#nav > ul:nth-child(2) > li{
padding-left:10px;
}
#nav > ul > li > ul{
display:none;
}
#nav > ul > li:hover > ul{
display:block;
z-index:5;
padding:0px;
margin:0px;
width:200px;
}
#nav > ul > li:hover > ul > li{
background: #fff;
border:1px solid #bbb;
display:block;
padding:10px;
line-height:16px;
font-weight:normal;
}
#nav > ul > li:hover > ul > li > a{
color:#333;
}

You are almost done, you need to set a z-index, and position: relative. This is because z-index needs to be set with a position (more information here and here).
#nav{
height:50px;
}
#nav > ul{
list-style:none;
padding:0px;
margin:0px;
vertical-align:top;
}
#nav > ul:nth-child(1){
float:left;
height:50px;
line-height:50px;
font-weight:bold;
}
#nav > ul:nth-child(2){
float:right;
height:50px;
line-height:50px;
}
#nav > ul > li {
display: inline-block;
vertical-align:top;
}
#nav > ul:nth-child(1) > li{
width:100px;
}
#nav > ul:nth-child(2) > li{
padding-left:10px;
}
#nav > ul > li > ul{
display:none;
}
#nav > ul > li:hover > ul{
display:block;
z-index:5;
padding:0px;
margin:0px;
width:200px;
}
#nav > ul > li:hover > ul > li{
background: #fff;
border:1px solid #bbb;
display:block;
padding:10px;
line-height:16px;
font-weight:normal;
background:red;
z-index:10;
position:relative;
}
#nav > ul > li:hover > ul > li > a{
color:#333;
}
<div id="nav">
<ul>
<li>Menu
<ul>
<li>category</li>
<li>category</li>
<li>category</li>
</ul></li>
<li>About Us</li>
<li>Contact Us</li>
</ul>
<ul>
<li>Hello,</li>
<li>Sign in</li>
<li>country</li>
<li>cart</li>
</ul>
</div>
<div>
Some content
</div>
Note that I've set a background color to see that works.

Related

CSS overlay with menu collapsible

How i make the child lists expand and collapse by click ?
.tree-list li > ul{
display: none;
}
See the Pen Pure CSS Fullscreen Overlay Menu .
You can use it by jquery/javascript or you can simply use css for hover.
for jquery just write onclick ul display:block along with this css.
<nav id="dropdown">
<ul>
<li>Menu
<ul>
<li> 1</li>
<li> 2</li>
<li> 3</li>
</ul>
</nav>
In CSS
#dropdown
{
margin-top:15px
}
#dropdown ul
{
list-style:none;
position:relative;
float:left;
margin:0;
padding:0
}
#dropdown 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
}
#dropdown ul li
{
position:relative;
float:left;
margin:0;
padding:0
}
#dropdown ul li:hover
{
background:#f6f6f6
}
#dropdown ul ul
{
display:none;
position:absolute;
top:100%;
left:0;
background:#fff;
padding:0
}
#dropdown ul ul li
{
float:none;
width:200px
}
#dropdown ul ul a
{
line-height:120%;
padding:10px 15px
}
#dropdown ul ul ul
{
top:0;
left:100%
}
#dropdown ul li:hover > ul
{
display:block
}

HTML Center navbar

I got a navbar with several levels that i changed acording to this site:https://codepen.io/philhoyt/pen/ujHzd
I've looked around on the internet but the solutions they provide (using display:inline-block) did not seem to work out.
Sources i've tryed:
How to center a navigation bar with CSS or HTML?
how to center css navigation bar
Any help would be appriciated.
#primary_nav_wrap
{
margin:auto;
}
#primary_nav_wrap ul
{
list-style:none;
text-align:center;
position:relative;
float:left;
margin:auto;
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:Futura;
}
#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:hover > ul
{
display:block
}
<nav id="primary_nav_wrap">
<ul>
<li>Nieuws</li>
<li>Info ▼
<ul>
<li>Taf & Toetje</li>
<li>Papierwerk</li>
<li>Uniform</li>
<li>Technieken</li>
</ul>
</li>
<li>Algemeen</li>
<li>Foto's</li>
<li>Inschrijven</li>
<li>Contact</li>
<li>Permekiaan ▼
<ul>
<li class="dir">Kapoenen ▼
<ul>
<li>Info</li>
<li>Activiteiten</li>
</ul>
</li>
<li class="dir">
Kawellen ▼
<ul>
<li>Info</li>
<li>Activiteiten</li>
</ul>
</li>
<li class="dir">
Jong-Givers ▼
<ul>
<li>Info</li>
<li>Activiteiten</li>
</ul>
</li>
<li class="dir">
Givers ▼
<ul>
<li>Info</li>
<li>Activiteiten</li>
</ul>
</li>
</ul>
</li>
<li>Contact</li>
</ul>
</nav>
I made some changes to these two classes:
#primary_nav_wrap
{
margin:auto;
text-align:center;
}
#primary_nav_wrap ul
{
list-style:none;
text-align:center;
display: inline-block;
position:relative;
margin:auto;
padding:0;
}
The solution was to use display:inline-block and text-align:center;. But also deleting the float: left. Working Fiddle
Try to add :
position: absolute;
left: 50%;
transform: translateX(-50%);
at #primary_nav_wrap
You can use this code to center the whole nav.
#primary_nav_wrap ul {
list-style: none;
position: absolute;
left: 50%;
transform: translateX(-50%);
right: 0;
margin: 0;
padding: 0;
}
I removed the float from the ul and li's and added display: inline-block to the li tags, so I ended up with this:
#primary_nav_wrap
{
margin-top:15px
}
#primary_nav_wrap ul
{
list-style:none;
position:relative;
text-align: center;
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;
display: inline-block;
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:hover > ul
{
display:block
}
Add class to <ul> make it <ul class="extra"> and add css to that class .extra{left:33.33%}
You just need to make few changes in your css classes.
#primary_nav_wrap ul li
{
position:relative;
/*float:left;*/
margin:0;
padding:0;
display:inline-block;
}
#primary_nav_wrap ul
{
list-style:none;
position:relative;
/*float:left;*/
margin:0;
padding:0;
text-align:center
}
Please check updated fiddle
HTML Center navbar with flexbox
#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:Futura;
}
#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:hover > ul
{
display:block
}
/* flexbox */
#primary_nav_wrap {
display: -webkit-flex;
display: flex;
-webkit-justify-content: center;
justify-content: center;
-webkit-align-items: center;
align-items: center;
width: 100%;
height: auto;
background-color: lightgrey;
}
#primary_nav_wrap ul {
list-style:none;
background-color: cornflowerblue;
//padding: 1rem;
//margin: 1rem;
-webkit-align-self: center;
align-self: center;
}
/* || flexbox */
<nav id="primary_nav_wrap">
<ul>
<li>Nieuws</li>
<li>Info ▼
<ul>
<li>Taf & Toetje</li>
<li>Papierwerk</li>
<li>Uniform</li>
<li>Technieken</li>
</ul>
</li>
<li>Algemeen</li>
<li>Foto's</li>
<li>Inschrijven</li>
<li>Contact</li>
<li>Permekiaan ▼
<ul>
<li class="dir">Kapoenen ▼
<ul>
<li>Info</li>
<li>Activiteiten</li>
</ul>
</li>
<li class="dir">
Kawellen ▼
<ul>
<li>Info</li>
<li>Activiteiten</li>
</ul>
</li>
<li class="dir">
Jong-Givers ▼
<ul>
<li>Info</li>
<li>Activiteiten</li>
</ul>
</li>
<li class="dir">
Givers ▼
<ul>
<li>Info</li>
<li>Activiteiten</li>
</ul>
</li>
</ul>
</li>
<li>Contact</li>
</ul>
</nav>
What happens if you add left and right auto margins to primary_nav_wrap? i.e. add this to CSS:
#primary_nav_wrap
{
margin-top:15px;
margin-right: auto;
margin-left: auto;
}

change ul li from horizontal to vertical

I am trying to do a Menu that will appear when Window width is resized to a small resolution. Below is the HTML and CSS are below
<nav>
<a id="menu-dropdown"><img src="http://localhost/influenza/logo/menu.png" /></a>
<ul>
<li><a class="tab-click" href="http://localhost/influenza/index.php">Home</a></li>
<li>Articles</li>
<li>Webinars</li>
</ul>
</nav>
CSS:
nav ul
{
list-style:none;
}
nav li{
display:inline;
line-height:1.5px;
}
nav li:not(:first-child) > :only-child,
nav ul > :first-child a{
text-decoration:none;
font-size:1.4em !important;
outline:1px solid blue;
padding:8px;
letter-spacing:0.9px;
margin-left:18px;
}
nav li:not(:first-child) > :only-child{
color:blue;
}
nav ul > :first-child a{
color:white !important;
background:blue;
}
Output in Horizontal:
Home Articles Webinars
How can I bring back the list to Vertical then set "ul" position to absolute and make "nav a" to be visible. The list should display vertically like below
Home
Articles
Webinars
nav{
position:relative;
}
nav ul
{
position:absolute;
top:40px; /* this height is same as the menu.png */
left:0;
z-index:999;
list-style:none;
}
nav li{
list-style:none;
display:block;
}
nav ul li a{
display:block;
}
nav li:not(:first-child) > :only-child,
nav ul > :first-child a{
text-decoration:none;
font-size:1.4em !important;
outline:1px solid blue;
padding:8px;
letter-spacing:0.9px;
margin-left:18px;
}
nav li:not(:first-child) > :only-child{
color:blue;
}
nav ul > :first-child a{
color:white !important;
background:blue;
}
<nav>
<a id="menu-dropdown"><img src="http://localhost/influenza/logo/menu.png" /></a>
<ul>
<li><a class="tab-click" href="http://localhost/influenza/index.php">Home</a></li>
<li>Articles</li>
<li>Webinars</li>
</ul>
</nav>

Nav. bar hovering color

Background color (red) misses out after I am hovering drop down items.
What should I do to keep the background color of the menu item I am in?
<style>
nav ul
{
list-style-type:none;
padding:0px;
text-align:center;
background-color:grey;
}
nav ul li
{
display:inline-block;
}
nav ul a
{
display:block;
padding:20px;
padding-left:50px;
padding-right:50px;
text-decoration:none;
color:black;
text-transform:uppercase;
font-family:arial;
font-size:20px;
}
nav ul a:hover
{
background:red;
}
nav ul ul {
display: none;
}
nav ul li:hover ul{
display:block;
position:absolute;
}
nav ul ul li
{
display:block;
border-bottom:solid 1px black;
}
</style>
</head>
<body>
<nav>
<ul>
<li>one</li>
<li>two
<ul>
<li>Photoshop</li>
<li>Illustrator</li>
<li>Web Design</li>
</ul>
</li>
<li>three
<ul>
<li>ay</li>
<li>bee</li>
</ul>
</li>
<li>four</li>
</ul>
</nav>
</body>
This is because the parent li is not attracting the :hover, so a hover on the child submenu doesn't keep it active. As such, you need to move the style change to the li:hover instead of the a child, as the submenu is an sibling of the a and not direct child.
Change:
nav ul a:hover {
background:red;
}
To:
nav ul li:hover {
background:red;
}
Demo Fiddle

IE navigation dropdown doesn't display as it should

I've created a drop down navigation in html. I then tested it with different browser such as chrome, firefox, opera and IE. The only problem I see is that the navigation doesn't display right in IE.
HTML:
<div id="nav_bar">
<ul id="nav">
<li>Home</li>
<li>Krazie
<ul>
<li>Colour</li>
<br />
<li>Black 'N' Grey</li>
</ul>
</li>
<!-- End of Krazie links -->
<li>Stacy
<ul>
<li>Colour</li>
<br />
<li>Black 'N' Grey</li>
</ul>
</li>
<!-- End of stacy links -->
<li>Matt
<ul>
<li>Colour</li>
<br />
<li>Black 'N' Grey</li>
</ul>
</li>
<!-- End of matt links -->
<li>Contact</li>
</ul>
</div>
CSS:
#nav
{
margin-top:-0px;
background:rgb(108,108,108);
position:fixed;
top:0;
left:0;
z-index:10;
width:100%;
z-index:9003;
height:32px;
list-style:none;
color:#FFF;
}
#nav li
{
float: left;
list-style:none;
}
#nav li a
{
display: block;
height:2em;
line-height:2em;
padding:0px 10px;
text-decoration:none;
list-style:none;
color:#FFF;
}
#nav ul
{
padding:0px;
margin-left:-20px;
background:rgb(108,108,108);
position:absolute;
display:none;
z-index:9003;
list-style:none;
}
#nav ul li a
{
padding:-50px;
width:110px;
list-style:none;
}
#nav li:hover ul
{
display:block;
list-style:none;
}
#nav > li:hover > a
{
background:rgb(108,108,108);
}
#nav ul li:hover a
{
background:rgb(48,48,50);
}
The bit that doesn't display properly is the actual links that you see when you put the mouse over them. This is how it shows on other browser like chrome: http://grab.by/e5ik
but in IE it displays as: http://grab.by/e5im
Your codes seems too complicated to get your work done.
Btw, please remove <br /> in html.
Then, add the following CSS: (between #nav ul and #nav ul li a)
#nav ul li {
clear: both;
}
(tested in IE8)
Please check again in the browsers whether any error occurs.
change this-
#nav ul li a
{
padding:-50px;
width:110px;
list-style:none;
}
#nav li:hover ul
{
display:block;
list-style:none;
}
#nav > li:hover > a
{
background:rgb(108,108,108);
}
#nav ul li:hover a
{
background:rgb(48,48,50);
}
to this-
#nav ul li a
{
padding:-50px;
width:110px;
list-style:none;
display:block
}
#nav a:hover ul
{
display:block;
list-style:none;
}
#nav > li > a:hover
{
background:rgb(108,108,108);
}
#nav ul li a:hover
{
background:rgb(48,48,50);
}
sometimes ie doesn't recognize hovering over elements except a elements.