Proper submenu using only HTML+CSS - html

I'm trying to write a simple HTML+CSS menu without any absolute positioning or JS. My problem is regarding submenus, where it either expand the current selected item or displace it:
The HTML is straightforward:
<ul id="menu">
<li>Item 1</li>
<li>Folder 1
<ul>
<li>Item 2</li>
</ul>
</li>
<li>Item 3</li>
</ul>
And so is the CSS:
#menu, #menu ul {
border-style: solid;
border-width: 0px;
border-top-width: 1px;
float: left;
list-style: none;
margin: 0px;
padding: 0px;
width: 180px;
}
#menu li ul {
background-color: cyan;
display: none;
position: relative;
right: -168px;
width: auto;
}
#menu li:hover ul {
display: block;
}
#menu li {
border-style: solid;
border-width: 1px;
border-top-width: 0px;
padding: 10px;
}
#menu li:hover {
background-color: lightgrey;
font-weight: bold;
}
I thought the submenu could only affect the layout after being repositioned, which seems
to not be the case here. I'm a bit at a loss.

Using this type of menu pattern, you should use position:relative on the parent LI of the sub-menu, and position:absolute on the UL child menu. The allows the child element to appear outside of the layout flow, relative to its parent.
It's also good practice to put all non-position styling on the A-tag inside each LI and use display:block. It would be difficult for you to style the text on "Folder 1" without it.
Simple example: http://jsfiddle.net/Diodeus/jejNy/127/

Use position:absolute on the ul and position:relative on the LI:
HTML:
<ul id="menu">
<li>Item 1</li>
<li>Folder 1
<ul>
<li>Item 2</li>
</ul>
</li>
<li>Item 3</li>
</ul>
CSS:
#menu, #menu ul {
border-style: solid;
border-width: 0px;
border-top-width: 1px;
float: left;
list-style: none;
margin: 0px;
padding: 0px;
width: 180px;
}
#menu li ul {
background-color: cyan;
display: none;
position: absolute;
top:-1px;
left: 178px;
}
#menu li:hover ul {
display: block;
}
#menu li {
position:relative;
border-style: solid;
border-width: 1px;
border-top-width: 0px;
padding: 10px;
}
#menu li:hover {
background-color: lightgrey;
font-weight: bold;
}
Check out this CodePen

Related

CSS dropdown menu display problems

I am working on a project and I am assigned to show a nested menu, I mean Drop down menu but I dont know what am I doing wrong here. Can anyone help me out ?
HTML
<div id="nav">
<ul>
<li>Menu Num 1</li>
<li>Menu Num 2
<ul>
<li>Sub Menu 2.1</li>
<li>Sub Menu 2.2</li>
<li>Sub Menu 2.3
<ul>
<li>Sub Sub Menu 2.3.1</li>
<li>Sub Sub Menu 2.3.2</li>
<li>Sub Sub Menu 2.3.3</li>
<li>Sub Sub Menu 2.3.4</li>
</ul>
</li>
<li>Sub Menu 2.4</li>
</ul>
</li>
<li>Menu Num 3</li>
<li>Menu Num 4</li>
</ul>
</div>
CSS
* {
margin: 0px;
padding: 0px;
}
#nav ul {
list-style-type: none;
font-size: 0px;
}
#nav ul li {
display: inline-block;
position: relative;
}
#nav ul li a {
padding: 10px;
display: block;
font-size: 12px;
text-decoration: none;
font-family: sans-serif;
border: 1px solid #008080;
margin: 5px;
}
#nav ul li a:hover {
background: #a1a1a1;
}
#nav ul ul {
display: none;
}
#nav ul li:hover > ul {
position: absolute;
display: block;
width: 100%;
}
My only problem is the li items are not shown properly under each parent li. I need a solution and for further code inspection I have a jsFiddle link.
I have a solution of your problem .you have to do some changes in your css sheet .I add a new block on content in css sheet which helps you to solve your problem.
* {
margin: 0px;
padding: 0px;}
#nav ul {
list-style-type: none;
font-size: 0px;
}
#nav ul li {
display: inline-block;
position: relative;
}
#nav ul li a {
padding: 10px;
display: block;
font-size: 12px;
text-decoration: none;
font-family: sans-serif;
border: 1px solid #008080;
margin: 5px;
}
#nav ul li a:hover {
background: #a1a1a1;
}
#nav ul ul {
display: none;
}
#nav ul li:hover > ul{
position: absolute;
display: block;
width: 100%;
}
#nav ul ul li:hover > ul{
position: absolute;
margin-left:100px;
top:0px;
display: block;
width: 100%;)/* CSS Document */
Trying adding display:block; to your top level hover class
#nav ul li a:hover {
background: #a1a1a1;
display:block;
}
I came across this which may be useful for you http://htmldog.com/techniques/dropdowns/

How to add a dropdown to this simple navbar?

Sorry if this is a silly question but I'm looking to add a dropdown list of items to this navbar. It's in a navbar.php file and here is the code:
<link rel="stylesheet" href="/styles/navbar.css" type="text/css" /> <!-- navbar styles -->
<ul id="nav">
<li>Home</li>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
Here's the navbar.css file:
#nav
{
width: 100%;
float: left;
margin: 0 0 3em 0;
padding: 0;
list-style: none;
background-color: #242424;
border-bottom: 1px solid #ccc;
border-top: 1px solid #ccc;
position:fixed;
top:0px;
}
#nav li
{
float: left;
}
#nav li a
{
display: block;
padding: 8px 15px;
text-decoration: none;
font-weight: bold;
color: #7ACC01;
border-right: 1px solid #ccc;
}
#nav li a:hover
{
color: #c00;
background-color: #fff;
}
So my question is, is there a simple way of adding a dropdown list of items to "Item 4" for example, where the dropdown menu will appear on mouseover?
I would like you to learn how we do that, instead of giving you the fish. With a rod, you can always fish by yourself.
Look at what I have done in this fiddle:
http://jsfiddle.net/jLkeH/
It actually comes to this:
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
You have to hide ul you want to toggle and set it back to visible if someone hovers on it.
PS: as you can see, I used HTML5, which is recommended, because it is (more) semantic.

submenu items won't line up with parent item

I am trying to create a drop down menu, however whenever I hover over a parent menu the child menu won't line up with it. I have gone through many examples and I always get the same result.
My CSS code is
#mainNav {
margin-top: 20px;
width: 800px;
margin-left: auto;
margin-right: auto;
padding: 0;
overflow: hidden;
background-color: #BBFFFF;
zoom: 1;
}
#mainNav li {
float: left;
list-style: none;
}
#mainNav li a {
color: #000000;
display: block;
width: 80px;
font-size: 14px;
text-align: center;
text-transform: uppercase;
text-decoration: none;
padding: 7px 0px 7px 0px;
border-right: 1px solid #999;
zoom: 1;
}
#mainNav li ul {
display: none;
}
#mainNav a:hover {
background-color: #66FF66;
}
#mainNav li:hover ul {
display: block;
position: absolute;
}
#mainNav li:hover li {
float: left;
width: 80px;
background-color: #BBFFFF;
margin: 0;
padding: 0;
}
My HTML is
<div>
<ul id="mainNav">
<li>Page One</li>
<li>Page Two</li>
<li>Page Three
<ul>
<li><a href="threePageOne.html" name="threePageOneLink>Sub Page One</a></li>
</ul>
</li>
</ul>
</div>
You could use a negative margin-left to pull it back in line. Add it to:
#mainNav li:hover ul {}
Or you can set padding: 0; on the same element.
Also, you are missing a closing '"' in:
<li><a href="threePageOne.html" name="threePageOneLink>Sub Page One</a></li>
Copy paste and try this. Something from this example will help you.
<html>
<head>
<style type="text/css">
#mainNav{
/* Your mainNav decoration here */
}
#mainNav li{
list-style:none;
display:inline-block;
background-color:#0FF;
padding:10px;
border:1px solid #000;
}
#mainNav ul{
position:absolute;
display:none;
padding:0px;
top:50px;
}
#mainNav li:hover ul{
display:inline-block;
}
</style>
</head>
<body>
<ul id="mainNav">
<li>Page One
<br />
<ul>
<li>Sub Page One</li>
</ul>
</li>
<li>Page Two</li>
<li>Page Three
<br />
<ul>
<li>Sub Page One</li>
</ul>
</li>
</ul>
</body>
</html>
There is a lot of css in there that is not specific enough and some stuff that is just in the wrong place.
I have written out a jsfiddle for you here: A DISTILED VERSION OF YOUR CODE
You have list style on li instead of ul where it belongs. You are also not specific enough about the sub lists and so one. Using > like,
.main-nav > li > a { } will say target the first layer of li only, and in that, the only a in that li etc.
I would give the ul's classes etc. See the fiddle for what I think is the most simple approach. For the other people, please let me know if my fiddle could be more concise.

Setting dropdown width longer than menu

I have been trying different things and still have been able to get exactly what I wanted. I want to create a menu. I want the menu to be about 20px wide and the dropdown to be 80-100px.
<div id="menulevel2grouping2">
<ul>
<li>T
<ul>
<li>Type 1</li>
<li>Type 2</li>
<li>Type 3</li>
<li>Type 4</li>
</ul>
</li>
<li>S
<ul>
<li>Small</li>
<li>Medium</li>
<li>Large</li>
</ul>
</li>
</ul>
</div>
#menulevel2grouping2{
margin: 0px;
padding: 0px;
}
#menulevel2grouping2 ul{
margin: 0px;
padding: 0px;
line-height: 30px;
}
#menulevel2grouping2 li{
margin: 0px;
padding: 0px;
list-style: none;
float: left;
position: relative;
}
#menulevel2grouping2 ul li a{
text-align: center;
font-family: Helvetica, Verdana Arial, sans-serif;
font-size: 12px;
background-color: blue;
width: 60px;
text-decoration: none;
display: block;
color: #000000;
}
#menulevel2grouping2 ul ul{
position: absolute;
display: none;
top: 30px
}
#menulevel2grouping2 ul li:hover ul{
display: block;
}
Any ideas on how I could get it done?
Thanks!
Drjay
Is this the effect you want? See fiddle here: http://jsfiddle.net/scTgZ/
I have revised your css. See the fiddle.
First of all, whenever possible you should provide a class or id to your elements to avoid complications. That's the purpose of CSS, to facilitate things, not to complicate them.
Anyway, something like this should work:
#menulevel2grouping2 ul li{width:20px; position:relative; z-index:10}
#menulevel2grouping2 ul li ul li{width:200px; position:relative; z-index:10}
you may not need the position and z-index (I'm not testing it) and you may need some float for the upper li, but this should be more than enough to give you an idea

Absolute <ul> for dropdown shows at wrong place on IE7

If you try that in FireFox:
http://jsfiddle.net/rJUKT/2/
it works fine. The dropdown menu shows at the bottom of its parent like that:
But if you try it with IE7, the dropdown menu shows at the right, like that:
Any idea why it does that?
HTML
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<ul id="menu">
<li>
<span>Menu 1</span>
<ul>
<li>Link 1.1</li>
<li>Link 1.2</li>
<li>Link 1.3</li>
<li>Link 1.4</li>
</ul>
</li>
<li>
<span>Menu 2</span>
<ul>
<li>Link 2.1</li>
<li>Link 2.2</li>
<li>Link 2.3</li>
<li>Link 2.4</li>
</ul>
</li>
</ul>
CSS
#menu { width: 100%; float: right; list-style: none; margin-top: 5px; color: #2A4153; }
#menu li {
float: left;
font-size: 17px;
font-weight: bold;
background-color: #e1f1ff;
border: 1px solid #93b5d4;
margin: 0 1px 0 1px;
padding: 4px 0 0 0;
border-bottom: none;
height: 20px;
}
#menu li a, #menu li span { padding: 4px 7px 2px 7px; cursor: pointer; }
#menu li ul {
clear: both;
position:absolute;
display:none;
padding:0;
list-style:none;
width: 150px;
margin: -1px 0 0 -2px;
}
#menu li ul li {
float: left;
width: 150px;
border: 1px solid #93b5d4;
border-top: none;
font-size: 15px;
font-weight: normal;
background-image: url('16x16/eye.png');
background-position: 4px 4px;
background-repeat: no-repeat;
-moz-border-radius: 0;
}
#menu li ul li a, #menu li ul li span { display:block; padding-left: 25px; }
#menu li ul li:hover { background-color: #e5f6e8; border: 1px solid #93d4a2; border-top: none; }
#menu li:hover { background-color: #e5f6e8; border: 1px solid #93d4a2; border-bottom: none; }
JS
$(function() {
$('#menu li').hover(
function () {
//show its submenu
$('ul', this).slideDown(100);
},
function () {
//hide its submenu
$('ul', this).slideUp(100);
}
);
});
Thanks!
You need to set "top" and "left" properties for dropdown UL
You can check it out this. Some css properties updated in this
http://jsfiddle.net/vkVHC/
If that's the only reason you're using jQuery (for the dropdown effect on hover), you may as well do it using CSS instead (and thus save many kBs being loaded by the site). Also, Alexei's answer is correct.