I am writing an article for my blog on the menu.
I am trying to create a Circular drop down menu. It works fine when there are two or more Submenus on all browsers. But if there is only one sub-menu to a parent menu it is displayed way below its intended position.
Here is working copy of the following code in jsfiddle: http://jsfiddle.net/avdhut/LDmM8/5/
I know the problem is in the #menucontainer ul li:hover > ul selector,
its with margin. But if I remove that margin-top menus having 2 or more sub-menus go up and are displayed way above their intended position.
Please help as I cannot find a suitable solution on this issue.
Following is the HTML code:
<div id="menucontainer">
<ul id="hmenu">
<li>Home
<ul>
<li>Test 1</li>
<li>Test 2</li>
<li>Test 3</li>
<li>Test 3</li>
</ul>
</li>
<li>About
<ul>
<li>Test 1</li>
<li>Test 2</li>
</ul>
</li>
<li>Contact Us
<ul>
<li>Test 1</li>
</ul>
</li>
<li>Info
<ul>
<li>Test 2</li>
<li>Test 3</li>
</ul>
</li>
</ul>
</div>
Following is the CSS code:
#menucontainer {
margin: 0px;
padding: 0px;
width: 100%;
height: 90px;
}
/* Following selectors will define the color and the border radius for the menu*/
#menucontainer ul, #menucontainer ul li ul {
list-style: none;
margin: 0px;
padding: 0px;
}
/* Following selector will define the style for individual menu or li tags. The following style gives them a look when the menus are not selected.
note that the position proerty is set to relative and dispaly is inline.
*/
#menucontainer ul li {
list-style: none;
margin: 10px;
padding: 0px;
background-color:#2CDF7B;
position: relative;
display: inline;
text-align: center;
/*Setting the height and width for the menu*/
width: 90px;
cursor:pointer;
height: 90px;
/*Setting the border-radius for the menu
various prefix are added so it is supported in older version of
browsers. For IE supported from IE-9+
IMPORTANT: to keep the border radius height and width same to get circle.
*/
-moz-border-radius: 90px;
-webkit-border-radius: 90px;
-khtml-border-radius: 90px;
border-radius: 90px;
/*Adding the box shadow to the menu*/
box-shadow: 0 0 10px black;
-moz-box-shadow: 0 0 10px black;
-webkit-box-shadow: 0 0 10px black;
float: left;
}
/* Style for the anchor tag defined in the menu.
*/
#menucontainer ul li a {
margin: 10px;
padding: 10px;
background-color:transparent;
display: block;
text-decoration: none;
text-align: center;
font-family:"Times New Roman", "Arabic";
font-size: 18px;
color: #E3E3C0;
}
/* The hover effect is defined using the below selectors
For the opacity is used to create a semi-transperant look as a hover effect
*/
#menucontainer ul li:hover {
list-style: none;
margin: 10px;
padding: 0px;
opacity: 0.7;
background-color:#2CDF7B;
position: relative;
display: block;
width: 90px;
text-align: center;
cursor:pointer;
height: 90px;
/*Setting the border-radius for the menu
various prefix are added so it is supported in older version of
browsers. For IE supported from IE-9+
IMPORTANT: to keep the border radius height and width same to get circle.
*/
-moz-border-radius: 90px;
-webkit-border-radius: 90px;
-khtml-border-radius: 90px;
border-radius: 90px;
-moz-box-shadow: inset 0 0 10px black;
box-shadow: inset 0 0 10px black;
-webkit-box-shadow: inset 0 0 10px black;
}
#menucontainer ul li:hover a {
margin: 10px;
padding: 10px;
background-color:transparent;
display: block;
text-decoration: none;
text-align: center;
font-family:"Times New Roman", "Arabic";
font-size: 18px;
color: white;
}
/*******************************************************/
/*******************************************************/
/*Sub menu*/
/*******************************************************/
#menucontainer ul li > ul {
display : none;
overflow: hidden;
position: relative;
border-bottom: solid 1px;
border: 0px;
height: 0px;
width: 100%;
}
#menucontainer ul li:hover > ul {
z-index: 10;
float: none;
left: -15px;
margin: 40px 10px 10px 10px;
height: auto;
padding: 0px 5px 0px 0px;
display:block;
width: 100px;
}
#menucontainer ul li:hover > ul li, #menucontainer ul li a:hover > ul li {
list-style: none;
margin: 10px;
padding: 0px;
background-color:#2CDF00;
position: relative;
display: inline;
text-align: center;
/*Setting the height and width for the menu*/
width: 90px;
cursor:pointer;
height: 90px;
/*Setting the border-radius for the menu
various prefix are added so it is supported in older version of
browsers. For IE supported from IE-9+
IMPORTANT: to keep the border radius height and width same to get circle.
*/
-moz-border-radius: 90px;
-webkit-border-radius: 90px;
-khtml-border-radius: 90px;
border-radius: 90px;
/*Adding the box shadow to the menu*/
box-shadow: 0 0 10px black;
-moz-box-shadow: 0 0 10px black;
-webkit-box-shadow: 0 0 10px black;
float: left;
}
/*******************************************************/
#menucontainer ul li:hover > ul li:after, #menucontainer ul li a:hover > ul li:after {
content:" ";
border: solid transparent;
margin: 0px;
padding: 0px;
position: absolute;
pointer-events: none;
border-color: rgba(44, 223, 123, 0);
border-top-color: #2CDF00;
top: 98%;
left: 50%;
margin-left: -10px;
border-width:10px;
}
#menucontainer ul li:hover > ul li:before, #menucontainer ul li a:hover > ul li:before {
content:" ";
border: solid transparent;
margin: 0px;
padding: 0px;
position: absolute;
pointer-events: none;
border-color: rgba(44, 223, 123, 0);
border-bottom-color: #2CDF00;
bottom: 98%;
left: 50%;
margin-left: -10px;
border-width:10px;
}
/*******************************************************/
Just some changes to css and you are done:-
#menucontainer ul li > ul {
display : none;
overflow: hidden;
position: absolute; //implement absolute
border-bottom: solid 1px;
border: 0px;
height: 0px;
width: 100%;
top:90px; //add top
}
#menucontainer ul li:hover > ul {
z-index: 10;
float: none;
left: -15px;
margin: 5px; //change it to this
height: auto;
padding: 0px 5px 0px 0px;
display:block;
width: 100px;
}
Working fiddle:- http://jsfiddle.net/LDmM8/7/
It has nothing to do with the fact that you only have 1 item in that dropdown menu. You can try and add more and see it's still happening. The real reason is that the "Contact Us" button is taking two rows instead of one like the other buttons.
The problem is solved by setting a fixed height for the anchors.
jsFiddle Demo
#menucontainer ul li a {
height:33px;
}
Related
I'm using a fairly typical nested UL setup to create a dropdown menu, however I can't get the anchorlinks inside the li to expand to their height.
The HTML
<div id="navbar-container">
<ul id="navbar">
<li>Home</li>
<li>Lessons</li>
<ul>
<li>sub item1sdfsdfsdfsdfsdf</li>
<li>sub item2</li>
<li>sub item3</li>
</ul>
</li>
<li>Custom Fitting</li>
</ul>
</div>
In the CSS I'm using display:block on the anchor tags which does make them expand to the width of the li but not the height. I have tried using padding but it does not work correctly across all browsers. #navbar is using display: table and the children lis are using display: table-cell. This is so the navbar can expand and contract to fit the screen size. I suspect display: table-cell may have something to do with the anchors not expand vertically.
Here is a JSFiddle so you can see what I'm talking about.
The CSS
#navbar-container {
min-width: 768px;
height: 32px;
position: relative;
background-color: #bb4212;
}
#navbar {
list-style-type: none;
display: table;
width: 100%;
font : 14px"Arial", sans-serif;
height: 100%;
}
#navbar li {
text-transform:uppercase;
display: table-cell;
text-align: center;
}
#navbar li a {
color: #f2f2f2;
display: block;
border-left: 1px solid #c17455;
}
#navbar > li:first-child a {
border: 0;
}
#navbar li ul {
list-style-type: none;
background-color: #f2f2f2;
position: absolute;
right: -9999px;
top: 32px;
margin-left: 1px;
-moz-box-shadow: 0px 6px 4px 0px #898989;
-webkit-box-shadow: 0px 6px 4px 0px #898989;
box-shadow: 0px 6px 4px 0px #898989;
}
#navbar li ul li:hover {
background-color: #bb4212;
}
#navbar li ul a:hover {
color: #f2f2f2;
}
#navbar li:hover {
background-color: #f2f2f2;
}
#navbar li:hover a {
color: #000;
}
#navbar li:hover ul {
right: auto;
}
#navbar li ul li {
display: block;
text-align: left;
}
#navbar li ul li a {
border: 0;
white-space:nowrap;
margin: 0 5px;
text-decoration: none;
display: block;
}
My favorite technique for filling up a parent container 100% width and height is to use absolute positioning:
parent {
position: relative; /* unless it's already positioned */
}
child {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
Here it is applied to your JSFiddle: http://jsfiddle.net/Kgz5p/
I have a submenu, the problem is my submenu must have more distance and should be visible on hover, so if i'm out of li the submenu goes closed.
Demonstration code on jsfiddle.
HTML code is:
<ul class="menu">
<li> submenu
<ul>
<li>sub item 1
</li>
<li>sub item 2
</li>
<li>sub item 3
</li>
<li>sub item 4
</li>
<li>sub item 5
</li>
<li>sub item 6
</li>
<li>sub item 7
</li>
</ul>
</li>
</ul>
CSS is:
a {
text-decoration: none;
}
ul.menu {
}
ul.menu li {
list-style-type: none;
position: relative;
float: left;
display: inline;
margin: 0px 0px 1px 1px;
padding: 0px 0px 0px 0px;
height: 33px;
line-height: 33px;
}
ul.menu li a {
display: block;
color: #5b615b;
padding: 0px 7px 0px 7px;
background-color: #e1e1e1;
text-transform: uppercase;
}
ul.menu li a:hover {
color: #000000;
background-color: #e1e1e1;
}
ul.menu li ul {
display: none;
font-size: 10px;
width: 100%;
position: absolute;
top: 37px;
left: 0px;
margin: 0px;
padding: 0px;
}
ul.menu li:hover > ul {
display: block;
}
ul.menu li:hover > a {
color: #000000;
background-color: #e1e1e1;
}
ul.menu li:hover > ul {
display: block;
}
ul.menu li ul li {
position: relative;
float: none;
display: block;
height: 22px;
line-height: 22px;
border-bottom: 1px solid #ffffff;
margin: 0px 0px 0px 0px;
padding: 0px 0px 0px 0px;
}
ul.menu li.active ul li a {
color: #5b615b;
background-color: #e1e1e1;
}
Change the top on ul.menu li ul to 33px instead of 37, and add a padding-top of 1px. Updated Fiddle here.
The padding ensures that the hover effect remains active between the elements (padding is part of the element so also has hover).
ul.menu li ul {
display: none;
font-size: 10px;
width: 100%;
position: absolute;
top: 34px;
left: 0px;
margin: 0px;
padding: 0px;
}
change top:37; to top:34; If you want to maintain the whitespace, you can add a 3px white border to the bottom of the top li.
jsfiddle showing this method http://jsfiddle.net/N23AM/4/
visualy identical, just allows the hover to be maintained by the border rather than losing focusing to the background.
The proper solutions would be to add padding-top:20px;(your desired value would be there of course) put in ul li:hover ul selector.
Another, maybe strange solution, that I could think of is to add in your ul submenu a new div that looks like this:
<div style="height:20px; background:transparent; width:100%;"></div>
I am building a pure HTML/CSS menu with a horizontal submenu.
The problem is that when you are hovering on the submenu the background color from the hover of the main-menu item is gone.
Is it possible to keep that background color of the main menu item when you are hovering on the submenu?
Here is what I got so far:
http://jsfiddle.net/YKEkB/1/
As you can see when you hover over "this is the one" you get the submenu. But when you are in the submenu the background color of "this is the one" changes back.
Is there any solution with pure HTML/CSS?
This is all the code:
<div class="wrapper">
<div class="menu-holder">
<ul class="menu">
<li>item 1
</li>
<li><a class="test" href="#">This is the one</a>
<ul class="submenu">
<li>Submenu item 1
</li>
<li>Submenu item 2
</li>
</ul>
</li>
<li><a href="#" >Menu item 3</a>
</li>
<li><a href="#" >Last item</a>
</li>
</ul>
</div>
</div>
css:
.wrapper {
width:900px;
height:200px;
background:grey;
}
.menu-holder {
padding: 50px 0 0 0;
}
.menu-holder ul {
margin: 0 0 0px 25px;
padding: 0;
list-style-type: none;
}
.menu-holder ul li {
position: relative;
float: left;
padding: 0px 10px 0 10px;
margin: 0px 0 0 0px;
border-left: 1px dotted white;
line-height: 0px;
}
.menu-holder ul li a {
font-family: arial, sans-serif;
font-size: 12px;
font-style: bold;
display: block;
color: white;
text-decoration: none;
padding: 15px 10px 15px 10px;
-webkit-border-radius: 5px 5px 0px 0px;
border-radius: 5px 5px 0px 0px;
}
.menu-holder ul li a:hover + ul {
display: block;
}
.menu-holder ul li a:hover {
display: block;
background-color: #025179;
}
.menu-holder ul li .submenu {
display: none;
position: absolute;
top: 100%;
left: 0px;
right: auto;
margin: -5px 0 0px 0px;
padding: 5px 10px 5px 10px;
white-space: nowrap;
}
.menu-holder ul li .submenu li {
position: static;
float: left;
display: inline;
padding: 15px 10px 15px 10px;
background-color: #025179;
}
.menu-holder ul li .submenu li a {
display: inline;
margin: 0 0px 0 0px;
padding: 0px 10px 0px 10px;
-webkit-border-radius: 0;
border-radius: 0;
}
.menu-holder ul li .submenu li:first-of-type {
-webkit-border-radius: 0px 0px 0px 5px;
border-radius: 0px 0px 0px 5px;
}
.menu-holder ul li .submenu li:last-of-type {
-webkit-border-radius: 0px 5px 5px 0px;
border-radius: 0px 5px 5px 0px;
}
.menu-holder ul li .submenu:hover {
display: block;
}
.menu-holder ul li .submenu:hover .test {
display: block;
background-color: #025179;
}
.menu-wrapper .menu-holder ul li:first-of-type {
border-left: none;
}
Easy: http://jsfiddle.net/YKEkB/2/
You have to change it to this (delete the a):
.menu-holder ul li:hover{
display: block;
background-color: #025179;
}
Now you will have to adjust it so it's centered vertically. Note that you should keep 0px between the menu item and the submenu.
Many more things.
You can use the pseudo element :last-child to add the dotted line in the last item (see fiddle 3):
.menu-holder ul li:last-child {
border-right: 1px dotted white;
}
If I'm right, you only wanted to round the last element in the drop down menu and the second corner of the first one:
.menu-holder ul li ul li:first-of-type {
-webkit-border-radius: 0px 5px 0px 0px;
border-radius: 0px 5px 0px 0px;
}
.menu-holder ul li .submenu li:last-of-type {
-webkit-border-radius: 0px 0px 5px 5px;
border-radius: 0px 0px 5px 5px;
}
You can (and should) access the submenues like this:
.menu-holder ul li ul {
Instead of this:
.menu-holder ul li .submenu {
Cleaned and touched a couple of things more, and here is your code working. Tell me if you find any trouble. There's still room for improvement (readability and DRY to name some), but here it goes:
Final Demo
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.
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/