Aligning drop-down menu in CSS - html

I can't seem to make the drop-down <ul> align with it's parent <li>.
What am I doing wrong?
HTML:
<nav id="toolbar">
<ul>
<li>
Section1</li>
<li>
Section2
<ul>
<li>
SubA
</li>
</ul>
</li>
<li>
Section3
<ul>
<li>SubB</li>
</ul>
</li>
<li>
Section4
</li>
</ul>
</nav>
CSS:
#toolbar ul {list-style-type:none;
}
#toolbar ul li {display:block;
position:relative;
float:left;
margin:0px 5px;
}
#toolbar li ul {display:none;
}
#toolbar ul li:hover ul {display: block;
position:absolute;
}
Fiddle

ul elements have a default padding-left value applied by the user-agent, so by removing that you get the alignment you asked for (There is also a margin added by you, but I'm gonna ignored it since you've added it yourself).
#toolbar ul {
padding-left: 0;
}
or (if you want to target only the sub-menu):
#toolbar ul ul {
padding-left: 0;
}
Demo

What adonis said was absolutely correct. There are certain other elements which take default padding and margin. So try using reset before styling any element.
*{margin:0;padding:0;}
if no list item of your page wants default bullets you may also include list-style:none in the above reset lines of code.

There is margin and padding in this case (you add the margin yourself) which is affecting it.
View here for a fix
(this has outlines for more visual proof)
CSS:
#toolbar ul {
padding: 0;
list-style-type:none;
}
#toolbar ul > li {
display:block;
position:relative;
float:left;
margin:0px 5px;
}
#toolbar li ul {
display:none;
}
#toolbar ul li:hover ul {
display: block;
position:absolute;
}
#toolbar ul ul > li {
margin: 0;
}

Try this one i have pasted the code here
HTML:
<nav id="toolbar">
<ul>
<li>
Section1</li>
<li>
Section2
<ul>
<li>
SubA
</li>
</ul>
</li>
<li>
Section3
<ul>
<li>SubB</li>
</ul>
</li>
<li>
Section4
</li>
</ul>
</nav>
CSS:
#toolbar ul {list-style-type:none;
}
#toolbar ul li {display:block;
position:relative;
float:left;
margin:0px 5px;
}
#toolbar li ul {display:none;
}
#toolbar ul li:hover ul {
display: block;
position:absolute;
}
#toolbar ul li:hover ul li{
margin-left: -30px;
}

Related

HTML/CSS: show submenu next to parent MENU, not parent item

I have a three-level navigation, the third level (sub-submenu) displays when hover on the second one (submenu).
The sub-submenu appears relative to the parent item. But I want it to appear simply next to the submenu, so always on the same place, regardless which submenu-item gets hovered. So the first item of the sub-submenu (e.g. "Second-One") is always on the same height as the first item of the submenu ("First")
Here is a JSFiddle for my actual status: http://jsfiddle.net/fc0rwbqu/
Thanks in advance!
Code:
<ul id="nav">
<li>Home</li>
<li>Numbers
<ul>
<li>First
<ul>
<li>First-One</li>
<li>First-Two</li>
<li>First-Three</li>
</ul>
</li>
<li>Second
<ul>
<li>Second-One</li>
<li>Second-Two</li>
<li>Second-Three</li>
</ul>
</li>
<li>Third
<ul>
<li>Third-One</li>
<li>Third-Two</li>
<li>Third-Three</li>
</ul>
</li>
</ul>
</li>
CSS:
#nav {
position: relative}
#nav li a,#nav li { float:left;
margin-left: 20px;
background-color: #ddd;}
#nav li {
list-style:none;
position:relative;}
#nav li ul {
display:none;
position:absolute;
left:0; width: 100px;
top:90%;
padding:0;
margin:0;}
#nav li:hover > ul {
display:block;}
#nav li ul li {
float:none;
height: 35px;
min-width: 100px;
line-height: 35px;
border-right: 0;
display:block;}
#nav li ul li ul {
display:none; margin: 0 0 0 10px;
width: 100px; z-index:9999;
float: left !important; overflow: display;
background-color: #f06;}
#nav li ul li:hover ul {
left: 100px;
top:0;}
Something like this?? http://jsfiddle.net/fc0rwbqu/1/
Appled position: relative to ul tag.

How to remove right border

Do you guys have any idea how to remove the right border on my drop down menu? I tried putting right-border: none, right-border: hidden, and right-border: 0px but nothing!
HTML :
<section class="menu">
<ul>
<li><a class="active" href="#"> PORTFOLIO </a>
<ul>
<li> illustrations </li>
<li> portraits </li>
<li> environments </li>
<li> life drawings </li>
</ul>
</li>
<li> STORE
<ul>
<li> society6 </li>
<li> redbubble </li>
</ul>
</li>
<li> CONTACT </li>
<li> ABOUT </li>
</ul>
</section>
CSS :
.menu {
height:29px;
width:100%;
/*background:orange;*/
}
.menu ul {
width:auto;
list-style-type:none;
font-family:"calibri", "arial";
}
.menu ul li {
position:relative;
display:inline;
float:left;
width:auto;
border-right: 2px solid purple;
margin-left:10px;
line-height:12px;
}
.menu ul li a {
display:block;
padding:3px;
color:#854288;
text-decoration:none;
font-size:20px;
font-weight:strong;
padding-right:25px;
}
.menu ul li a:hover, .active {
color:#788d35
}
.menu ul li ul {
display:none;
}
.menu ul li:hover > ul {
display:block;
position:absolute;
top:23px;
float:left;
padding-left:20px;
text-align:left;
margin-left: -30px;
}
.menu ul li ul li {
position:relative;
min-width:135px;
max-width:1350px;
width:100%;
}
.menu ul li ul li a {
padding: 3px;
margin-left: 1px;
border-right: hidden; /* <---- DOES NOT WORK */
}
This removes border from the main menu (after the last item About) :
.menu ul li:last-child{ border:none; }
JSFiddle
If you also want to remove border from the nested lis, you should add border:none to .menu ul li ul li :
JSFiddle
try this
#right_border_less{
border:solid;
border-right:none;
}

How to stop nested list overlapping parent list?

See here: http://jsfiddle.net/wHztz/67/
In this example the colors are placeholders for background images, I noticed the nested list stops overlapping when the display:block is removed from .innerLeft ul li a but then the background images wont show correctly.
I don't have much experience with CSS. Is there a way around this? Thanks!
HTML:
<div class="innerLeft">
<ul>
<li>Fruit</li>
<li>
<ul>
<li>Apples</li>
<li>Apples</li>
<li>Apples</li>
<li>Apples</li>
<li>Apples</li>
<li>Apples</li>
<li>Apples</li>
<li>Apples</li>
</ul>
</li>
<li>Vegetable</li>
</ul>
</div>
CSS:
.innerLeft ul {
width:199px;
float:left;
margin:0px;
padding:0px 0 0 12px;
list-style:none;
min-height:10px;
}
.innerLeft ul li{
padding:0px;
margin:0px 0 10px 0;
height:18px;
}
.innerLeft ul li a{
background: red;
display:block;
}
.innerLeft ul li ul li a{
background: blue;
}
.innerLeft ul li {
clear: left; /* Added */
padding:0px;
margin:0px 0 10px 0;
height:18px;
}
http://jsfiddle.net/wHztz/70/
use display:inline-block; instead
demo
.innerLeft ul li a{
background: red;
display:inline-block;
}
Its happening because you have applied css to ul li a and not ul li ul :)

CSS horizontal menu with sub options

I'm trying to add sub menu under main menu items but failed to do so. I've added display:none to hide sub menu, display:block to show when mouse is over main menu etc to some of the tags in CSS but none of them worked. Perhaps I added to wrong places.
I've cleared all my faulty codes not to deal with messy code instead giving you clear one to modify it.
Sub menu shouldn't be visible unless mouse is over its parent menu. Also sub menu should appear right under its parent menu.
Thanks in advance
CSS
<style>
.menu{
width: 100%;
background-color: #666666; }
.menu ul{
margin: 0; padding: 0;
float: left;}
.menu ul li{
display: inline;
position: relative;}
.menu ul li a{
float: left; text-decoration: none;
color: white;
padding: 10.5px 11px;
background-color: #333; }
.menu ul li a:hover, .menu ul li .current{
color: #fff;
background-color:#0b75b2;}
</style>
HTML
<div class="menu">
<ul>
<li>HOME
<ul>
<li>Home</li>
<li>Home short</li>
<li>Home very long</li>
</ul>
</li>
<li>ADMINISTRATOR
<ul>
<li>Admin</li>
<li>Admin short</li>
</ul>
</li>
<li>STAFF
<ul>
<li>staff</li>
</ul>
</li>
<li>LOGOUT</li>
</ul>
<br style="clear:left"/>
</div>
Change to .menu ul li ul{
position: absolute;}
If I understand your problem correctly, works fine now.
so..
.menu ul li ul{
position:absolute;
margin-top:40px;
width:150px;}
.menu ul li ul li{
display:block;}
To hide until hover, .menu ul li ul{ display:none; } .menu ul li:hover ul{display:block; }

help with css menu with drop downs

I am new to css, and have tried to make this work for a while with no luck
basically i want to display a drop down when "MENU" is selected but nothing seems to happen here is my css:
#tabs {
margin-top:-12ex;
float:left;
width:100%;
font-size:100%;
line-height:10px;
vertical-align:top;
margin-left:20px;
position:static;
}
#tabs ul {
margin:0;
padding:1px 1px 0 20px;
list-style:none;
}
#tabs li {
display:inline;
margin:0;
padding:5;
}
#tabs a {
float:left;
background:url("../images/tableft.gif") no-repeat left top;
margin:0;
padding:0 0 0 3px;
text-decoration:none;
}
#tabs a span {
float:left;
display:block;
background:url("../images/tabright.gif") no-repeat right top;
padding:10px 10px 10px 10px;
color:#FFF;
}
/* Commented Backslash Hack hides rule from IE5-Mac \*/
#tabs a span {float:none;}
/* End IE5-Mac hack */
#tabs a:hover span {
color:#FFF;
}
#tabs a:hover{
background-position:0% -42px;
}
#tabs a:hover span {
background-position:100% -42px;
}
div#tabs ul ul,
div#tabs ul li:hover ul ul,
div#tabs ul ul li:hover ul ul
{display: none;}
div#tabs ul li:hover ul,
div#tabs ul ul li:hover ul,
div#tabs ul ul ul li:hover ul
{display: block;}
Here is my html
<body>
<div id="wrapper">
<table height="860px">
<tr>
<td colspan="2" width="710px" height="150px">
</td>
</tr>
<tr>
<td colspan="2" width="710px" height="50px" valign="bottom">
<div id="tabs">
<ul>
<li><span>HOME</span></li>
<li><span>CATERING</span></li>
<li><span>MENU</span></li>
<ul>
<li>
<span>hey</span> //i want this to pop when hovering menu
<li>
</ul>
<li><span>RESERVATIONS</span></li>
<li><span>EVENTS</span></li>
<li><span>ABOUT US</span></li>
<li><span>CONTACT US</span></li>
</ul>
</div>
</td>
</tr>
</table>
</body>
Well once I fixed the few typos. I realised that you hadn't put the sub menu inside the <li> of the MENU.
here's the new list html, the css is fine for now:
<div id="tabs">
<ul>
<li><span>HOME</span></li>
<li><span>CATERING</span></li>
<li><span>MENU</span>
<ul>
<li>
<span>hey</span>
</li>
</ul>
</li>
<li><span>RESERVATIONS</span></li>
<li><span>EVENTS</span></li>
<li><span>ABOUT US</span></li>
<li><span>CONTACT US</span></li>
</ul>
</div>
Edit:
You need to make the inside <ul> appear absolute in the css, something like this:
#tabs {
width:100%;
}
#tabs>ul>li {
display:block; position:relative;
float:left;
list-style:none outside;
margin:0 10px;
}
#tabs>ul>li:hover ul{display:block}
#tabs>ul>li>a{
display:block;
}
#tabs>ul>li ul{
position:absolute; display:none;
list-style:none;
}
You need to put that secondary list inside your menu, like this:
<li>
<span>MENU</span></li>
<ul>
<li>
<span>hey</span>
</li>
</ul>
</li>
Also, remember to use the css selector > when you want to put rules on the first list only:
#tabs>ul>li{
/* css */
}