Dropdown menu keeps skipping away - html

I am creating some websites and I am always using the same dropdown method for them. However i am stuck with my dropdown menu. When I hover on it it always replaces my main navigation and well lets say that not so customer friendly.
The problem probably lies within the position values I give in my css code but I fail to find how to make the right values for my needs.
So in proper words, my navigation is fine till you hover over one with a dropdown menu then it replaces himself.
I included a JSFIDDLE to show you what i mean. There is a dropdown menu under the second link in the main navigation.
HTML:
<div id="header">
<div class="blueborder">
</div>
<section class="nav">
<div class="row">
<ul class="main-nav" id="drop-nav">
<li class="home-active">Home</li>
<li> / </li>
<div class="dropdown">
<li>Over Ons</li>
<div class="dropdown-content">
<li>Betuigenissen</li>
</div>
</div>
<li> / </li>
<li>Schilderwerken</li>
<li> / </li>
<li>Behangwerken</li>
<li> / </li>
<li>Raamdecoratie</li>
<li> / </li>
<li>Realisaties</li>
<li> / </li>
<li>Contacteer ons</li>
</ul>
</div>
</section>
</div>
CSS:
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
text-align: center;
display: none;
position: inherit;
background-color: #2C2A26;
margin-top: 40px;
color: #000;
list-style: none;
width: auto;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 998;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown-content li a {
color: #000;
border-bottom: 0px solid #ed5d00;
}
.dropdown-content li {
padding: 10px 10px;
}
.dropdown-content li a:hover {
color: #fff;
padding-bottom: 0px;
border-bottom: 0px solid #ed5d00;
}
/* Main Navi */
.main-nav {
float: left;
list-style: none;
margin-top: 0px;
padding-top: 0px;
color: #000;
font-size: 110%;
font-weight: 600;
font-family: 'Source Sans Pro', sans-serif;
}
.main-nav li {
display: inline-block;
margin-left: 1px;
color: #fff;
}
.main-nav li a {
padding: 8px 0;
color: #000;
text-decoration: none;
text-transform: uppercase;
font-size: 90%;
border: 0;
}
.main-nav li a:hover,
.main-nav li a:active {
color: #8dcee6;
}

Change to position: absolute; on the .dropdown-content and remove the top-margin, works for me:
CSS:
.dropdown-content {
text-align: center;
display: none;
position: inherit;
background-color: #2C2A26;
color: #000;
list-style: none;
width: auto;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 998;
}
.dropdown:hover .dropdown-content {
display: block;
position: absolute;
}
https://jsfiddle.net/r3gra7ta/2/

i think Your Navigation is a little bit confused.
One of simplest method is to set the submenu within the OVER ONS
<li name="OVER ONS" ><li SUBITEM1 ></li><li SUBITEM2 ></li></li>
Here a Basic 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:hover > ul
{
display:block
}
<h1>LOGO</h1>
<nav id="primary_nav_wrap">
<ul>
<li class="current-menu-item">HOME</li>
<li>OVERONS
<ul>
<li>Betuigenissen</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>
</ul>
</li>
<li>Schilderwerken</li>
<li>Behangwerken</li>
<li>Raamdecoratie</li>
<li>Realisaties</li>
<li>Contacteer ons</li>
</ul>
</nav>

just change the postion to "fixed" and remove margin-top in dropdown-content then it will work fine.
.dropdown-content {
text-align: center;
display: none;
position: inherit;
background-color: #2C2A26;
color: #000;
list-style: none;
width: auto;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 998;
}

Related

CSS3/HTML5 dropdown menu

Been trying to fix my code for a couple of hours and I just can't. I want to have my navbar have a dropdown function. But I can't seem to make it work.
As you can see I've start on the dropdown menu class, but I can't figure where to go from there. Been googling, and found a lot of examples, tried to copy/paste some of the bits, with no good results. So I was thinking that you might be able to help.
nav {
display: block;
background-color: indianred;
box-shadow: 0px 10px 5px #888888;
}
nav ul {
text-align: center;
}
nav ul li {
display: inline-block;
padding: 20px;
}
nav ul li a {
width: 125px;
color: black;
padding: 20px;
background-color: blueviolet;
text-decoration: none;
}
nav ul li a:hover {
background-color: aqua;
}
.dropdown {
display: none;
}
<nav>
<ul>
<li>FORSIDE
</li>
<li>ERHVERVSUDDANNELSER
<ul class="dropdown">
<li>content 1</li>
<li>content 1</li>
<li>content 1</li>
<li>content 1</li>
</ul>
</li>
<li>EUX
</li>
<li>HTX
</li>
<li>OM TECH COLLEGE
</li>
</ul>
</nav>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display: block;
}
<ul>
<li>Home</li>
<li>News</li>
<li class="dropdown">
Dropdown
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</li>
</ul>
for more detail visit Dropdown Menu in Navbar
Change the CSS to this:
nav {
display: block;
background-color: indianred;
box-shadow: 0px 10px 5px #888888;
}
nav ul {
text-align: center;
}
nav ul li {
display: inline-block;
padding: 20px;
position:relative;
}
nav ul li a {
width: 125px;
color: black;
padding: 20px;
background-color: blueviolet;
text-decoration: none;
}
nav ul li a:hover {
background-color: aqua;
}
nav ul li ul {
display:none;
}
nav ul li:hover ul {
display:block;
position:absolute;
background:indianred;
}
<nav>
<ul>
<li>FORSIDE</li>
<li>ERHVERVSUDDANNELSER
<ul class="dropdown">
<li>content 1</li>
<li>content 1</li>
<li>content 1</li>
<li>content 1</li>
</ul>
</li>
<li>EUX</li>
<li>HTX</li>
<li>OM TECH COLLEGE</li>
</ul>
</nav>
You wanted to hide the dropdown but then show it when you hover over the dropdown's parent element.
I then made the parent li position:relative; and the dropdown position:absolute so it didn't push any of the other dropdown menu items anywhere it shouldn't be, but didn't style it past giving it a background colour.
you need to write css for the element on hover
like
element1:hover tobeshownele{display:block;}
You can try something like this. Didn't want to change too much so the code and UI are still quite messy.
nav {
display: block;
background-color: indianred;
box-shadow: 0px 10px 5px #888888;
}
nav ul {
text-align: center;
}
nav ul li {
display: inline-block;
margin: 20px;
}
nav ul li a {
width: 125px;
color: black;
padding: 20px;
background-color: blueviolet;
text-decoration: none;
}
nav ul li a:hover {
background-color: aqua;
}
.dropdown {
display: none;
position: absolute;
}
nav ul li:hover .dropdown {
display: block;
}
ul.dropdown li {
display: block;
padding: 10px;
}
<nav>
<ul>
<li>FORSIDE</li>
<li>ERHVERVSUDDANNELSER
<ul class="dropdown">
<li>content 1</li>
<li>content 1</li>
<li>content 1</li>
<li>content 1</li>
</ul>
</li>
<li>EUX</li>
<li>HTX</li>
<li>OM TECH COLLEGE</li>
</ul>
</nav>

CSS Dropdown-menu not displaying when <li>-element is hovered.

I have a top navigation bar with 4 options, where I wish that while the fourth option is hovered, a dropdown-menu will display.
The navigation bar is an unordered list, and the dropdown-menu (which should be activated by the fourth option) is a div consisting of 3 links.
The list item that is supposed to display the dropdown-menu while hovered:
<li class="dropdownbutton">Contact</li>
With the help of this line of code:
.dropdownbutton:hover .dropdowncontent {
display:inline-block;
}
And this is the CSS for the dropdown-content:
.dropdowncontent {
display:none;
background-color:#333;
margin-left:272px;
width:90px;
text-align:center;
}
What am I missing?
This should be allright.
ul {
margin: 0;
padding: 0;
list-style: none;
background-color: #333;
}
ul li {
display: inline-block;
position: relative;
text-align: left;
background-color: #333;
}
ul li a {
display: block;
color: #f2f2f2;
text-align: center;
padding: 16px 15px;
text-decoration: none;
transition-duration: 0.3s;
-webkit-transition-duration: 0.3s;
font-size: 17px;
font-family: Arial;
}
ul li a:hover {
background-color: #555;
}
ul li ul.dropdown {
min-width: 100%;
display: none;
position: absolute;
z-index: 999;
left: 0;
width: 90px;
}
ul li:hover ul.dropdown {
display: block;
}
ul li ul.dropdown li {
display: block;
}
ul li ul.dropdown li a {
font-size: 14px;
padding: 10px 15px;
}
<ul>
<li>Home</li>
<li>About Us</li>
<li>Products</li>
<li>Contact
<ul class="dropdown">
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
</ul>
</li>
</ul>

How to Line Up Sub List Items Vertically in Drop Down Menu

I'm working on a CSS hover menu and all is working except that my sub menu seems to shift one of the menu items and they don't line up vertically. I've been toying with the CSS but can't seem to figure out which setting it is.
<ul>
<li>Reports</li>
<li>Users</li>
<li>Settings</li>
<li>Admin
<ul>
<li>Sub 1</li>
<li>Sub 2</li>
<li>Sub 3</li>
</ul>
</li>
</ul>
My CSS
.navigation ul ul {
display: none;
}
.navigation ul li:hover > ul {
display: block;
position: absolute;
background-color: #CC0000;
margin-left: -77px;
}
.navigation ul ul li {
float: none;
display: list-item;
position: relative;
text-align: right;
min-width: 100px;
}
You can see this in action at: http://codepen.io/anon/pen/ZGZNqG
You have to remove padding-right attribute from the .navigation ul li:last-child. Because you have given the other lis a padding of 10px in .navigation ul li styles...
.navigation {
border: 1px solid black;
}
.navigation a {
text-decoration: none;
color: #000000;
}
.navigation a:hover {
text-decoration: underline;
}
.navigation ul {
list-style: none;
margin: 0px 0px 0px 0px;
}
.u-pull-right li:last-child {
padding: 2px 0px 2px 10px;
}
#admin-list li{
padding: 2px 0px 2px 0px;
}
.navigation ul li {
display: inline-block;
padding: 2px 10px 2px 10px;
}
.navigation ul li:first-child {
padding-left: 0px;
}
.navigation ul li.active {
font-weight: bold;
}
.navigation ul ul {
display: none;
}
.navigation ul li:hover > ul {
display: block;
position: absolute;
background-color: #CC0000;
margin-left: -77px;
}
.navigation ul ul li {
float: none;
display: list-item;
position: relative;
text-align: right;
min-width: 100px;
}
<div class="row navigation no-print">
<ul class="u-pull-right">
<li>Reports
</li>
<li>Users
</li>
<li>Settings
</li>
<li id="admin-list">Admin
<ul>
<li>Sub 1</li>
<li>Sub 2</li>
<li>Sub 3</li>
</ul>
</li>
</ul>
</div>

Horizontal alignment of CSS sub-submenu?

I have created a nested menu with the ul/li tags and I would like to horizontally align the second and the third sub-submenu to the first submenu.
Visually, I have this: http://i.imgur.com/vGtPtft.png
and I want this: http://i.imgur.com/wcqw3TA.png
CSS/HTML:
body {
background-color: #000;
font-size: 1.6vw;
color: #FFF;
}
div#header {
width: 65vw;
margin-left: auto;
margin-right: auto;
}
#nav {
font-size: 1.4em;
white-space: nowrap;
position: absolute;
}
#nav ul {
display: inline-table;
list-style-type: none;
margin: 0px;
border-style: none;
padding: 0px;
}
#nav ul li {
display: table-cell;
}
#nav ul li a {
margin: 0px 0px 0px 20px;
padding: 0px 0px 0px 5px;
border-left-style: solid;
border-left-width: 7px;
border-color: #FFF;
color: #FFF;
text-decoration: none;
}
#nav ul li .active {
color: #F00;
border-left-color: #F00;
}
#nav ul li a:hover {
color: #F00;
border-color: #F00;
}
#nav ul li ul {
position: absolute;
display: none;
}
#nav ul li:hover ul {
display: block;
margin: 0px;
padding: 5px 0px 0px;
}
#nav ul li:hover ul li ul {
display: none;
}
#nav ul li:hover ul li:hover ul {
display: block;
margin: 0px;
padding: 5px 0px 0px;
font-size: 0.85em
}
<html>
<head>
<title>Home</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="header">
<div id="nav">
<ul>
<li>Menu 1
</li>
<li>Menu 2
<ul>
<li>Submenu 1
<ul>
<li>Subsubmenu 1
</li>
<li>Subsubmenu 2
</li>
</ul>
</li>
<li>Submenu 2
<ul id="sub2">
<li>Subsubmenu 3
</li>
<li>Subsubmenu 4
</li>
<li>Subsubmenu 5
</li>
<li>Subsubmenu 6
</li>
<li>Subsubmenu 7
</li>
</ul>
</li>
<li>Submenu 3
<ul id="sub3">
<li>Subsubmenu 8
</li>
<li>Subsubmenu 9
</li>
<li>Subsubmenu 10
</li>
</ul>
</li>
</ul>
</li>
<li>Menu 3
</li>
<li>Menu 4
</li>
<li>Menu 5
</li>
<li>Menu 6
</li>
</ul>
</div>
</div>
</body>
</html>
The only idea I came up with is to manually move the margin to the left by adding this:
#nav ul li:hover ul li:hover ul#sub2 {
margin-left: -10.8vw
}
#nav ul li:hover ul li:hover ul#sub3 {
margin-left: -21.5vw
}
I thought that using "vw" as unit is a good idea because I have a 16:9 monitor (resolution: 1366x768) and I assumed it would scale on other 16:9 monitors but tests on other resolutions show that the sub-submenu's are either more to the left or still a bit to the right of the first submenu. Is it possible to handle this problem with minor modifications or would I have to rewrite the whole thing by using, for example fixed sizes in pixels ?
You can simply try adding this:
#nav ul li ul li ul {
left: 0;
}
Demo - http://jsfiddle.net/d8hrcxmr/2/
If you ever need all the sub menus appear to the very left check out this demo - http://jsfiddle.net/d8hrcxmr/1/
#nav ul li ul {
left: 0;
}
I think this is better for solving the problem of sub menus get cut off on the right (Imagine you have long sub menus on Menu6).
You have do use left to move sub menu to left edge of its containing element.
Here is the working example of your desired result
body {
background-color: #000;
background-position: center;
background-repeat: repeat;
font-family: 'Open Sans Condensed', Calibri, sans-serif;
font-size: 1.6vw;
color: #FFF;
}
div#header {
width: 65vw;
margin-left: auto;
margin-right: auto;
}
#nav {
font-size: 1.4em;
white-space: nowrap;
position: absolute;
}
#nav ul {
display: inline-table;
list-style-type: none;
margin: 0px;
border-style: none;
padding: 0px;
}
#nav ul li {
display: table-cell;
}
#nav ul li a {
margin: 0px 0px 0px 20px;
padding: 0px 0px 0px 5px;
border-left-style: solid;
border-left-width: 7px;
border-color: #FFF;
color: #FFF;
text-decoration: none;
}
#nav ul li .active {
color: #F00;
border-left-color: #F00;
}
#nav ul li a:hover {
color: #F00;
border-color: #F00;
}
#nav ul li ul {
position: absolute;
display: none;
}
#nav ul li:hover ul {
display: block;
margin: 0px;
padding: 5px 0px 0px;
}
#nav ul li:hover ul li ul {
display: none;
}
#nav ul li:hover ul li:hover ul {
display: block;
left: 0;
margin: 0px;
padding: 5px 0px 0px;
font-size: 0.85em
}
<html>
<head>
<title>Home</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="header">
<div id="nav">
<ul>
<li>Menu 1
</li>
<li>Menu 2
<ul>
<li>Submenu 1
<ul>
<li>Subsubmenu 1
</li>
<li>Subsubmenu 2
</li>
</ul>
</li>
<li>Submenu 2
<ul id="sub2">
<li>Subsubmenu 3
</li>
<li>Subsubmenu 4
</li>
<li>Subsubmenu 5
</li>
<li>Subsubmenu 6
</li>
<li>Subsubmenu 7
</li>
</ul>
</li>
<li>Submenu 3
<ul id="sub3">
<li>Subsubmenu 8
</li>
<li>Subsubmenu 9
</li>
<li>Subsubmenu 10
</li>
</ul>
</li>
</ul>
</li>
<li>Menu 3
</li>
<li>Menu 4
</li>
<li>Menu 5
</li>
<li>Menu 6
</li>
</ul>
</div>
</div>
</body>
</html>
it's pretty simple.just add the
#nav ul li ul li ul {
left: 0;
} to your css code.
body {
background-color: #000;
font-size: 1.6vw;
color: #FFF;
}
div#header {
width: 65vw;
margin-left: auto;
margin-right: auto;
}
#nav {
font-size: 1.4em;
white-space: nowrap;
position: absolute;
}
#nav ul {
display: inline-table;
list-style-type: none;
margin: 0px;
border-style: none;
padding: 0px;
}
#nav ul li {
display: table-cell;
}
#nav ul li a {
margin: 0px 0px 0px 20px;
padding: 0px 0px 0px 5px;
border-left-style: solid;
border-left-width: 7px;
border-color: #FFF;
color: #FFF;
text-decoration: none;
}
#nav ul li .active {
color: #F00;
border-left-color: #F00;
}
#nav ul li a:hover {
color: #F00;
border-color: #F00;
}
#nav ul li ul {
position: absolute;
display: none;
}
#nav ul li:hover ul {
display: block;
margin: 0px;
padding: 5px 0px 0px;
}
#nav ul li:hover ul li ul {
display: none;
}
#nav ul li:hover ul li:hover ul {
display: block;
margin: 0px;
padding: 5px 0px 0px;
font-size: 0.85em
}
#nav ul li ul li ul {
left: 0;
}

Adding a border-bottom: 1px solid #FFF to Index (top menu only) on hover in CSS Horizontal Drop Down Menu

I can't seem to figure as to how to add a border-bottom: 1px solid #FFF to all the top menu (index) items when hovered?
#menu{
padding:0;
margin:0;
position: fixed;
top: 30px;
left: 0px;
font-size: 8pt;
}
#menu ul{
padding:0;
margin:0;
}
#menu li{
position: relative;
float: left;
list-style: none;
margin: 0;
padding:0;
}
#menu li a{
width:120px;
height: 20px;
display: block;
text-decoration:none;
line-height: 20px;
background-color: #A9BBD3;
color: #FFF;
}
#menu li a:hover{
background-color: #446087;
}
#menu ul ul{
position: absolute;
top: 20px;
visibility: hidden;
}
#menu ul ul li a {
width: 115px;
padding-left: 5px;
}
#menu ul li:hover ul{
visibility:visible;
}
#menu > ul > li > a {
text-align:center;
}
<div id="menu">
<ul>
<li>File
<ul>
<li>Save</li>
<li>Link 1-2</li>
<li>Link 1-3</li>
</ul>
</li>
<li>Edit
<ul>
<li>Add</li>
<li>Delete</li>
</ul>
</li>
<li>Reports
<ul>
<li>Link 3-1</li>
<li>Link 3-2</li>
<li>Link 3-3</li>
</ul>
</li>
</ul>
<ul>
</div>
Use the css child selector to get the first one only:
/* NEW: This is the new style rule. */
#menu > ul > li > a:hover {
border-bottom: 1px solid #FFF;
}
#menu ul ul {
position: absolute;
top: 21px; /* <------ Had to add a pixel */
visibility: hidden;
}
Demo: http://jsbin.com/aciceg/1/
Source: http://jsbin.com/aciceg/1/edit
#menu ul ul{
position: absolute;
top: 21px;
visibility: hidden;
}
Change top to 21px for this one.
To put the border only on the top-level elements, use the direct descendant selector to make this rule:
#menu > ul > li > a:hover { border-bottom: 1px solid #fff; }
Then to make your new border visible, and not covered up by the submenus, move the submenus down by one pixel: change top: 20px to top: 21px in #menu ul ul.