I'm trying to create a hover effect in my drop down menu but I can't figure out how to do it.
What I want to do is this:
instead of this:
My Code - You can also see it here, (updated version)
HTML:
<nav>
<ul>
<li>One
<ul>
<li>1.1</li>
<li>1.2
</ul>
</li>
<li>Two
<ul>
<li>2.1</li>
<li>2.2</li>
<li>2.3</li>
</ul>
</li>
<li>Three
<ul>
<li>3.1</li>
<li>3.2</li>
</ul>
</li>
<li>Four</li>
</ul>
</nav>
CSS:
nav {
float: left;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
list-style: none;
position: relative;
display: inline-table;
}
nav ul:after {
content:"";
clear: both;
display: block;
}
nav ul li {
float: left;
}
nav ul li:hover {
}
nav ul li:hover {
background-color: #000;
}
nav ul li a:hover {
color: #fff;
}
nav ul li a {
display: block;
padding: 25px 15px;
color: #6F0;
text-decoration: none;
}
nav ul ul {
border-radius: 0px;
padding: 0;
position: absolute;
}
nav ul ul li {
float: none;
position: relative;
}
nav ul ul li a {
padding: 15px 40px;
color: #000;
}
Any help will be appreciated.
Working fiddle:
Change the styles for the below two:
nav ul li:hover {
border-bottom: 1px solid #000;
color: #000;
}
nav ul li a:hover {
color: #000;
}
And add:
nav ul li:hover li:hover{
background: #000;
}
In order to style the sub-menus.
The first (li:hover) you want to set a bottom border - you can change the width of this border from 1px to something more thick, say, 3px
Related
I have the following HTML snippet for my menu bar:
#menubar {
display: block;
background: #FFFFFF;
}
#menubar ul li a.menubutton {
display: none;
/* dont show burger symbol (mobile menu symbol) */
}
#menubar ul {
display: block;
width: 2em;
/* restrict burger symbol */
padding: 0.9em;
}
#menubar ul li {
display: inline;
}
#menubar ul li ul li {
display: none;
}
/*Header*/
header {
display: block;
background: #2F2C2C;
text-align: center;
}
/* Navigation */
nav {
display: block;
height: 2.5em;
background: #FFFFFF;
text-align: center;
}
nav ul {
display: block;
}
nav ul li {
display: inline;
margin: 0em 0.188em 0em 0.188em;
}
nav ul li a {
color: #454040;
font-size: 1.125em;
line-height: 2.5em;
padding: 0.563em 0.938em 0.375em 0.983em;
transition: background 0.2s;
/* nice transition effect */
-webkit-transition: background 0.2s;
}
nav ul li a:hover {
background: #DBD9D8;
border-bottom: 0.188em solid #FF0000;
}
nav ul li a.active {
border-bottom: 0.188em solid #E7590B;
}
nav ul li ul {
display: none;
}
nav ul li:hover ul.submenu {
text-align: center;
position: fixed;
display: block;
margin-left: 40%;
}
nav ul li ul li:hover {
background: #DBD9D8;
border-bottom: 0.188em solid #FF0000;
}
<nav class="nav">
<ul>
<li>Start</li>
<li>Termine</li>
<li>Organisation
<ul class="submenu">
<li>Page1</li>
<li>Page2</li>
<li>Page3</li>
</ul>
</li>
<li>About
<ul class="submenu">
<li>Page1</li>
<li>Page2</li>
<li>Page3</li>
</ul>
</li>
<li>Contact</li>
</ul>
</nav>
The ul #submenu are displayed on hover.
I managed to center the submenus with the whole page using left: 40% in the nav ul li:hover ul.submenu rule. However, I want to position the submenus centered to their parent components, so taking organisation for example I'd like to have the page 2 submenu centered with the parents organisation field and for about the same.
How can I achieve to center relatively to the parent component?
Add position: relative to the menu entries which have a submenu (i.e. to the nav ul li selector) and change position: fixed to position: absolute for the submenu, i.e. the nav ul li:hover ul.submenu selector.
This makes the submenu position "relative" to their parent (the main menu entry), so you should then adjust the left and top settings in the submenu CSS rule accordingly.
#menubar {
display: block;
background: #FFFFFF;
}
#menubar ul li a.menubutton {
display: none;
/* dont show burger symbol (mobile menu symbol) */
}
#menubar ul {
display: block;
width: 2em;
/* restrict burger symbol */
padding: 0.9em;
}
#menubar ul li {
display: inline;
}
#menubar ul li ul li {
display: none;
}
/*Header*/
header {
display: block;
background: #2F2C2C;
text-align: center;
}
/* Navigation */
nav {
display: block;
height: 2.5em;
background: #FFFFFF;
text-align: center;
}
nav ul {
display: block;
}
nav ul li {
display: inline;
margin: 0em 0.188em 0em 0.188em;
position: relative;
}
nav ul li a {
color: #454040;
font-size: 1.125em;
line-height: 2.5em;
padding: 0.563em 0.938em 0.375em 0.983em;
transition: background 0.2s;
/* nice transition effect */
-webkit-transition: background 0.2s;
}
nav ul li a:hover {
background: #DBD9D8;
border-bottom: 0.188em solid #FF0000;
}
nav ul li a.active {
border-bottom: 0.188em solid #E7590B;
}
nav ul li ul {
display: none;
}
nav ul li:hover ul.submenu {
text-align: center;
position: absolute;
display: block;
left: -50%;
}
nav ul li ul li:hover {
background: #DBD9D8;
border-bottom: 0.188em solid #FF0000;
}
<nav class="nav">
<ul>
<li>Start</li>
<li>Termine</li>
<li>Organisation
<ul class="submenu">
<li>Page1</li>
<li>Page2</li>
<li>Page3</li>
</ul>
</li>
<li>About
<ul class="submenu">
<li>Page1</li>
<li>Page2</li>
<li>Page3</li>
</ul>
</li>
<li>Contact</li>
</ul>
</nav>
Hi I just read your question. So you will have to implement this with your own class names. But this is how to centre your child elements inside their respective parents.
.child-class{
width:max-content;
margin:0px auto;
}
In order for this to work you have to have a width. This will auto centre align your div or whatever tag you are using.
I recommend this method over position relative, because what's really convenient about using this method is that it will centre align your content evenly even if the different li tags are different widths. Which is convenient for mobile optimisation. :)
I'm stuck with a drop down menu. The problem is that the "parent" link is jumping.
HTML:
<ul id="nav">
<li><span>Page 1</span>
<ul>
<li><a>Extralong Page 1.1</a></li>
<li><a>Page 1.2</a></li>
<li><a>Page 1.3</a></li>
</ul>
</li>
<li>Page 2</li>
<li><span>Page 3</span>
<ul>
<li><a>Page 3.1</a></li>
<li><a>Long description for page 3.2</a></li>
<li><a>Page 3.3</a></li>
<li><a>Page 3.4</a></li>
</ul>
</li>
</ul>
CSS:
* {
margin: 0;
padding: 0;
}
#nav {
float: right;
}
#nav ul {
float: left;
}
#nav li {
float: left;
padding-top: 2px;
list-style: none;
background: #3451ff;
}
#nav li a,
#nav li span {
display: block;
text-decoration: none;
font-size: 12pt;
font-weight: bold;
padding: 13px 10px 9px 10px;
}
#nav li a:link, #nav li a:active, #nav li a:visited,
#nav li span {
color: #FFF;
}
#nav li a:hover, #nav li a.active,
#nav li span:hover {
color: #000;
}
#nav li li {
display: none;
}
#nav li:hover li {
display: block;
float: none;
background: #555;
}
#nav li li a {
font-size: 10pt;
margin: 1px;
width: 100%;
background: #3c6f3a;
padding: 5px 0;
}
Demo
How can I make this parent static such that it's width isn't changed on hover? I don't want to use js.
Set a width for the parent:
#nav li {
float: left;
padding-top: 2px;
list-style: none;
width: 100px; /* Add this bad boy */
background: #3451ff;
}
DEMO HERE
You can use position:absolute and make its parent li position:relative Demo
#nav ul {
position:absolute;
right:0;
}
Use position:relative for ul parent and give position:absolute for parent child..it will be working fine.
Here is the updated fiddle :http://jsfiddle.net/nikhilvkd/gmU55/5/
#nav li li {
display: none;
position:relative;
}
#nav li:hover li {
display: block;
float: none;
position:absolute;
background: #555;
}
I'm stuck with a dropdown menu i made.
When I hover a menu item, a dropdown appears, but i can't seem to click the dropdown items because there is a blank space between the dropdown and the menu.
this is my code:
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
float : right;
padding: 0px 10px;
list-style: none;
position: relative;
display: inline-table;
}
nav ul:after {
content: ""; clear: both; display: block;
}
nav ul li {
float: left;
}
nav ul li:hover {
background: #fff;
}
nav ul li:hover a {
color: #000;
}
nav ul li a {
display: block; padding: 0px 10px;
color: #757575; text-decoration: none;
}
nav ul ul {
background: #fff; padding: 0;
position: absolute; top: 100%;
}
nav ul ul li {
float: none;
position: relative;
}
nav ul ul li a {
padding: 0px 20px;
color: #fff;
}
nav ul ul li a:hover {
background: #fff;
}
nav ul ul ul {
position: absolute; left: 100%; top:0;
}
does anyone has an idea?
you can see the code in : http://dropdown.kiran.be
ty
K
just add
left:0
to your nav ul ul (drop down menu)
and remove the margin-bottom from nav ul li (it inherits some margin from your base.css you can see whats going on if you use firebug)
Basically, you make the UL appear when you hover a li:
HTML
<ul>
<li>one</li>
<li>two
<ul class="sub">
<li>sub one</li>
<li>sub two</li>
</ul>
</li>
</ul>
CSS
ul li ul {
display:none;
position:absolute;}
ul li:hover ul.sub {
display:block;
}
Hope that helps!
You should remove the ul margin that you have now. So this is a possible solution:
ul li ul {
margin-left: 0;
}
You should even remove the margin on top of the drop-down menu. I can't click on it because it slips away!
i've a menu with sub menus in it, i've added border at bottom while hovering but when i hover on the menu the menu height increases a bit, the whole menu takes a space at top and bottom when i put it in a div.
here is my fiddle : http://jsfiddle.net/p7Nsf/
<div class="head"></div>
<div class="menudiv">
<div class="menu">
<ul>
<li>Home</li>
<li>About
<ul>
<li>School</li>
<li>Vision and Mission </li>
<li>Principal’s desk
<li>Management
</ul> </li>
<li>Admission
<ul>
<li>Overview</li>
<li>Download application form</li>
</ul> </li>
<li>Gallery</li>
<li>School Calander</li>
<li>News & Events</li>
<li>Career</li>
<li>Contact</li>
</ul>
</div>
</div><!-- menu div ends-->
<div class="foot"></div>
CSS
.menudiv
{
width:980px;
}
.menu {
font-family: 'Open Sans', sans-serif;
font-size:14px;
}
.menu ul ul {
display: none;
}
.menu ul li:hover > ul {
display: block;
}
.menu ul {
background: #111312;
list-style: none;
position: relative;
display: inline-table;
border:3px solid #111312;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
}
.menu ul:after {
content: "";
clear: both;
display: block;
}
.menu ul li {
float: left;
}
.menu ul li:hover {
background: #111312;
border-bottom: 3px solid #fff;
}
.menu ul li:hover a {
color: #fff;
}
.menu ul li a {
display: block;
padding-top: 10px; padding-left: 25px; padding-right: 25px; padding-bottom: 10px;
color: #fff;
text-decoration: none;
}
.menu ul ul {
background: #111312;
padding: 0;
position: absolute;
top: 100%;
}
.menu ul ul li {
float: none;
position: relative;
}
.menu ul ul li a {
padding: 10px;
color:#000;
display: block;
}
.menu ul ul li a:hover {
background: #111312;
color: #fff;
}
.menu ul ul ul {
position: absolute;
left: 100%;
top:0;
padding: 0;
}
.menu ul ul ul li {
float: none;
border-top: 1px solid #6b727c;
border-bottom: 1px solid pink;
position: relative;
}
.menu ul ul ul li a {
padding: 10px;
color: #fff;
display: block;
}
.menu ul ul ul li a:hover {
background: #95CEF1;
color: #000;
}
.menu ul ul ul ul {
position: absolute;
left: 100%;
top:0;
}
.head
{
width:500px;
height:200px;
background:#789;
}
.foot
{
width:500px;
height:200px;
background:#123;
}
Try this, your menu item jumps because you are adding border to the item so it increases its height by the 3 px border
.menu ul li {
float: left;
border-bottom: 3px solid transparent;
}
http://jsfiddle.net/p7Nsf/1/
reduce padding on the anchor to compensate for the 3px
.menu ul li a {
padding-bottom: 7px;
}
http://jsfiddle.net/p7Nsf/2/
Update
.menu ul {
background: none repeat scroll 0 0 #111312;
border: 3px solid #111312;
display: inline-table;
list-style: none outside none;
margin: 0;
padding: 0;
position: relative;
}
I removed the display prop
http://jsfiddle.net/p7Nsf/4/
I have been working on a drop down navigation which I have got close to how I like but I'm having trouble figuring out how to get the Logo (which is really just h1 text) and the tagline, to line up with my navigation.
The html for the logo and tagline is:
<div class="logo grid_5 omega">
<ul>
<li><h1>karma.</h1></li>
<li><p id="tagline">A stop motion animation</p></li>
</ul>
</div>
And it's corresponding CSS is this so far:
/* Header (Logo) -------------------------------------------------- */
.header.grid_12.omega {
margin-top:40px;
box-shadow:0 3px 10px #222;
background:#FFFFFF;
}
.logo.grid_5.omega {
float:left;
}
The navigation's html is:
<nav class="grid_7 omega">
<ul>
<li>About</li>
<li>Process
<ul>
<li>Models</li>
<li>Backgrounds</li>
<li>Animation</li>
<li>Post-production</li>
</ul>
</li>
<li>Style</li>
<li>Reflection</li>
</ul>
</div>
</nav>
And it's CSS is:
/* Drop down nav ---------------------------------------- */
nav ul ul {
display:none;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
background: #FFF;
padding: 0 20px;
list-style: none;
position: relative;
display: inline-table;
}
nav ul:after {
content: ""; clear: both; display: block;
}
nav ul li {
float: left;
}
nav ul li:hover {
background: #4b545f;
}
nav ul li:hover a {
color: #fff;
}
nav ul li a {
display: block; padding: 25px 40px;
color: #757575; text-decoration: none;
}
nav ul ul {
background: #5f6975; border-radius: 0px; padding: 0;
position: absolute; top: 100%;
}
nav ul ul li {
float: none;
border-top: 1px solid #6b727c;
border-bottom: 1px solid #575f6a;
position: relative;
}
nav ul ul li a {
padding: 15px 40px;
color: #fff;
}
nav ul ul li a:hover {
background: #4b545f;
}
However, as you can see: it's a little off:
http://imgur.com/HDofl9a
Give an id to your main ul and give some top margin?
HTML
<nav class="grid_7 omega">
<ul id="navi">
<li>About</li>
...
</ul>
</nav>
CSS
ul#navi{
margin:30px 0 0 0; /*adjust your top margin*/
}
I think you are looking for this
html
<div class="logo grid_5 omega">
<ul>
<li><h1>karma.</h1></li>
<li><p id="tagline">A stop motion animation</p></li>
</ul>
</div>
<nav class="grid_7 omega navigation">
<ul>
<li>About</li>
<li>Process
<ul>
<li>Models</li>
<li>Backgrounds</li>
<li>Animation</li>
<li>Post-production</li>
</ul>
</li>
<li>Style</li>
<li>Reflection</li>
</ul>
</div>
</nav>
css
.header.grid_12.omega {
margin-top:40px;
box-shadow:0 3px 10px #222;
background:#FFFFFF;
}
.logo.grid_5.omega {
float:left;
}
.logo.grid_5.omega li{
float: left;
line-height: 40px;
list-style-type: none;
margin-right: 25px;
padding-top: 14px;
}
.navigation{
float:left;
}
nav ul ul {
display:none;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
background: #FFF;
padding: 0 20px;
list-style: none;
position: relative;
display: inline-table;
}
nav ul:after {
content: ""; clear: both; display: block;
}
nav ul li {
float: left;
}
nav ul li:hover {
background: #4b545f;
}
nav ul li:hover a {
color: #fff;
}
nav ul li a {
display: block; padding: 25px 40px;
color: #757575; text-decoration: none;
}
jsFiddle File