I have made a drop down menu for a website I'm creating, it looks nice I think, though there is this one px space between the horizontal nav bar and the vertical drop down bars. When you hover your mouse on the space the vertical menu disappears again and since you sometimes put your mouse on the one px space it's very confusing for the users.
This is my html;
<div class="nav">
<div class="links">
<ul id="dropdown">
<li class="currentpage">
Tetterode
<ul>
<li>Project</li>
<li>Promenade</li>
<li>Brochure</li>
<li>Impressies</li>
</ul>
</li>
<li>
Woningen
<ul>
<li>Oplevering</li>
<li>Impressies</li>
<li>Kavelkaart</li>
</ul>
</li>
<li>
Locatie
<ul>
<li>Ligging</li>
<li>Routplanner</li>
<li>Situatie</li>
</ul>
</li>
<li>
Financiering
<ul>
<li>Hypotheken</li>
<li>Fiscale mogelijkheden</li>
</ul>
</li>
<li>
Contact
<ul>
<li>Makelaars</li>
<li>Gegevens</li>
</ul>
</li>
</ul>
</div>
</div>
And this is my css;
.nav{
width: 100%;
height:50px;
background-image:url("bg.jpg");
background-repeat:repeat;
text-align:center;
padding-bottom:0px;
margin-bottom:0px;
}
.links ul li {
list-style-type: none;
padding-right: 17px;
height:50px;
position:relative;
display:inline-block;
padding-top:0px;
line-height:50px;
padding-left:17px;
text-shadow: 3px 3px 2px rgba(0, 0, 0, 0.50);
margin-top:0px;
}
.links ul li:hover {
height:50px;
background-color:#b2071a;
display:inline-block;
margin:0px;
}
.links a {
text-transform:uppercase;
font-family:helvetica;
font-size:18px;
color:#fff;
display:inline-block;
font-size:20px;
text-decoration:none;
}
.links a:hover {
background-color:transparent;
}
#dropdown ul{
background-color:#b2071a;
list-style:none;
position:absolute;
left:-9999px;
z-index:20;
font-size:16px;
padding-top:0px;
margin-top:0px;
}
#dropdown ul li{
float:none;
}
#dropdown ul a{
white-space:nowrap;
font-size:16px;
}
#dropdown li:hover ul{
left:0;
}
#dropdown li:hover a{
text-decoration:underline;
}
#dropdown li:hover ul a{
text-decoration:none;
}
#dropdown li:hover ul li a:hover{
color:#000;
}
.currentpage{
height:50px;
background-color:#b2071a;
display:inline-block;
}
I don't see any margin/padding that causes the 1 px space and I don't know how to solve this. I hope someone can help me, thank you.
Your code and style sheet are pretty good, easy to read and follow.
A solution is to tweak the top margin of #dropdown ul to -2px as follows:
#dropdown ul{
background-color:#b2071a;
list-style:none;
position:absolute;
left:-9999px;
z-index:20;
font-size:16px;
padding-top:0px;
margin-top:-2px;
}
Negative margins are allowed and can be quite useful. The problem you encountered is quite common in pull-down menus and this fix is a typical one.
For reference, see the fiddle: http://jsfiddle.net/audetwebdesign/Ygn6X/
Related
I am trying to center my nav bar. I checked my sites but nothing works
and I'm completely stumped as to why it won't center, here is my navigation bar HTML that I want to center:
<div class='nav'>
<ul class='menu' id='menu'>
<li><a expr:href='data:blog.homepageUrl'>home</a></li>
<li><a class='drop-ctg' href='#'>PORTFOLIO</a>
<ul>
<li><a href='http://effectsforshare.blogspot.com/p/trailer.html'>TRAILER</a></li>
<li><a href='http://effectsforshare.blogspot.com/p/motion-graphics.html'>MOTION GRAPHICS</a></li>
<li><a href='http://effectsforshare.blogspot.com/p/trapcode-particular.html'>TRAPCODE PARTICULAR</a></li>
</ul>
</li>
<li><a href='http://effectsforshare.blogspot.com/'>TEMPLATES</a></li>
<li><a href='http://effectsforshare.blogspot.com/p/my-blog.html'>MY BLOG</a></li>
<li><a href='http://effectsforshare.blogspot.com/p/contact-me.html'>CONTACT ME</a></li>
</ul>
</div>
and here is the CSS
*{
margin:0;
padding:0;
outline:0;
}
.nav {
width:950px;
height:auto;
border-bottom:1px solid #eee;
margin:10px auto 5px;
display:inline-block;
}
.menu {
width:auto;
list-style:none;
font:$pagenavifont;
text-align:center;
margin:0 auto;
}
.menu a {
float:left;
color:#999;
text-decoration:none;
text-transform:uppercase;
width:auto;
line-height:36px;
padding:0 20px;
}
.menu a:hover,li.menuhover a{
color:#111;
}
.menu li {
position:relative;
float:left;
width:auto;
}
.menu li:last-child {
background:none;
}
.menu ul{
display:none;
position:absolute;
top:36px;
left:0;
background:#fbfbfb;
display:none;
list-style:none;
}
.menu ul li{
float:none;
border-top:1px solid #e3e3e3;
border-right:1px solid #e3e3e3;
border-left:1px solid #e3e3e3;
width:auto;
background:none;
}
.menu ul li:last-child {
border-bottom:1px solid #e3e3e3
}
.menu ul li a{
float:none;
display:block;
background:none;
line-height:36px;
min-width:137px;
width:auto;
text-align:left;
padding-left:10px;
color:#444;
}
.menu ul li a:hover{
background:#fdfdfd;
color:#777;
}
I would give you a link to the page but it's being done in Dreamweaver and is not up yet. The logobar.jpg is the logo for the webpage. I love how it looks, but it needs to be centered and not be cut off or taken to the next line when I shrink my screensize.
I tried each of float: right, float: left, and float: none on almost all of the classes; text-align: center on each class on the html side; I have tried align=center on each class; display: inline, inline-block on ul and li classes.
Thank you for your help!
On your .menu class, you do have text-align:center set. We can certainly use that to center the child nodes.
So within your .menu li css property, add display:inline-block and remove the floats. This will center your list items.
See the fiddle: http://jsfiddle.net/RGREA/
<style>
div.nav{margin: 3px auto;}
</style>
I'm currently experiencing an issue where my ul is being pushed out of my div. I have played with the margin, border, and padding on all the objects that might be affecting it with no success as of yet, here is the current code:
HTML:
<div id="nav">
<ul id=mainNav>
<li class=mainNav><a>Home</a></li>
<li class=mainNav><a>Products</a></li>
<li class=mainNav><a>Stores</a></li>
<li class=mainNav><a>About Us</a></li>
<li class=mainNav><a>Contact Us</a></li>
<li class=mainNav><a>Pet Pics</a></li>
</ul>
</div>
</div>
<div id="subBar">
<div id="subNav">
<ul id=subNavl>
<li class=subNav><a>Home</a></li>
<li class=subNav><a>Products</a></li>
<li class=subNav><a>Stores</a></li>
<li class=subNav><a>About Us</a></li>
<li class=subNav><a>Contact Us</a></li>
<li class=subNav><a>Pet Pics</a></li>
</ul>
</div>
</div>
CSS:
#header{
width:1000px;
background-color:#B443B7;
border-radius:200px;
height:50px;
margin:auto;
margin-top:0px;
margin-bottom:opx;
}
h1{
display:inline;
color:white;
margin-left:20px;
font-family: 'Indie Flower', cursive;
}
h1, a{
text-decoration:none;
color:#B443B7;
margin-top:auto;
margin-bottom:60px;
}
#subBar {
height:20px;
padding-top:10px;
width:875px;
margin-right:auto;
margin-left:auto;
background-color:#F4F459;
color:black;
border-radius: 0px 0px 200px 200px;
}
#nav {
margin:auto;
}
#subNav {
margin:auto;
}
#mainNav {
list-style-type:none;
}
.mainNav {
display:inline
}
.mainNav a {
display:inline-block;
height:35px;
padding-top:15px;
color:white;
width:100px;
text-align:center;
font-family:arial, sans-serif;
font-size:16px;
}
.mainNav a:hover{
background-color:#F4F459;
color:black;
}
#subNavl {
list-style-type:none;
}
.subNav {
display:inline
}
.subNav a {
display:inline-block;
height:30px;
margin-top:0px;
padding-top:0px;
color:black;
width:80px;
text-align:center;
font-family:arial, sans-serif;
font-size:16px;
}
And here's a screen shot of the results:
It would be much appreciated if someone could help me in resolving this problem.
http://jsbin.com/gorabonoqe/1/
I dropped your code in a JSBin and didn't look exactly like your screenshot but I think I was able to address the area you were trying to fix (the text dropping out of the yellow bar).
There's a lot of potential issues going on here. For one, all <a> elements have a bottom margin of 60px. I think that's what was throwing you off the most. I also changed the padding on #subBar and added a margin:0 on #subNavl.
Take a look at the jsbin link above and see if that's helpful.
Just to let you know .. when you work with ul > li > a
ul{
list-style: none;
list-style-type: none;
margin : 0;
padding: 0;
}
ul > li{
display : inline-block;
}
ul > li > a{
display: block;
height: 100%;
}
this way will make everything comes easy in your ul code
You set the height of that div, #subBar, to 20px and the browser is obeying that.
I am relatively new to CSS and I'm having trouble getting my navigation bar aligned horizontally. I tried the display:inline; function with no success. When using the float:left function, I get all the li aligned horizontally, but in this case, I cannot center it on the page. Also the margin:0 auto; does not do it in this case.
The navigation bar is created via an ul inside a DIV, which is also contained inside a "wrapper div".
Is the problem linked to the various ID selectors used and the nesting of the various elements?
Please find the jsfiddle below for visualisation:
JsFiddle
Any help would be greatly appreciated :)
Thanks, Danny
HTML
<div id="wrapper">
<div id="navigation">
<ul>
<li>HOME</li>
</ul>
<ul>
<li>GALLERY</li>
<li class="sublinks">
PORTRAITURE
NATURE
</li>
</ul>
<ul>
<li>BLOG</li>
</ul>
<ul>
<li>ABOUT</li>
</ul>
<ul>
<li>MODEL RELEASE</li>
</ul>
<ul>
<li>CONTACT</li>
</ul>
</div>
</div>
CSS
ul {
list-style:none;
margin:0px 5px;}
ul li {
display:inline;}
ul li a {
color:#ffffff;
background:#09F;
padding:0px 0px;
margin:0 auto;
font-weight:bold;
font-size:14px;
font-family:'sans open', Arial, Helvetica, sans-serif;
text-decoration:none;
display:block;
width:auto;
height:40px;
line-height:15px;
text-align:center;
-webkit-border-radius:5px;
-moz-border-radius:5px;
border: 1px solid #560E00;}
ul li a:hover {
color:#cccccc;
background:#06F;
font-weight:bold;
text-decoration:none;
display:block;
width:auto;
text-align:center;
-webkit-border-radius:5px;
-moz-border-radius:5px;
border: 1px solid #000000;}
ul li.sublinks a {
color:#000000;
background:#f6f6f6;
border-bottom:1px solid #cccccc;
font-weight:normal;
text-decoration:none;
display:block;
width:100px;
text-align:center;
margin-top:2px;}
ul li.sublinks a:hover {
color:#000000;
background:#FFEFC6;
font-weight:normal;
text-decoration:none;
display:block;
width:100px;
text-align:center;}
ul li.sublinks {
display:none;}
Your ul needs inline-block and the menu wrapper needs text-align:center plus some width to make menu center regardless of an ammount of menu items.
I've updated your fiddle here: jsFiddle
You just need to make the the ul and ul li display inline-block. I also added text-align: center; to the #navigation element.
Here is the working code example on JsFiddle: http://jsfiddle.net/hhjfypmt/
You could do as said above, but if you also want to align it to the left/right, you can add a float:right (or left) instead, to your ul li:
ul {
list-style:none;
margin:0px 0px;
}
ul li {
display:inline-block;
float:left;
}
ul li a {
color:#ffffff;
background:#09F;
padding:0px 0px;
margin:0 auto;
font-weight:bold;
font-size:14px;
font-family:'sans open', Arial, Helvetica, sans-serif;
text-decoration:none;
width:auto;
height:40px;
line-height:15px;
text-align:center;
-webkit-border-radius:5px;
-moz-border-radius:5px;
border: 1px solid #560E00;
}
ul li a:hover {
color:#cccccc;
background:#06F;
font-weight:bold;
text-decoration:none;
width:auto;
text-align:center;
-webkit-border-radius:5px;
-moz-border-radius:5px;
border: 1px solid #000000;
}
ul li.sublinks a {
color:#000000;
background:#f6f6f6;
border-bottom:1px solid #cccccc;
font-weight:normal;
text-decoration:none;
width:100px;
text-align:center;
margin-top:2px;
}
ul li.sublinks a:hover {
color:#000000;
background:#FFEFC6;
font-weight:normal;
text-decoration:none;
width:100px;
text-align:center;
}
ul li.sublinks {
display:none;
}
/* CSS For Dropdown Menu End */
<div id="wrapper">
<div id="navigation">
<ul>
<li>HOME</li>
</ul>
<ul>
<li>GALLERY</li>
<li class="sublinks">
PORTRAITURE
NATURE
</li>
</ul>
<ul>
<li>BLOG</li>
</ul>
<ul>
<li>ABOUT</li>
</ul>
<ul>
<li>MODEL RELEASE</li>
</ul>
<ul>
<li>CONTACT</li>
</ul>
</div>
</div>
How can it be that there are so many answers on this topic and I still can't figure this out? I've been fiddling with the CSS on jsfiddle for hours and I still don't understand why my navigation bar won't center without going into a vertical list.
The html:
<div class='nav'>
<ul class='menu' id='menu'>
<li><a expr:href='data:blog.homepageUrl'>home</a></li>
<li><a class='drop-ctg' href='a'>MAKEUP</a>
<ul>
<li><a href='a'>EYES</a></li>
<li><a href='a'>LIPS</a></li>
<li><a href='a'>FACE</a></li>
</ul>
</li>
<li><a href='a'>SKINCARE</a></li>
<li><a href='a'>LIFESTYLE</a></li>
<li><a href='a'>DIY</a></li>
<li><a href='a'>CONTACT</a></li>
</ul>
</div>
and the CSS, I think:
*{
margin:0;
padding:0;
outline:0;
}
.nav {
width:950px;
height:auto;
border-bottom:1px solid #eee;
margin:10px auto 5px;
display:inline-block;
}
.menu {
width:auto;
list-style:none;
font:$pagenavifont;
text-align:center;
margin:0 auto;
}
.menu a {
float:left;
color:#999;
text-decoration:none;
text-transform:uppercase;
width:auto;
line-height:36px;
padding:0 20px;
}
.menu a:hover,li.menuhover a{
color:#111;
}
.menu li {
position:relative;
float:left;
width:auto;
}
.menu li:last-child {
background:none;
}
.menu ul{
display:none;
position:absolute;
top:36px;
left:0;
background:#fbfbfb;
display:none;
list-style:none;
}
.menu ul li{
float:none;
border-top:1px solid #e3e3e3;
border-right:1px solid #e3e3e3;
border-left:1px solid #e3e3e3;
width:auto;
background:none;
}
.menu ul li:last-child {
border-bottom:1px solid #e3e3e3
}
.menu ul li a{
float:none;
display:block;
background:none;
line-height:36px;
min-width:137px;
width:auto;
text-align:left;
padding-left:10px;
color:#444;
}
.menu ul li a:hover{
background:#fdfdfd;
color:#777;
}
I just started my blog today, and so far I've learned that getting rid of floats and putting inline-block might help, but there are so many that I really don't get which code applies to what. Any help is appreciated!
Here's the fiddle link: http://jsfiddle.net/vFDrV/9/
Here's the link to my blog: http://theprettyfoxes.blogspot.com/
if I understand correctly your question, its quite simple.
Add the follow code to your menu css class.
.menu { /* applying to a ul element */
/* ... your code ... */
display: inline-block;
}
You can read more about this at the Mozilla Docs
https://developer.mozilla.org/en-US/docs/Web/CSS/display
What it's going on when we add "inline-block" is this:
The element generates a block element box that will be flowed with
surrounding content as if it were a single inline box (behaving much
like a replaced element would)
Thats all!
remove float from following:
.menu a {
/*float: left;*/
color: #999;
text-decoration: none;
text-transform: uppercase;
width: auto;
line-height: 36px;
padding: 0px 20px;
}
.menu li {
position: relative;
/*float: left;*/
width: auto;
display: inline; /* <- add this */
}
basically i'm trying to create a navigation bar and I'm trying to position two (and more) li elements next to each other, I've tried to figure it out but can't seem to do it???
css is below.... html is below css
don't even know whether i've laid it out correctly, if you know a tutorial or something to do the same thing let me know I've spent days figuring out how to set it up correctly......
CSS
li {
list-style-type:none;
list-style:none;
background:clear;
padding:0;
margin:0;
}
ul {
list-style-type:none;
list-style:none;
margin:0;
padding:0;
background-color:clear;
}
li.nav a {
text-align:center;
font-family:"Helvetica";
font-weight:lighter;
text-decoration:none;
display:inline;
background-color:green;
padding:10px;
margin:0;
color:#FF0;
float:none;
}
li.nav a:hover {
text-align:center;
font-family:"Helvetica";
font-weight:lighter;
text-decoration:none;
display:inline;
background-color:blue;
padding:10px;
margin:0;
color:#0FF;
}
li.sub a {
position:relative;
top:2px;
background:pink;
display:block;
margin:0;
padding:10px;
color:red;
width:100px;
}
li.sub a:hover {
position:relative;
top:2px;
background:pink;
display:block;
margin:0;
padding:10px;
color:red;
width:100px;
}
HTML
<li class="nav">Community
<ul>
<li class="sub">Third Age</li>
<li class="sub">Tide Timetables</li>
<li class="sub">Schools</li>
<li class="sub">Religion</li>
<li class="sub">Clubs+Societies</li>
<li class="sub">Courses</li>
<li class="sub">The Council</li>
<li class="sub">Culture</li>
</ul>
</li>
<!--END OF COMMUNITY SECTION-->
<!--START OF EVENTS NAVIGATION BUTTON AND SUBMENU-->
<li class="nav">Events
<ul>
<li class="sub">Festivals</li>
<li class="sub">Family</li>
<li class="sub">Music</li>
<li class="sub">Comedy</li>
<li class="sub">Theatre</li>
<li class="sub">Exhibitions</li>
<li class="sub">Film</li>
<li class="sub">Literature</li>
</ul>
</li>
<!--END OF EVENTS SECTION-->
Try to add this lines in your css.
li.nav{display:inline-block;position:absolute;}
li.nav:nth-child(2)
{margin-left:140px;}
OR
HTML
<div class="main-navigation">
<nav class="navigation">
<ul>
<li>1
<ul class="sub-menu"><li>1a</li><li>1b</li></ul>
</li>
<li>2
<ul class="sub-menu"><li>2a</li><li>2b</li></ul>
</li>
<li>3</li>
<li>4</li>
</ul>
</nav>
</div>
CSS
nav.navigation{
width: 75%;
height:50px;
}
nav ul ul.sub-menu{
display: none;
margin-left:-40px;
margin-top:-10px;
}
nav ul li:hover > ul.sub-menu{
display: block;
}
nav ul{
height:50px;
list-style:none;
padding: 3px 0 0 0;
position: relative;
text-transform: uppercase;
}
nav ul:after{
content:"";
clear:both;
display:block;
}
nav ul li{
float: left;
padding:10px 30px;
background:yellow;
margin-left:5px;
}
nav ul ul.sub-menu{
background-color: #fcfcfc;
padding: 0;
position: absolute;
z-index:100;
top: 100%;
}
nav ul ul.sub-menu li{
float: none;
margin-top: -3px;
height:30px;
background-color:green;
}
The DEMO is here
add float:left; to the li-items.
li
{
float:left;
}
DEMO
you can use display:inline-block level property to all li element.