Simple CSS Dropdown - html

I have made a few attempts at this and they ended up just getting confusing. The current HTML and CSS seems to be working fine for a simple horizontal CSS menu. I am struggling with dropping subitems off the current <li> elements.
I am trying to making them show up exactly below the current menu items with the same hovering effects as I have in place now. Any assistance would be appreciated. I am admittedly no CSS pro.
Current HTML:
<div class="MenuTop">
<ul class="Nav">
<li>Home</li>
<li>Vehicles
<ul>
<li>SubItemOne</li>
</ul>
</li>
<li>About</li>
<li>Contact</li>
<li>News</li>
</ul>
</div>
Current CSS:
.MenuTop
{
width: 960px;
background-color: Black;
color: #fff;
margin: auto auto 0px auto;
padding: 5px;
height:auto;
font-family: Segoe UI, Arial;
font-weight:bold;
min-height:15px;
}
.MenuTop ul
{
float: left;
list-style: none;
margin: -5px ;
padding: 0px;
}
.MenuTop li
{
float: left;
font-size:12px;
font-family: Segoe UI, Arial;
margin: 0;
padding: 0;
}
.MenuTop a
{
background-color: Black;
color: #fff;
display: block;
float: left;
margin: 0;
padding: 4px 12px 4px 12px;
text-decoration: none;
}
.MenuTop a:hover
{
background-color: #404040;
color: #fff;
padding: 4px 12px 4px 12px;
}

You were close, but you're forgetting about positioning your submenu items absolutely to your parent li menu item and hiding it as well, by using display:none and then showing it on hover, so try something like this to achieve that effect:
CSS
.Nav li {
position:relative;
}
.Nav li ul {
display:none;
position:absolute;
top:30px;
}
.Nav li:hover ul {
display:block;
}
Also, your submenu ul is not properly closed so go ahead and close it.
Ninja Edit: demo, by the way you can greatly benefit from properly targeting your menu by using the class you have given it, .Nav, instead of its parent class of its container, .MenuTop, that way you can target your menu and your menu alone and not any other element you might place inside that container,

I have created a working example for you on jsFiddle.
The HTML is as follows:
<nav>
<ul class="cf">
<li>Home</li>
<li>Vehicles
<ul>
<li>Sub-menu Item 1</li>
<li>Sub-menu Item 2</li>
<li>Sub-menu Item 3</li>
</ul>
</li>
<li>About</li>
<li>News</li>
</ul>
</nav>​
and the CSS:
nav ul {
background: #ddd;
list-style: none;
margin: 0;
padding: 0;
width: 100%;
}
nav li {
float: left;
margin: 0;
padding: 0;
position: relative;
min-width: 25%;
}
nav a {
font-family: Lucida Grande;
background-color: #666666;
-webkit-background-clip: text;
-moz-background-clip: text;
background-clip: text;
color: transparent;
text-shadow: rgba(255,255,255,0.5) 0px 3px 3px;
display: block;
font: bold 14px sans-serif;
padding: 0 25px;
text-align: center;
text-decoration: none;
}
nav li ul {
float: left;
left: 0;
opacity: 0;
position: absolute;
top: 1em;
visibility: hidden;
z-index: 1;
}
nav li:hover ul {
opacity: 1;
top: 1em;
visibility: visible;
}
nav li ul li {
float: none;
width: 100%;
}
.cf:after, .cf:before {
content:"";
display:table;
}
.cf:after {
clear:both;
}
.cf {
zoom:1;
}​

Related

How do I include short vertical lines in between links in navbar?

What I'm looking for:
/
What I've got:
I want to include small vertical lines, evenly spaced with matching color, in between the links in my navbar. Below is the CSS code I've written. I'm new to coding and I've searched Google but I keep seeing the same answer which got me these huge lines that I don't want. ???
/* GLOBAL */
body {
margin: 0 auto;
background: grey;
font-family: 'Arial', 'Helvetica Neue', Helvetica, sans-serif;
font-weight: 300;
}
/* NAVBAR */
header {
background: #ffffff;
max-width: 100%;
border-bottom: 2px solid #777777;
}
header::after {
content: '';
display: table;
clear: both;
}
.container {
margin: 0 auto;
max-width: 960px;
}
.logo {
float: left;
background-color: #4aaaa5;
color: #ffffff;
margin: 0;
padding: 20px 30px;
display: inline-block;
font-family: 'Georgia', Times, 'Times New Roman', serif;
}
nav {
float: right;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
padding-top: 30px;
margin-left: 20px;
**border-right: 1px solid #777777;**
}
nav a {
color: #777777;
text-decoration: none;
}
nav li:last-of-type {
**border-right: none;**
}
nav a:hover {
color: #4aaaa5;
}
===========================================================================
<body>
<header>
<div class="container">
<h1 class="logo">Name</h1>
<nav>
<ul>
<li><a class="active" href="about.html">About</a></li>
<li>Portfolio</li>
<li>Content</li>
</ul>
</nav>
</div>
</header>
</body>
You can consider using pseudo elements. That'll do the trick.
Have a look at this
ul {
list-style:none;
}
ul li{
display:inline-block;
padding:0 7px;
position:relative;
}
ul li:not(:last-child)::after{
content:"";
border:1px solid #e2e2e2;
border-width: 1px 1px 0 0;
position:absolute;
right:-3px;
top:0;
height:100%;
}
<ul>
<li>Home</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
Try adding the border-right to each li tag except the last li tag. And add padding to the each li tag.
like the below code
.nav li{
border-right: 2px solid #c4c4c4;
display: inline-block;
float: left;
padding: 0 15px;
}
.nav li:last-child {
border-right:0;
}
Below is the full working demo here. Hope it helps you.
.nav{
list-style: none;
padding: 0;
margin: 0;
margin-top: 25px;
float: left;
}
.nav li{
border-right: 2px solid #c4c4c4;
display: inline-block;
float: left;
padding: 0 15px;
}
.nav li:last-child {
border-right:0;
}
<ul class="nav">
<li>About</li>
<li>Home</li>
<li>Contact</li>
</ul>
You can do this by using border-right or the way below.
.nav{
list-style: none;
padding: 0;
margin: 0;
margin-top: 25px;
float: left;
}
.nav li{
display: inline-block;
position: relative;
float: left;
padding: 0 15px;
}
.nav li::after {
position: absolute;
width: 2px;
right: 0;
content: ' ';
height: 100%;
background: rgba(0, 0, 0, 0.2);
}
.nav li:last-child::after {
width: 0;
}
<ul class="nav">
<li>About</li>
<li>Home</li>
<li>Contact</li>
</ul>
working demo
<ul class="nav">
<li>About</li>
<li>Home</li>
<li>Contact</li>
</ul>
.nav{
list-style: none;
padding: 0;
margin: 0;
margin-top: 25px;
float: left;
}
nav li {
display: inline-block;
padding-top: 30px;
padding-left:10px
padding-right:10px;
}
.nav li:after{
content:'|';
display:inline-block;
color:#dddddd;
vertical-align:middle;
}
.nav li:last-child:after {
display:none;
}
Add the separator image as a background image on the li.
#nav li + li {
background:url('seperator.gif') no-repeat top left;
padding-left: 10px
}

How to add second drop down menu to the first one?

i make a drop down menu with css, but i want to ask you, how can i add secondary drop down to the first? If it can't be made with this code, how can i make it? I try to use droppy, but the code bugged i mean the style..
I use this HTML:
<ul id="menu">
<!-- put class="selected" in the li tag for the selected page - to highlight which page you're on -->
<li class="selected">Index</li>
<div class="dropdown">
<li>Drop Down</li>
<div class="dropdown-content">
Link 1
Link 2
Link 3 --> Here i want to add another drop down menu
Link 4
</div>
</div></ul>
And this CSS:
ul#menu
{ float: right;
margin: 0;}
ul#menu li
{ float: left;
list-style: none;
margin: 2px 2px 0 0;
background: #5A5A5A url(tab.png) no-repeat 0 0;
border-top-right-radius: 1.5em;
border-top-left-radius: 1.5em;
overflow:hidden;
max-height: 27px;}
ul#menu li a
{ font: normal 100% 'trebuchet ms', sans-serif;
display: block;
float: left;
height: 20px;
padding: 6px 35px 5px 28px;
text-align: center;
color: #FFF;
text-decoration: none;
background: #5A5A5A url(tab.png) no-repeat 100% 0;}
ul#menu li.selected a
{ height: 20px;
padding: 6px 35px 5px 28px;}
ul#menu li.selected
{ margin: 2px 2px 0 0;
background: #00C6F0 url(tab_selected.png) no-repeat 0 0;
}
ul#menu li.selected a, ul#menu li.selected a:hover
{ background: #00C6F0 url(tab_selected.png) no-repeat 100% 0;
color: #FFF;}
ul#menu li a:hover
{ color: #E4EC04;}
/* The container <div> - needed to position the dropdown content */
.dropdown {
float:left;
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
margin-top: 30px;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
border-radius: 15px;
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
border-radius: 15px;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* Change the background color of the dropdown button when the dropdown content is shown */
.dropdown:hover .dropbtn {
background-color: #3e8e41;
}
I should say that I kept slogging for more than an hour to get this up. As I have just learned HTML and CSS. But enjoyed doing it as it made me learn something new. Thanks.
Here is what I have so far:
CSS:
#menu, #first, #second, #third {
list-style-type: none;
border: 1px solid black;
}
#menu {
text-align: center;
padding-right: 10px;
padding-left: 10px;
width: 70px;
margin: 25px;
}
#first, #second, #third {
position: absolute;
background-color: #f9f9f9;
width: 50px;
display:none;
padding-left: 0;
width: 90px;
margin-left: 25px;
margin-right: 25px;
}
a {
text-decoration: none;
color: black;
}
#menu:hover #first {
display: block;
}
#first:hover #second {
display: block;
}
.hover3:hover #third {
display: block;
}
And here is the HTML:
<ul id="menu">
<!-- put class="selected" in the li tag for the selected page - to highlight which page you're on -->
<li class="hover">Index
<ul id="first">
<li class="hover2">Drop Down
<ul id="second">
<li> Link 1 </li>
<li>Link 2</li>
<li class="hover3">Link 3
<ul id="third">
<li>Link 4</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>

Hovering over menu and sub menu

I have a vertical menu some of which have sub menu displayed to the right side. When I hover first menu and sub menus of it, it displays the second set of sub menu belonging to third menu.
<body>
<ul class="menu">
<li>Population and Socio-Economic Atlas
<ul>
<li>Population
<ul>
<li>Population Density</li>
<li>Population By Sex</li>
<li>Population By Age</li>
</ul>
</li>
<li>Education</li>
<li>Agriculture
<ul>
<li>Agricultural Land</li>
<li>Agriculture Holding</li>
<li>Agriculture By Type</li>
</ul>
</li>
</ul>
</li>
<li>Level1 Menu2</li>
<li>CA Election 2070
<ul>
<li>Districts</li>
<li>Constituencies</li>
<li>Result
<ul>
<li>District Map</li>
<li>Constituencies Map</li>
<li>Election Result 2070</li>
</ul>
</li>
</ul>
</li>
<li>To Be Continued</li>
</ul>
</body>
my CSS below:
.menu,
.menu ul,
.menu li,
.menu a {
margin: 5px;
padding: 0;
}
.menu {
height: 250px;
width: 300px;
background: #FFCC99;
}
.menu li {
width:100%;
height:25px;
text-align:left;
display: block;
padding: 0 0px;
margin: 3px 0px;
line-height: 25px;
border-bottom: 1px solid #393942;
}
.menu li a {
font-family: Helvetica, Arial, sans-serif;
font-weight: bold;
text-decoration: none;
font-size: 14px;
}
.menu li:hover > a { color: #FF0000; }
/* second level menu */
.menu ul {
position: relative;
width: 200px;
top: -26px;
left: 295px;
opacity: 0;
background: #CCCCCC;
}
.menu li:hover > ul { opacity: 1; }
.menu ul li {
height: 25px;
overflow: hidden;
padding: 0 0px;
width: 200px;
padding: 0 0px;
margin: 6px 0px;
border: none;
border-bottom: 1px solid #353539;
}
.menu li:hover > ul li {
height: 25px;
overflow: visible;
padding: 0 0px;
}
.menu ul li a {
font-family: Helvetica, Arial;
font-style: italic;
font-weight: normal;
font-size: 12px;
}
.menu ul li:last-child a { border: none; }
/* third level menu */
.menu ul ul {
position: relative;
width: 200px;
top: -26px;
left: 195px;
opacity: 0;
background: #00FF00;
}
.menu ul ul:hover > ul { opacity: 1; }
.menu ul ul li {
height: 25px;
overflow: hidden;
padding: 0 0px;
width: 200px;
padding: 0 0px;
margin: 6px 0px;
border: none;
border-bottom: 1px solid #0000FF;
}
.menu ul ul ul:hover > ul {
height: 25px;
overflow: visible;
padding: 0 0px;
}
.menu ul ul li a {
font-family: Helvetica, Arial;
font-style: italic;
font-size: 12px;
}
.menu ul li:last-child a { border: none; }
How to solve this problem?
You need to use display: block/none; instead of overflow: hidden/visible to hide/unhide your sub-menus.
Here's the fiddle
overflow only affects visibility of the element. The element still exists in the DOM and responds to/ triggers all events associated with it. Hence, the issue.
That happens because you use opacity to hide sub menus. When opacity is 0 then sub menu is still on page, and receives mouse events. Submenus added to lower elements, are higher in visibility stack, and that reason why they steal hover. Instead of opacity use display.
Fiddle: http://jsfiddle.net/mL3eD/

CSS Drop Down menu being cut off by parent div height

In my set up I have my navigation bar set horizontally and contained within my header div like this:
<div id="header-section">
<div id="main-menu-wrapper">
<ul id="main-menu">
<li>Home</li>
<li>Services
<ul id="sub-men">
<li>Service 1</li>
<li>Service 2</li>
<li>Service 3</li>
</ul>
</li>
</ul>
<div class="clear"></div>
</div>
</div>
My problem is that the sub-menu is not showing because the height on "main-menu-wrapper" is set to auto. The sub-menu is showing when I set a height like 100px. When I set the position on the sub-menu to static instead of absolute, it expands the entire main-menu-wrapper. How can I get the sub-menu to show properly?
Here's the CSS portion for my whole header section:
#header-section {
position: relative;
width: 100%;
padding: 5px 0px;
background: #740600;
}
#main-menu-wrapper {
position: relative;
width: 74%;
min-width: 600px;
height: auto;
margin: 0% auto;
}
#main-menu {
list-style: none;
font-weight: bold;
line-height: 150%;
}
#main-menu li {
position: relative;
float: right;
margin: 0px 5px;
}
#main-menu a {
padding: 3px;
color: #ffffff;
background: #740600;
text-decoration: none;
border-radius: 5px;
}
#main-menu a:hover {
padding: 3px;
color: #740600;
background: #ffffff;
text-decoration: none;
}
#main-menu li ul {
position: absolute;
display: none;
}
#main-menu li ul li{
float: none;
}
#main-menu li:hover ul {
display: block;
}
#main-menu li ul a {
padding: 3px;
color: #ccc;
background: #740600;
text-decoration: none;
border-radius: 5px;
}
#main-menu li ul a:hover {
padding: 3px;
color: #740600;
background: #ccc;
text-decoration: none;
}
#banner-wrapper {
position: relative;
padding: 5px 0 5px;
}
#banner {
position: relative;
max-width: 75%;
min-width: 600px;
margin: 0% auto;
background: #ffffff;
}
#logo {
max-width: 600px;
height: auto;
}
I'm a little confused by what you're asking here, but I created a fiddle where your menu shows.
I deleted the styles for #main-menu-wrapper and I removed the background color on #header-section.
Hopefully this can be a decent starting point for you: http://jsfiddle.net/44vRN/
#header-section {
position: relative;
width: 100%;
padding: 5px 0px;
}
You could try to use absolute positioning on the submenu to remove it from the document flow.

Aligning DIV at bottom of another element causes menus to not be displayed in IE7

I've added markup on a webpage to align a element at bottom of another element. However when I do this the menus on my page aren't displayed in IE7. Here's the markup:
<div id="header">
<div class="panel">
<h1>Heading</h1>
<div class="nav">
<ul>
<li class="hdr"><a class="hdr" href="#">Submenu One</a>
<ul>
<li class="menuitem">Submenu one</li>
<li class="menuitem">Submenu 2</li>
</ul>
</li>
<li class="hdr"><a class="hdr" href="#">Submenu 2</a></li>
<li class="hdr"><a class="hdr" href="#">Submenu 3</a></li>
</ul>
</div>
</div>
</div>
The associated style sheet has the following:
#header
{
position: relative; /* Move to bottom */
height: 100px;
width: 100%;
}
.nav
{
position: absolute; /* Move to bottom */
bottom: 0; /* Move to bottom */
}
#header ul
{
padding-left: 0;
margin-left: 0;
margin: 12px 0px 0px 0px;
list-style: none;
position: relative;
left: -10px;
float: left;
}
#header ul li.hdr
{
display:-moz-inline-stack;
display: inline-block;
zoom: 1;
*display: inline; /* IE Hack */
margin-right: 15px !important;
font-size: 16px;
z-index: 1000;
}
#header ul li a.hdr
{
display: block;
color: white !important;
text-decoration: none;
padding: 9px 11px 11px 11px;
}
#header ul li a.hdr:hover
{
background: #505050;
border: solid 1px #606060;
padding: 8px 10px 10px 10px;
text-shadow: 2px 2px 2px #111;
}
#header ul ul
{
display: none;
border: 1px solid #a0a0a0;
background: #f5f5f5;
position: absolute;
top: 27px;
left: 0px;
zoom: 1;
padding: 10px;
line-height: 20px;
}
#header ul li:hover > ul
{
display: block;
}
#header ul ul li
{
display: block;
}
#header ul ul li a
{
font-size: 12px;
border: none;
color: #000;
background: #f5f5f5;
text-decoration: none;
padding: 8px;
}
The lines with the comment /* Move to bottom */ are responsible for moving the nav div to the bottom of the header. I've tried putting z-index's everywhere, as well as other attributes to ensure IE sees the elements with hasLayout equal to true, but to no avail. I'm pulling my hair out over this, any help much appreciated.
Your IE hack is wrong:
use
*+display: inline; /* IE Hack */
instead of
*display: inline; /* IE Hack */
(star) hack is for IE6 only.
[See here][1]
http://css-tricks.com/how-to-create-an-ie-only-stylesheet/