CSS: Menu Dropdown showing horizontally when I need vertical - html

I am working on a navigational menu. I have a problem with the dropdown submenus. When I hover over the submenu, it shows the entries horizontally, but I want it vertically. Can someone please show me what I can do to fix it? Thanks.
Here's a fiddle.
Here is my CSS code:
#nav {overflow:hidden;
margin:0px auto;
text-align:center;}
#nav ul li {display:inline-block;
list-style:none;
vertical-align:top;
width:100px;}
#nav>ul>li>a>img {text-align:center;
width:100px;
height:100px;}
#nav a {text-decoration:none !important;}
#nav a:hover {text-decoration:underline !important;}
#nav > ul > li > ul {display:none;
z-index:999;
position:absolute;}
#nav li:hover ul {display:block;}
.submenu li {border-bottom:solid 1px #333333;
font-size:12px;}
.submenu img {display:inline;
vertical-align:middle;
width:25px;
height:25px;
float:left;
margin:5%}
.submenu li:hover {background-color:#CCC;}
Here is my HTML Code:
<div id="nav">
<ul>
<li>
<a href="">
<img src="images/placeholder.png">
Home
</a>
</li>
<li>
<a href="">
<img src="images/placeholder.png">
Menu w/ Submenu
</a>
<ul class="submenu">
<li>
<a href="">
<img src="images/placeholder.png">
Submenu Item
</a>
</li>
<li>
<a href="">
<img src="images/placeholder.png">
Submenu Item
</a>
</li>
</ul>
</li>
<li>
<a href="">
<img src="images/placeholder.png">
Menu Item
</a>
</li>
<li>
<a href="">
<img src="images/placeholder.png">
Menu Item
</a>
</li>
</ul>
</div>

The easiest way is to comment position:absolute; in your submenu.
#nav > ul > li > ul {display:none;
z-index:999;
/* position:absolute;*/}
Check this in action:
http://jsfiddle.net/5Kjg4/

Here is a DEMO
You have css class #nav ul li which makes sub menu align horizonatally..
one way to solve this is using !important property and ovveriding display property.
Better solution would be using psuedo classes
#nav ul li:not(.subItem) {display:inline-block;
list-style:none;
vertical-align:top;
width:100px;}
.subItem{
display:block;
}
this way inline-block is not applied to subItems.

you can add this to you css
// if you need position: absolute
#nav ul li ul li {
float: left; // float left for inner li
display: inline-block;
}
#nav ul li ul {
width: 100px; // set width for you'r ul
}

Change the following css in your code and it should work fine.
Problem was that you are displaying inline-block for all ul li.
Take a look at fiddle DEMO
#nav ul li {
display:inline-block;
list-style:none;
vertical-align:top;
width:100px;
}
To
#nav > ul > li {
display:inline-block;
list-style:none;
vertical-align:top;
width:100px;
}

Here is the solution to you problem
You just had to add display:block and float to your submenu li

Related

Floating menu jumps to top of page when sub-menu clicked

I have a floating pop-out menu (position:fixed) that is giving me a weird little problem if you click it after you have scrolled down the page. If your mouse happens to be where the sub-menu overlaps with the main menu when it opens, everything is fine. However if your mouse is not in the overlap, the page jumps right to the top (which I think some people might find a little confusing).
Is there any way to fix this, preferably just using just HTML and CSS?
HTML:
<div id="menu">
<ul class="levelone" >
<li class="active"> Home</li>
<li class="fly"> 1</li>
<li class="fly"> 2 »
<ul class="dropdown d1">
<li class="fly">2a</li>
<li class="fly">2b</li>
<li class="fly">2c</li>
</ul>
</li>
<li class="fly"> 3 »
<ul class="dropdown d1">
<li>3a</li>
<li>3b</li>
</ul>
</li>
<li class="fly"> 4</li>
</ul>
</div>
CSS:
#menu {display:initial; position:fixed; z-index:500; width: 150px;vertical-align: top;line-height: 200%;}
#menu ul {padding:0; margin:0; list-style:none; padding: 0;list-style: none;margin: 0px;}
#menu ul ul {z-index:501; position:absolute; left:-9999px; width:150px;}
#menu ul li {margin-right:5px; float:left;position:relative;line-height: 200%;position: relative;}
#menu ul li a {display:block; float:left; text-decoration:none;width: 150px;;display: block;line-height: 200%; text-align:center; border: 1px solid grey;background: white; }
#menu ul ul li {margin:0;}
#menu ul ul li a {text-align:center; width:144px;}
#menu ul li a:active + ul.dropdown {left:130px; }
#menu ul li a:focus + ul.d1,
#menu ul li a:focus + ul.d2 {left:130px}
#menu ul li ul.dropdown:hover {left:130px}
#menu ul li ul li{background: #999999}
#menu ul li a:hover{ color: #FFF; background: #333;}
#menu li.active > a,
#menu li.active > a:hover,
#menu li.active > a:focus { color: #fff; background-color: #337ab7;}
Fiddle here.
This behaviour is due to the href attribute in the higher level link.
The anchor tag <a> should be used only to navigate, not as dummy "focusable" item. The problem is that these high level buttons are intended to open a sub-menu, not to navigate to a given location in the document.
Solution
Typically, pure HTML/CSS dropdown menus are hard to set up, but given what you have done, I would suggest you to use some unstyled <button> tags for the upper level items (and <a> for actual anchors). They have the benefit to better fit semantically and free you from placeholders like href="#" or href="javascript:void(0)".
Beyond pure semantic, the solutions with the dummy href is more hacky and a bit intrusive because some browsers shows them as the link target on the bottom of the screen (when it should be hidden from users perspective). As a user its really annoying when such content is displayed.
I recommend this question about using a button or a link in the first place.
Code
Here is a working jsFiddle with proper semantic and clean rendering.
The # in your link href will link to the top of the page (causing your jump).
Replace it with this <a href="javascript:void(0)">
When the # is used in an anchor, it is usually followed by an ID e.g. <a href="#HelpSection"> this is a way that you can link to an ID on the current page (or even a different page). As you are not adding an ID, it will jump to the top of the page.
Update css and html with this code, i had made update as i understand
#menu {display:initial; position:fixed; z-index:500; width: 150px;vertical-align: top;line-height: 200%;top:10px;}
#menu ul {padding:0; margin:0; list-style:none; padding: 0;list-style: none;margin: 0px;}
#menu ul ul {z-index:501; position:absolute; left:-9999px; width:150px;}
#menu ul li {margin-right:5px; float:left;position:relative;line-height: 200%;position: relative;}
#menu ul li a {display:block; float:left; text-decoration:none;width: 150px;;display: block;line-height: 200%; text-align:center; border: 1px solid grey;background: white; }
#menu ul ul li {margin:0;}
#menu ul ul li a {text-align:center; width:144px;}
#menu ul li a:active + ul.dropdown {left:130px; }
#menu ul li a:focus + ul.d1,
#menu ul li a:focus + ul.d2 {left:130px}
#menu ul li ul.dropdown:hover {left:130px}
#menu ul li ul li{background: #999999}
#menu ul li a:hover{ color: #FFF; background: #333;}
#menu li.active > a,
#menu li.active > a:hover,
#menu li.active > a:focus { color: #fff; background-color: #337ab7;}
#wrapper{padding-top: 180px;}
<div id="wrapper">
<div id="menu">
<ul class="levelone" >
<li class="active"> Home</li>
<li class="fly"> 1</li>
<li class="fly"> 2 »
<ul class="dropdown d1">
<li class="fly">2a</li>
<li class="fly">2b</li>
<li class="fly">2c</li>
</ul>
</li>
<li class="fly"> 3 »
<ul class="dropdown d1">
<li>3a</li>
<li>3b</li>
</ul>
</li>
<li class="fly"> 4</li>
</ul>
</div>
<p>Test
</p>
<p>Test
</p>
<p>Test
</p>
<p>Test
</p>
<p>Test
</p>
<p>Test
</p>
<p>Test
</p>
<p>Test
</p>
<p>Test
</p>
<p>Test
</p>
<p>Test
</p>
<p>Test
</p>
<p>Test
</p>
<p>Test
</p>
<p>Test
</p>
<p>Test
</p>
<p>Test
</p>
</div>

CSS - dropdown menu

I'm new with CSS and testing some stuff. I created a dropdown menu but this doesn't work correctly.
Here is my code:
<!DOCTYPE html>
<html>
<head>
<link href = "üben_css.css" rel = "stylesheet" type="text/css">
<title> xx</title>
</head>
<body>
<div id="nav">
<ul>
<li> Home </li>
<li> xx
<ul>
<li> Hc </li>
<li> Scc </li>
</ul>
<li>Zubehör </li>
<li> Service </li>
<li> Forum </li>
</ul>
</div>
</body>
</html>
CSS code:
body{
margin:0;
padding:0;
}
#nav{
background-color:silver;
position:relative;
}
#nav ul li{
display:inline;
}
#nav ul ul{
display:none;
}
#nav ul li:hover ul{
display:block;
}
#nav ul ul li{
display:block;
}
The problem is that the nav bar is displayed inline but when I hover over my "xx" element the rest of my nav bar appear under my dropdown list. How can I fix this?
http://www.fotos-hochladen.net/uploads/20140921004u0kxad3i2r.png
Remove these lines from your css:
#nav ul li:hover ul{
display:block;
}
Using
#nav ul li{
display:inline-block;
}
Compared to just inline. If it was just inline the absolute positioning below would cause multiple dropdowns to all appear in the same place according to the #nav div.
#nav ul li:hover ul{
position: absolute;
display:block;
}
This still doesn't look very good but it's functional. Checkout at tutsplus for some good tutorials.
At the same time, I'm sure you could solve this much more efficiently with jQuery.

absolute position stay with parent of parent instead of parent

I ran into some problem when trying to position an absolute positioned div. its working as its should i guess, however i want it to stay with parent of parent instead of parent becouse i have a dropdown list and it will follow the parent down on the side when i want it to stay in top like first li with div is displayed. ive created a jsfiddle to show the problem. http://jsfiddle.net/trptR/
can this be done using css only or is Javascript a must?
HTML
<div id="navmenu">
<ul>
<li>example
<ul>
<li>sub example1</li>
<li>sub example2</li>
<li>sub example3</li>
<li>sub example4</li>
</ul>
</li>
<li>Test
<ul>
<li>Sub Test 1
<div>
<ul>
<li>Projekt</li>
</ul>
</div>
</li>
<li>Sub Test 2
<div>
<ul>
<li>Projekt</li>
</ul>
</div>
</li>
</ul>
</li>
</ul>
</div>
CSS
#navmenu{
display:inline-block;
background:red;
}
#navmenu ul{
list-style:none;
margin:0;
padding:0;
}
#navmenu ul li{
float:left;
position:relative;
display:block;
padding:0.5em;
}
#navmenu ul li ul{
position:absolute;
display:none;
border:solid 1px #333;
background:#fff;
}
#navmenu ul li:hover ul{
display:inline-block;
}
#navmenu ul li ul li{
float:none;
display:block;
position:relative;
}
#navmenu ul li ul li:hover{
background-color:#EBEBEB;
}
#navmenu ul li ul li div{
display:none;
width:10em;
height:14.6em;
background:blue;
position:absolute;
top:0;
left:6em;
border:solid 1px #000;
}
#navmenu ul li ul li:hover div{
display:block;
}
Could you remove position:relative from both your #navmenu ul li style set and from you #navmenu ul li ul li style set?
http://jsbin.com/ziqov/1/edit
Positioning is key
I'm not sure I understand the explanation of your problem, but I do think I understand when you say you have
parentElementTop > parentElementBelow > element
that you want element to be aligned to parentElementTop rather than the parentElementBelow.
To align element absolutely to parentElementTop all you need to do in CSS is to not set position to relative or absolute on the intermediate parentElementBelow and any subsequent absolutely positioned element will be aligned according to last non-statically positioned ancestor. In your case that would be the parentElementTop which is what you want.
think about using
#navmenu > ul > li{
float:left;
position:relative;
display:block;
padding:0.5em;
cursor:pointer;
}
because otherwise your selectors overwrite each other.

Css ul display is none when hover display is block

I'm working on a dropdown box but the only thing that doenst want to work is when I hover over the word Info in the unordered list the dropdownbox is not displayed.
I know I have display:none; in ul style but can you change that to display:block; when hovered over the word info?
HTML Code:
<ul id="Menu">
<li>Home </li>
<li>Info
<ul style="display:none;">
<li>Program</li>
<li>Getting Started<li>
<li>Credits</li>
</ul>
</li>
<li>Video </li>
<li>server </li>
<li>Profile </li>
<li>Help</li>
</ul>
CSS Code:
#Menu {
list-style:none;
font-size:16px;
font-weight:Bold;
padding:14px 0 0;
width:415px;
margin-top:0;
float:left;
margin-left:0;
}
#menu { list-style:none;}
li:hover > ul {display:list-item;}
li ul { margin:0; padding:0; position:absolute;z-index:5;padding-top:6px;}
li { float:left; margin-left:10px; }
li ul li { float:none; margin:0; display:inline;}
li ul li a {display:block; padding:6px 10px; background:#333; white-space:nowrap;}
li { display: list-item; text-align: -webkit-match-parent;}
ul { border:0; font-size:100%; font:inherit;vertical-align:baseline;}
Help in advanced. any help is apreciated :)
also post a code not only telling me whats wrong thanks!
use #Menu before your css class names : DEMO
like: #Menu li:hover > ul {display:list-item;}
NOTIC: css Class names are Case-Sensitive
I am thinking this is because the html code contains an inline display:none.
In the inner <ul>, we should remove <ul style="display:none;"> in the above html.

how to make list inline with images and buttons?

im tying to make these two items inline with each other, each list item has an image and button, the button should be underneath each image: i.e
<div id="cocktails">
<ul>
<li>
<img src ="http://bokertov.typepad.com/.a/6a00d83451bc4a69e2014e8a8f894b970d-800wi" width="100px" height="100px">
<button>Select</button>
</li>
<li>
<img src ="http://bokertov.typepad.com/.a/6a00d83451bc4a69e2014e8a8f894b970d-800wi" width="100px" height="100px">
<button>Select</button>
</li>
</ul>
</div>
The CSS:
#cocktails ul{margin:0;padding:0}
#cocktails ul li{list-style-type:none;display:inline;padding:0}
#cocktails ul li img{width:150px;height:150px;display:block;float:left;padding:0 10px}
#cocktails ul li button{display:block;float:left}
but the buttons are not positioned as expected the jsfiddle is here:
http://jsfiddle.net/8KWGJ/
enclose buttons in li tags :
<div id="cocktails">
<ul>
<li>
<img src ="http://bokertov.typepad.com/.a/6a00d83451bc4a69e2014e8a8f894b970d-800wi" width="100px" height="100px">
</li>
<li> <button>Select</button>
</li>
<li>
<img src ="http://bokertov.typepad.com/.a/6a00d83451bc4a69e2014e8a8f894b970d-800wi" width="100px" height="100px">
</li>
<li> <button>Select</button>
</li>
</ul>
and comment css as shown:
#cocktails{float:left;width:419px}
#cocktails ul{margin:0;padding:0}
#cocktails ul li{list-style-type:none;display:inline;padding:0}
/*#cocktails ul li img{width:150px;height:150px;display:block;float:left;padding:0 10px}*/
/*#cocktails ul li button{display:inline;}*/
http://jsfiddle.net/8KWGJ/3/
I floated the <li> left, instead of the button and image inside it
Change the float from the image to the li.
DEMO
#cocktails ul {
margin:0;
padding:0;
}
#cocktails ul li{
list-style-type:none;
display:inline;
float: left;
padding:0;
}
#cocktails ul li img{
width:150px;
height:150px;
display:block;
padding:0 10px;
}
#cocktails ul li button {
display:block;
}
Setting your image to display:block; and the li to float:left; should solve your problem.
Example
I've removed all of your CSS and implimented only the required to solve your problem.
You can do something like this: JSFIDDLE
Change
#cocktails ul li{list-style-type:none;display:inline;padding:0}
#cocktails ul li img{width:150px;height:150px;display:block;float:left;padding:0 10px}
#cocktails ul li button{display:block;float:left}
to
#cocktails ul li{list-style-type:none;display:inline;padding:0;float:left;}
#cocktails ul li img{width:150px;height:150px;display:block;padding:0 10px}
#cocktails ul li button{display:block;margin:auto}
​
​
Add
clear:both; for the buttons and images.
Also give some margin to the button.
#cocktails{float:left;width:419px}
#cocktails ul{margin:0;padding:0}
#cocktails ul li{list-style-type:none;display:inline;padding:0;}
#cocktails ul li img{width:150px;height:150px;display:block;float:left;padding:0 10px; clear: both;}
#cocktails ul li button{display:block;float:left;clear:both; margin-left: 60px;}
http://jsfiddle.net/8KWGJ/8/
Sometimes display block does not work. So in that use clear with display:block; You can clear either left,right or both. So use clear as per the requirement.
Hope this helps.
This did the trick for me.
#cocktails ul{margin:0;padding:0}
#cocktails ul li{list-style-type:none; width:150px; float:left; margin:10px;}
#cocktails ul li img{width:150px;height:150px; }
#cocktails ul li button{ margin-left:50px; width:50px}