I'm making a CSS dropdown menu. So, when the mouse hovers one menu item, it displays its subitens. Well, all is working fine except the position of submenus. What I got is this:
|Menu 1| |Menu 2|
|Submenu 1|
|Submenu 2|
|Submenu 3|
But I have to change this layout into this, when the mouse hovers a item:
|Menu 1| |Menu 2|
|Submenu 1|
|Submenu 2|
|Submenu 3|
I don't know if it's even possible to make that change.
CSS
.menu{
border:none;
border:0px;
margin:0px;
padding:0px;
font: 67.5% "Lucida Sans Unicode", "Bitstream Vera Sans", "Trebuchet Unicode MS", "Lucida Grande", Verdana, Helvetica, sans-serif;
font-size:14px;
font-weight:bold;
float: right;
width: auto;
}
.menu ul{
background:#EBF0FD;
line-height: 2.0em;
list-style:none;
margin:0;
padding:0px;
}
.menu li{
float:left;
padding:0px;
}
.menu li a{
background:#EBF0FD url("images/seperator.gif") bottom right no-repeat;
color:#336699;
display:block;
font-weight:normal;
line-height:2.0em;
margin:0px;
padding:0px 25px;
text-align:center;
text-decoration:none;
}
.menu li a:hover, .menu ul li:hover a{
background: #5E78B5 bottom center no-repeat;
color:white;
text-decoration:none;
}
.menu li ul{
background:#EBF0FD;
display:block;
float: left;
height:auto;
padding:0px;
margin:0px;
border-style:solid;
border-color:#C9D8FF;
position:absolute;
width:auto;
z-index:200;
}
.menu li:hover ul{
display:block;
}
.menu li li {
background: bottom left no-repeat;
display:block;
float:none;
margin:0px;
padding:0px;
width:225px;
color: red;
}
.menu li:hover li a{
background:none;
color: #336699;
}
.menu li ul a{
display:block;
line-height:35px;
font-size:12px;
font-style:normal;
margin:0px;
padding:0px 10px 0px 15px;
text-align:left;
}
.menu li ul a:hover, .menu li ul li:hover a{
background:#5E78B5 left no-repeat;
border:0px;
color:#ffffff;
text-decoration:none;
}
.menu p{
clear:left;
}
HTML
<div class="menu">
<ul>
<li>
Menu 1
<ul>
<li>Submenu 1</li>
<li>Submenu 2</li>
<li>Submenu 3</li>
</ul>
</li>
</ul>
</div>
SOLUTION:
Andres Ilich's code have solved my problem (Thanks!). Now, all submenus upper right edge is correctly aligned with the bottom right edge of the top menus. However, there was a problem when several menus itens (top level) were in the layout so that they appeared in vertical, not horizontal. I was able to correct it. So, I'm posting the final working CSS if anyone is also facing this problem. Thanks for helping!
.menu {
font-size:14px;
font-weight:bold;
float: right;
width: auto;
}
.menu ul {
background:#EBF0FD;
line-height: 2.0em;
list-style:none;
margin:0;
padding:0px;
font-family: Verdana, Geneva, sans-serif;
}
.menu li {
float:left;
padding:0;
margin:0;
position:relative;
}
.menu li a {
background:#EBF0FD;
color:#336699;
display:block;
font-weight:bold;
line-height:2.0em;
margin:0px;
padding:0px 25px;
text-align:center;
text-decoration:none;
}
.menu li a:hover, .menu ul li:hover a {
background: #5E78B5 bottom center no-repeat;
color:white;
text-decoration:none;
}
.menu li ul {
background:#EBF0FD;
display:none;
float: left;
height:auto;
padding:0px;
margin:0px;
border-style:solid;
border-color:#C9D8FF;
position: absolute;
right: 0;
width:auto;
z-index:200;
}
.menu li:hover ul {
display:block;
}
.menu li li {
background: bottom left no-repeat;
display:block;
float:none;
margin:0px;
padding:0px;
width:200px;
}
.menu li:hover li a {
background:none;
color: #336699;
}
.menu li ul a {
display:block;
line-height:35px;
font-size:12px;
font-style:normal;
margin:0px;
padding:0px 21px;
text-align:right;
}
.menu li ul a:hover, .menu li ul li:hover a {
background:#5E78B5 left no-repeat;
color:#ffffff;
text-decoration:none;
}
You can absolutely position your submenu and just align it to the right most part of your main menu, like so:
First declare your li items as relative, to position the submenu properly:
.menu li {
float: left;
padding: 0;
position: relative;
}
Then declare your submenu as position:absolute along with the right:0 property, to position your submenu absolutely to the right edge of your main menu:
.menu li ul {
position: absolute;
right: 0;
}
And then just position your submenu text to the right to complete the effect:
.menu li ul a {
text-align:right;
}
Edit: Demo
Try this
.menu li ul { display:block; margin-left: -130px; }
jsfiddle example:
http://jsfiddle.net/BBJv5/
But you have to know what width the submenu items gonna have, before you do it this way.
see the fiddle for code and demo
Fiddle: http://jsfiddle.net/dMCKD/1/
Demo: http://jsfiddle.net/dMCKD/1/embedded/result/
Screen Shot:
Related
I have created a Menu list.
It is working perfectly on Chrome and Firefox but misaligned in IE8
Chrome Snapshot :-
http://postimg.org/image/g6uf9zikr/
IE8 Snapshot :-
http://postimg.org/image/u6o3pzasz/
Here's my CSS ------
div.menu {
background-color:#383838;
height:65px;
margin-bottom:20px;
}
div.menu ul {
vertical-align:middle;
list-style:none;
margin:0;
padding:0;
}
div.menu ul li {
float:left;
display:block;
}
div.menu ul li a {
color:#FFFFFF;
display:block;
float:left;
font-family:"Trebuchet MS", Verdana, Arial;
font-weight:bold;
padding:24px 20px 22px;
text-transform:uppercase;
text-decoration:none;
}
div.menu ul li a:hover {
text-decoration:underline;
background-color:#2B2B2B;
}
div.menu ul li:hover ul {
display:block;
}
div.menu ul li ul {
vertical-align:middle;
text-align:center;
float: none;
list-style: none;
display: none;
position:absolute;
z-index:999;
margin-top:60px;
background-color:#383838;
opacity:0.8;
}
div.menu ul li ul li{
float: none;
vertical-align:middle;
background-color:#383838;
padding:15px;
}
div.menu ul li ul li a {
color:#FFFFFF;
font-family:"Trebuchet MS", Verdana, Arial;
font-weight:bold;
text-transform:uppercase;
padding:0px;
}
div.menu ul li ul li a:hover {
text-decoration:underline;
}
div.menu ul li ul li:hover{
background-color:#2B2B2B;
opacity:1.0;
}
<div class="menu">
<ul>
<li> HOME </li>
<li>CONTROLLER
<ul>
<li> CONTROLLER-1</li>
<li> CONTROLLER-2</li>
</ul></li>
<li>IOS BPL ZIO
<ul>
<li> IOS BPL ZIO-1</li>
<li> IOS BPL ZIO-2</li>
</ul></li>
</ul>
</div>
Please help me to resolve the issue.
Remove float: left from the links in the menu. This will cause those links to fill the entire row, and the ul of the submenu will not be shown next to it.
To counter that gap that causes, remove the margin-top from the submenu ul.
That should do the trick, although I don't have IE8 at hand to test it.
div.menu {
background-color:#383838;
height:65px;
margin-bottom:20px;
}
div.menu ul {
vertical-align:middle;
list-style:none;
margin:0;
padding:0;
}
div.menu ul li {
float:left;
display:block;
}
div.menu ul li a {
color:#FFFFFF;
display:block;
font-family:"Trebuchet MS", Verdana, Arial;
font-weight:bold;
padding:24px 20px 22px;
text-transform:uppercase;
text-decoration:none;
}
div.menu ul li a:hover {
text-decoration:underline;
background-color:#2B2B2B;
}
div.menu ul li:hover ul {
display:block;
}
div.menu ul li ul {
vertical-align:middle;
text-align:center;
float: none;
list-style: none;
display: none;
position:absolute;
z-index:999;
background-color:#383838;
opacity:0.8;
}
div.menu ul li ul li{
float: none;
vertical-align:middle;
background-color:#383838;
padding:15px;
}
div.menu ul li ul li a {
color:#FFFFFF;
font-family:"Trebuchet MS", Verdana, Arial;
font-weight:bold;
text-transform:uppercase;
padding:0px;
}
div.menu ul li ul li a:hover {
text-decoration:underline;
}
div.menu ul li ul li:hover{
background-color:#2B2B2B;
opacity:1.0;
}
<div class="menu">
<ul>
<li> HOME </li>
<li>CONTROLLER
<ul>
<li> CONTROLLER-1</li>
<li> CONTROLLER-2</li>
</ul></li>
<li>IOS BPL ZIO
<ul>
<li> IOS BPL ZIO-1</li>
<li> IOS BPL ZIO-2</li>
</ul></li>
</ul>
</div>
I´m trying to figure out why the background image on my a:selected class is being cut both bottom and top. I´ve tried assigning height:!important; and min-height; to .main_menu ul li and .main_menu ul li a.selected, but seems like something else is limiting the background image size (17x21px)
I wanted to provide an image, but since I´m new they won´t let me do so :(
The image is a circle and it´s being sliced bottom and top, just as it was to fit into a smaller container
Here´s the code, thanks for any suggestion!
.menu_container{
position: absolute;
float: left;
width: 270px;
margin-top: 220px;
}
.main_menu ul {
padding: 0px;
margin:0px;
list-style-type: none;
}
.main_menu ul li {
font-family:Arial, Helvetica, sans-serif;
font-size:11px;
letter-spacing:4px;
text-align:right;
line-height:35px;
list-style-type:none;
}
.main_menu ul li a {
padding-right: 25px;
text-decoration:none;
color:#999;
}
.main_menu ul li a.selected {
color: #bc4c9b;
font-weight:bold;
background: url(images/circle.gif) right center no-repeat;
height: 35px!important;
}
.main_menu ul li:hover {
text-decoration:none;
color:#999;
font-weight:bold;
background:url(images/circle_grey.gif) right center no-repeat;
}
try and rewrite the following rule:
.main_menu ul li a {
padding-right: 25px;
text-decoration:none;
color:#999;
display:block;
}
Hey i think you can give to
anchor tag display: inline-block; or give to line-height
as like this
.main_menu ul li a{
display:inline-block;
line-height:35px;
}
Your background is attached to a anchor tag (.main_menu ul li a) and is therefor sized after font-size. To see the whole background file, you need to make anchor element to block or you can use padding.
Use:
display: inline-block;
or
display: block;
or use padding for .main_menu ul li a
You just need to define display:inline-block; in your li a see your updated css :-
.menu_container{
position: absolute;
float: left;
width: 270px;
margin-top: 220px;
}
.main_menu ul {
padding: 0px;
margin:0px;
list-style-type: none;
}
.main_menu ul li {
font-family:Arial, Helvetica, sans-serif;
font-size:11px;
letter-spacing:4px;
text-align:right;
line-height:35px;
list-style-type:none;
}
.main_menu ul li a {
padding-right: 25px;
text-decoration:none;
font-weight:bold;
color:#999;
display:inline-block;
}
.main_menu ul li a.selected {
color: #bc4c9b;
font-weight:bold;
background: url(http://i.imgur.com/TXNfW.gif) right center no-repeat;
}
.main_menu ul li a:hover {
text-decoration:none;
color: #595959;
font-weight:bold;
background:url(http://i.imgur.com/5Ic6o.gif) right center no-repeat;
}
see the demo:- http://jsbin.com/agavid/13/edit
I´m building a vertical menu in CSS, but I can´t get the a:hover to work, it´s just doing nothing. I´ve been searching and reviewing the code (I´m new to CSS), but can´t figure it out...if anyone has any suggestions, much appreciated :)
Here´s CSS:
.menu_container{
position: absolute;
float: left;
width: 270px;
margin-top: 220px;
}
.main_menu ul {
padding: 0px;
margin:0px;
list-style-type: none;
}
.main_menu ul li {
padding-right: 25px;
font-family:Arial, Helvetica, sans-serif;
font-size:11px;
letter-spacing:4px;
text-align:right;
line-height:35px;
list-style-type:none;
}
.main_menu ul li a {
text-decoration:none;
color:#999;
}
.main_menu ul li a:hover {
text-decoration:none;
color:#999;
font-weight:bold;
background:url(images/circle_grey.gif) right center no-repeat;
padding-right: 25px;
float:right;
}
.main_menu ul li a.selected {
background: url(images/circle.gif) right center no-repeat;
padding-right: 25px;
float:right;
color: #bc4c9b;
font-weight:bold;
}
HTML:
<div class="menu_container">
<div class="main_menu">
<ul>
<li>HOME</li>
<li><a href="quienes_somos.html" class="selected" >QUIÉNES SOMOS</a></li>
<li>Consultoría</li>
<li>Capacitación</li>
<li>Académico / ARTÍCULOS</li>
<li>Alianzas</li>
<li>Proyectos</li>
<li>Contacto</li>
</ul>
</div>
</div>
The menu was being floated to the left on hover and also on the .selected class and that was what was causing the problem. Here are the fixed classes with the float properties removed:
.main_menu ul li a:hover {
text-decoration:none;
color:#999;
font-weight:bold;
background:url(images/circle_grey.gif) right center no-repeat;
padding-right: 25px;
}
.main_menu ul li a.selected {
background: url(images/circle.gif) right center no-repeat;
padding-right: 25px;
color: #bc4c9b;
font-weight:bold;
}
I want to make a small website for myself and so I ask this question as a beginner. I cannot succeed to have the submenu contain another background when hovering the main menu.
HTML:
<div id="nav">
<ul class="sub1">
<li><h2>Home</h2></li>
<li><h2>About</h2><!---id="onlink"--->
<ul class="sub2">
<li>Us</li>
<li>Our product</li>
</ul>
</li>
</ul>
</div>
CSS:
#nav{
width:100%;
height: 60px;
padding-top: 20px;
padding-bottom: 10px;
}
#nav ul{
margin:0;
padding:0;
width: 60%;
margin: auto;
}
#nav ul li{
margin:0;
padding:0;
list-style: none;
float:left;
position:relative;
background:transparent url(EN/Button_menu_unselected.png);
}
#nav ul li ul li{
background:aqua; /*Test that doesn't work*/
}
#nav ul li a{
text-align:center;
text-decoration:none;
height:20px;
width:100px;
padding:10px;
display:block;
color:#003300;
}
#nav ul ul{
position:absolute;
visibility:hidden;
}
#nav ul li:hover ul{
visibility:visible;
}
#nav ul li:hover a{
background-image: url(EN/Button_menu_rollover.png);
}
The drop down items get the same format as the button called "Button_menu_rollover", which gives me a very bad result.
The code:
#nav ul li:hover a{
background-image: url(EN/Button_menu_rollover.png);
}
is applied to all elements and sub-elements.
This fixes it, but this code does not work on IE6:
#nav ul li:hover > a{
background: #fff;
}
Or you may use Javascript for HOVER effect to get exact results.
I am currently using position:absolute; for the second level menu, because when I use position: relative; the first level menu is broken (after the hovered item is a linebreak?)
But I want to achieve that the second level menu that is showed, is positioned relative to the hovered item in the first level menu (directly under it and also horizontal)
My menu is like this currently:
Home / Test / News
Test_Sub1 / Test_Sub2 / Test_Sub3
But should be
Home / Test / News
Test_Sub1 / Test_Sub2 / Test_Sub3
Here is my CSS Code
#navigation ul.menu {
list-style-type:none;
list-style:none;
/*overflow:hidden;*/
width:980px;
}
#navigation li {
list-style:none;
list-style-type:none;
padding:0;
float:left;
display:inline;
height:32px;
}
#navigation ul.menu {
width:100%;
height:32px;
border-bottom: 1px solid #cecbcd;
}
/* style list as navigation using float:left */
/* set distance from left corner to the first li item */
#navigation ul.menu li:first-child {
margin-left:150px;
}
#navigation ul.menu li ul li:first-child {
margin-left:0px;
}
#navigation ul.menu li ul {display:none; position:absolute; top:32px; left: 10em; float:left; border:none;}
#navigation ul.menu li:hover ul {display:inline;}
#navigation ul.menu li:hover ul li {height:45; padding-top:8px; float:left; position: relative;}
#navigation ul.menu li:hover ul li a {float:left;}
#navigation ul.menu li ul li {display:inline; background-image:none; float:left; position:relative; left: 5em; }
#navigation ul.menu li a {
/*background-color:#ffffff; */
font: 12pt/24pt 'SansumiRegular', Arial, sans-serif;
text-decoration:none;
color: #666666;
height: 32px;
background-image:none;
display:block;
padding-left:5px;
padding-right:5px;
float: left;
}
#navigation ul.menu li ul li a {
/*background-color:#ffffff; */
font: 12pt/24pt 'SansumiRegular', Arial, sans-serif;
text-decoration:none;
color: #666666;
height: 32px;
background-image:none;
display:block;
padding-left:5px;
padding-right:5px;
float: left;
}
#navigation ul.menu li ul li a:hover, #navigation ul.menu li ul li.active-trail a.active-trail {
/*background-color:#ffffff; */
font: 12pt/24pt 'SansumiRegular', Arial, sans-serif;
text-decoration:underline;
color: #666666;
height: 32px;
background-image:none;
display:block;
padding-left:5px;
padding-right:5px;
float: left;
}
#navigation ul.menu li a:hover {
font: 12pt/24pt 'SansumiRegular', Arial, sans-serif;
text-decoration:none;
color: #ffffff;
height: 32px;
background-image:url('../images/navi_active.png');
background-position:center;
background-repeat:no-repeat;
}
#navigation ul.menu li a.active, #navigation ul.menu li.active-trail a.active-trail {
font: 12pt/24pt 'SansumiRegular', Arial, sans-serif;
text-decoration:none;
color: #ffffff;
height: 32px;
background-image:url('../images/navi_active.png');
background-position:center;
background-repeat:no-repeat;
}
div.region-header ul.menu {
display:none;
}
Try This : http://jsfiddle.net/rathoreahsan/3NRu4/
Edit:
I have figured out your code:
you have defined:
#navigation ul.menu li:hover ul {display:inline;}
Chenge it to:
#navigation ul.menu li:hover ul {display:block;}
Also in this line,
#navigation ul.menu li ul li {display:inline; background-image:none; float:left; position:relative; left: 5em; }
Remove left: 5em to get the result like this: http://jsfiddle.net/rathoreahsan/GFURU/
Edited:
More over in this line:
#navigation ul.menu li ul {display:none; position:absolute; top:32px; float:left; border:none; background:red; left: 10em;}
Remove left: 10em to get ul a relative position
See the Demo: http://jsfiddle.net/rathoreahsan/rTsAE/
Hope it will fix it.