I have the following site http://jsfiddle.net/Me4fw/4/ in which I am attempting to implement a horizontal css drop down menu that has a submenu which is also horizontal. I have tried everything but nothing seems to get the submenu items to sit inline with each other. All I can get them to do is mash up ontop of each other and be ugly/unreadable.
You problem is that you are placing the sub-menu absolutly positioned to the parent li. But the parent li has a small width.
The solution is to place the sub-menu absolutely positioned to the menu wrapper.
See this link with a working presentation!
UPDATE YOUR CSS:
#menu {
width: 820px;
height: 60px;
margin: 0 auto;
padding: 0px 40px;
position: relative;
}
#menu li {
float: left;
}
#menu ul ul {
list-style-type: none;
position: absolute;
z-index: 500;
left: 0;
right: 0;
}
AND DELETE THIS CSS:
#menu ul li ul li #jackie_spencer{
display: inline;
position: absolute;
top: 20px;
left: 0;
width: 100px;
color: #FFF;
}
Note:
You can view the source and check the CSS:
where it reads "updated", some definitions were changed!
Where it reads "updated, deleted", the declarations were deleted.
I took a look at your page, and the reason your submenu is vertical is the submenu <ul> is too narrow: its width is the same as enclosing <li>. You should set a width on it to make it take up all available space.
Making it left-aligned with the parent element and right-aligned with the right end of the menu bar is tricky. You might just want to set it to something wide enough to hold everything you know will be in it, and assume there won't be any reflow.
You also could use some JavaScript to calculate the right positions for it. JQuery is good for that. You'd only need to set those once when the page is first loaded, as part of $(document).ready().
Some tweaks to give second level submenus:
/* Menu */
#menu { /* UPDATED */
width: 820px;
height: 60px;
margin: 0 auto;
padding: 0px 40px;
position: relative;
}
#menu ul {
margin: 0;
padding: 0px 0px 0px 0px;
line-height: normal;
line-style: none;
list-style-type: none;
}
#menu a {
display: block;
height: 20px;
margin-right: 1px;
padding: 10px 20px 0px 20px;
/* height: 40px;
margin-right: 1px;
padding: 20px 20px 0px 20px;*/
text-decoration: none;
text-transform: uppercase;
font-family: 'Abel', sans-serif;
font-size: 16px;
font-weight: normal;
color: #FFFFFF;
border: none;
}
#menu a:hover {
background: url(images/page-content-bg.png) repeat;
}
#menu ul ul a:hover {
background: url(images/page-menu-bg.png) repeat;
}
#menu .current_page_item a {
background: url(images/page-content-bg.png) repeat;
}
#menu li { /* UPDATED */
float: left;
}
/* UPDATED, REMOVED
#menu ul li ul li #jackie_spencer{
display: inline;
position: absolute;
top: 20px;
left: 0;
width: 100px;
color: #FFF;
}
*/
#menu ul ul { /* UPDATED */
list-style-type: none;
position: absolute;
z-index: 500;
left: 50px;
right: 0;
}
#menu ul ul ul {
position: absolute;
/* top: 0;
left: 100%;*/
z-index: 500;
left: 50px;
right: 0;
}
div#menu ul ul,
div#menu ul li:hover ul ul,
div#menu ul ul li:hover ul ul
{display: none;}
div#menu ul li:hover ul,
div#menu ul ul li:hover ul,
div#menu ul ul ul li:hover ul
{display: block;}
div#menu ul ul li:hover { background:#c0c0c0;}
div#menu ul ul li { background:#cccccc;}
div#menu ul ul li a:hover { color:#000;}
Page code
<div id="menu">
<ul>
<li class="current_page_item"><a id="home_menu" href="#Home">Home</a><ul>
<li>linkx</li>
<li>linkx</li>
<li>linkx</li>
<li>linkx</li>
<li>linkx</li>
<li>linkx
<ul>
<li>linky</li>
<li>linky</li>
<li>linky</li>
<li>linky</li>
<li>linky</li>
<li>linky</li>
</ul>
</li>
</ul></li>
<li><a id="aboutus_menu" href="#About_Us">About Us</a><ul>
<li>link</li>
<li>link</li>
<li>link</li>
<li>link</li>
<li>link</li>
<li>link</li>
</ul></li>
</ul>
<ul>
<li><a id="what_we_do" href="#What_We_Do">What We Do</a>
<ul>
<li><a id="jackie_spencer" href="#Jackie_Spencer">Hypnotherapy</a></li>
<li><a id="diana_menz" href="#Diana_Menz">Massage</a></li>
<li><a id="afton_land" href="#Jackie_Spencer">Estitician</a></li>
</ul>
</li>
</ul>
<ul>
<li><a id="contactus_menu" href="#Contact_Us">Contact Us</a><ul>
<li>link</li>
<li>link</li>
<li>link</li>
<li>link</li>
<li>link</li>
<li>link</li>
</ul></li>
</ul>
</div>
Related
I'm trying to create a drop-down menu but my text-align command wouldn't seem to work (same applies with font-size and other text-related codes). I tried putting my text-align codes in nav ul li and nothing seem to happen. I've also tried putting it on the main .drop-down menu on CSS but it still has no changes. Can anyone help me out here? I couldn't figure out the reasoning behind this.
My HTML and CSS codes are:
nav{
flex:1;
text-align: right;
}
nav ul li{
list-style: none;
display: inline-block;
margin-left: 60px;
position: relative;
}
nav ul li a{
text-decoration: none;
color: #fff;
font-size: 13px;
transition: all 0.5s;
}
nav ul li a:hover{
color: #E6C7F3;
}
.dropdown-menu{
display: none;
}
nav ul li:hover .dropdown-menu {
display: block;
position: absolute;
left: -35;
top: 100%;
width: 100px;
background-color:black;
opacity: .8;
border-radius: 10px;
}
.dropdown-menu ul{
display: block;
padding-top: 10px;
}
.dropdown-menu ul li{
width: 0px;
left: -58;
bottom: 5;
padding: 10px;
padding-right: 40px;
text-align: center;
}
<div class="navbar">
<img src="image/durra.png" class="logo">
<nav>
<ul>
<li>HOME</li>
<li>ABOUT US</li>
<li>SHOP
<div class="dropdown-menu">
<ul>
<li>Bestsellers</li>
<li>Accessories</li>
<li>Jewelry</li>
<li>Miscellaneous</li>
</ul>
</div>
</li>
<li>FEEDBACK</li>
<li>CONTACT</li>
</ul>
</nav>
</div>
text-align works with a element having width. But you have used width: 0px . So it's pretty obvious you cannot use alignment there.
Hi First you have to add padding to ul under dropdown-menu. and then set width of li item to 100%. Please refer below code.
.dropdown-menu ul {
display: block;
padding-top: 10px;
padding: 10px 0 0;
}
.dropdown-menu ul li {
width: 100%;
left: -58;
bottom: 5;
padding: 10px 0;
/* padding-right: 40px; */
text-align: center;
}
I have a menu with margin-top and margin-left of 12 pixels for the elements that are inside another , but when the cursor is between that space (margin), the menu is hidden. I can solve that if I remove position:absolute, but I need it. How can I solve this?
HTML
<ul>
<li>Home</li>
<li>Food
<ul>
<li>Chilean food</li>
<li>Chinese food</li>
<li>Mexican food
<ul>
<li>Nachos</li>
<li>Quesadillas</li>
<li>Tacos</li>
</ul>
</li>
</ul>
CSS
ul {
margin: 0;
padding: 0;
list-style: none;
display: flex;
}
ul li {
position: relative;
}
ul li ul {
display: none;
position: absolute;
margin-top: 4px;
}
ul li:hover > ul {
display: block;
}
ul li a {
text-decoration: none;
padding: 12px;
display: block;
background: black;
color: white;
white-space: nowrap;
}
ul li ul li ul {
top: 0;
left: 100%;
margin-top: 0;
margin-left: 4px;
}
JSFiddle: https://jsfiddle.net/v3ebd6hy/3/
Thanks!
Change the margin-top to padding-top in ul li ul.
Fiddle for Reference
I'm having great difficultly designing a drop down menu with pure css, that has fluid width. The examples I've found all have a fixed width top menu. As soon as I make the top menu fluid width, the drop down doesn't work. Can anyone help me complete the code? I basically have 7 menu items including a blank middle one. Over the middle one I have an absolute positioned logo.
HTML
<div style="position:relative;">
<img style="position:absolute; width:14.28%; max-height:80px; margin-left: auto;margin-right: auto;left: 0;right: 0;" src="\adrenicon.jpg"/>
<nav>
<ul>
<li>Start Here</li>
<li>Destinations
<ul>
<li>Africa</li>
<li>Europe</li>
<li>America</li>
</ul>
</li>
<li>Features</li>
<li> </li>
<li>Activities</li>
<li>Planner</li>
<li>Contact</li>
</ul>
</nav>
</div>
CSS:
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
nav li {
float: left;
width:14.28%;
text-align: center;
line-height: 50px;
display: inline-block;
position: relative;
}
nav li a {
font-weight: bold;
color: hotpink;
text-align: center;
padding: 0px;
text-decoration: none;
-webkit-transition: color 1s;
transition: color 1s;
}
nav li a:hover {
color: #111;
}
nav ul li ul {
position: absolute;
display: none;
background-color: #f9f9f9;
min-width: 160px;
}
Thanks for any help.
first you need to set overflow:visible to the main ul ( nav ul )
then, because you set a width of 14.28% on the nav ul li you need to set a width of 100% to the second level of li ( nav ul li ul li ) so they don't get on top of eachoter and each stays on a separate row
and because the li has float:left; you need to set float:left;width:100% on the nav ul also.
then on hover on li change display:none to display:block on the second ul
see below snippet. let me know if it works
nav ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: visible;
background-color: #333;
float:left;
width:100%;
}
nav li {
float:left;
width:14.28%;
text-align: center;
line-height: 50px;
position: relative;
}
nav li a {
font-weight: bold;
color: hotpink;
text-align: center;
padding: 0px;
text-decoration: none;
-webkit-transition: color 1s;
transition: color 1s;
}
nav li a:hover {
color: #111;
}
nav ul li ul {
position: absolute;
display: none;
background-color: #f9f9f9;
min-width: 160px;
}
nav ul li ul li {
width:100%;}
nav ul li:hover > ul{
display:block;
}
nav ul ul ul {
right:-100%;
top:0
}
<div style="position:relative;">
<img style="position:absolute; width:14.28%; max-height:80px; margin-left: auto;margin-right: auto;left: 0;right: 0;" src="http://placehold.it/100x100.jpg"/>
<nav>
<ul>
<li>Start Here</li>
<li>Destinations
<ul>
<li>Africa
<ul>
<li>Kenya</li>
<li>Tanzania</li>
</ul>
</li>
<li>Europe
<ul>
<li>Spain</li>
<li>France</li>
</ul>
</li>
<li>America</li>
</ul>
</li>
<li>Features</li>
<li> </li>
<li>Activities</li>
<li>Planner</li>
<li>Contact</li>
</ul>
</nav>
</div>
Edited also for 3rd level of ul .
code : ul ul ul { right:-100%;top:0;}
I would like to have a dropdown sub- menu in the same style, I know it's simple but I'm still new to making websites and I can't figure it out by myself.
here's the top part of my HTML file:
<!DOCTYPE html>
<html>
<head>
<title>Startpagina</title>
<LINK href="../CSS/stylesheet.css" rel=stylesheet>
</head>
<body>
<div class="schikking">
<img src="../Images/bibram.png" alt="Logo van de bib" height="90" width="170">
<!-- navigatie -->
<nav>
<ul>
<li><span class ="s2">Startpagina</span></li>
<li>Aanwinsten</li>
<li>Catalogus
<ul class="sub">
<li>Pages</li>
<li>Archives</li>
<li>New Posts</li>
</ul>
</li>
<li>Uitlening</li>
<li>Reservatie</li>
<li>Suggestie</li>
<li>Contact</li>
</ul>
</nav>
and a big part of my CSS file:
.schikking {
margin: 0 auto;
padding: 30px 0px 0px 0px;
max-width: 1010px;
}
.content {
background-color: red;
background-color: rgba(147, 4, 0, 0.84);
border: 1px solid black;
}
nav li
{
display: inline;
padding-right: 8px;
}
nav {
text-align: center;
margin: -20px 0px 0px 0px;
}
nav ul{
background-color: rgba(126, 4, 0, 0.79);
border: 1px solid black;
}
nav ul li{
display: inline;
}
nav ul li a{
padding-left: 1em;
padding-right: 1em;
font-size: 12px;
font-family: Arial, Helvetica, sans-serif;
text-decoration: none;
color: lightgray;
}
nav ul li a:hover{
color: #999999;
}
nav ul ul{display: none; position: relative;}
nav li ul li{float:none;display: inline-block; }
nav ul li:hover ul {display: inline-block;}
heres a picture of how it looks atm:
normal: http://gyazo.com/8f6553245b736feee8cc5ebf8d4a030c
while hovering over "catalogus": http://gyazo.com/662eee4bbbb2ea2318925be76b3722d2
You have nearly got it. I have only made some minor changes to the CSS to make it work.
nav ul li { display: inline-block; height: 100%; } instead of just display: inline is required so that the each <li> takes up all the height of the "menu" otherwise there is a small gap between the bottom of the <li> and the sub-menu which would cancel the :hover event since you are out of the <li>. inline elements do not have height (or width), so changed to display: inline-block.
The CSS at the end is where the other changes are. Your code is:
nav ul ul{display: none; position: relative;}
nav li ul li{float:none;display: inline-block; }
nav ul li:hover ul {display: inline-block;}
The display code doesn't need to be anything more than
nav ul li:hover ul {
display: block;
}
But to position the sub-menu outside of it's normal flow (which is currently appearing next to the parent menu item), you need to add an absolute position to the sub-menu `.
nav ul ul {
display: none;
position: absolute;
}
If you want a horizontal menu, that should be all the changes needed, since your rule nav ul li { display: inline-block; }will already apply to the sub-menu list-items. If you want a vertical menu, you need to reset the display back to the default list-item or block with:
nav ul ul li {
display: block;
}
See demo
Don't do it yourself. I use this jquery plug-in and its great:
Superfish
If you are having problems with anything I'd reccomend you to google them first. Here's a generator (just choose the one you want and follow the instructions):
Css drop down menu maker
I would also reccomend you to actually learning the language and expanding your knowledge, as well as googling questions before posting them here.
HTML :
<nav>
<ul>
<li><span class ="s2">Startpagina</span></li>
<li>Aanwinsten</li>
<li>Catalogus
<ul class="sub">
<li>Pages</li>
<li>Archives</li>
<li>New Posts</li>
</ul>
</li>
<li>Uitlening</li>
<li>Reservatie</li>
<li>Suggestie</li>
<li>Contact</li>
</ul>
CSS :
nav {
margin: -20px 0px 0px 0px;
text-align: center;}
nav ul ul {
display: none;
padding-right: 8px;}
nav ul li:hover > ul {
display: block;}
nav ul {
background-color: red;
border: 1px solid black;
list-style: none;
position: relative;
display: inline-table;}
nav ul:after {
content: ""; clear: both; display: block;}
nav ul li {
float: left;}
nav ul li:hover a {
color: #999999;}
nav ul li a {
display: block;
padding-left: 1em;
padding-right: 1em;
font-size: 12px;
font-family: Arial, Helvetica, sans-serif;
text-decoration: none;
color: lightgray;}
nav ul ul {
background: #5f6975; border-radius: 0px; padding: 0;
position: absolute; top: 100%;}
nav ul ul li {
float: none; position: relative;padding: 10px;}
nav ul ul li a {
color: #fff;}
nav ul ul ul {
position: absolute; left: 100%; top:0;}
Screenshot1:
Screenshot 2:
If you look at the screenshot1, you can see when you hover over web design it doesn't completely change the background color. However it does in case you hover over text 'Search Engine' in Screenshot 2.
Here is the markup:
Html:
<div id="nav">
<ul>
<li><a href="#" >My Health</a></li>
<li><a href="#" >Fitness</a></li>
<li><a href="#" >Diet & Nutrition</a>
<ul>
<li>Webdesign</li>
<li>Development</li>
<li>Illustration</li>
<li>Search engine</li>
<li>WordPress</li>
</ul>
</li>
<li>Stress Management</li>
</ul>
</div>
CSS:
#nav {
width: 100%;
height: 36px;
background: url('../images/nav-bg.png') repeat;
margin-bottom: 10px;
}
#nav ul li a{
display: block;
float: left;
font: bold 15px Arial, Helvetica, sans-serif;
color: #FFFFFF;
padding: 9px 14px;
}
#nav ul li a:hover{
color: #355da5;
background: #fff;
border: medium black;
}
#nav ul li ul{
display: none;
position: absolute;
top: 130px;
margin: 8px 0px 0px 180px;
background: url('../images/nav-bg.png') repeat;
}
#nav ul li ul li a{
text-align: center;
font-size: 12px;
}
#nav ul li:hover ul, li.over ul{
display: block;
}
Does anyone know how can i fix this?
Thank you.
Try setting width: 100% on #nav ul li a.
your nested <a> tags are matched by the css selector #nav ul li a, and are therefore floated left.
try adding float:none; inside #nav ul li ul li a