I'm creating a Horizontal menu with a single line drop down sub nav.
When I rollover my main navigation links it displays the sub nav but pushes the rest of the content to the right. I would like the submenu to appear to the far left of the main menu and for it not to affect the main menu. Thanks!
Fiddle
HTML
<ul id="nav">
<li>LINK1</li>
<li>LINK2
<ul>
<li>LINK3</li>
<li>LINK4</li>
<li>LINK5</li>
<li>LINK6</li>
</ul></li>
<li>LINK7</li>
<li>LINK8</li>
CSS
#nav{
display:block;
list-style:none;
text-align: left;
}
#nav a{
width: 50px;
height: 15px;
text-decoration:none;
display:block;
font-size:12px;
font-family: Arial, Verdana, Helvetica, sans-serif;
text-align:center;
color:#666666;
border-left: 1px solid #999999;
float:left;
padding: 10px 5px 5px 5px;
}
#nav a:hover, #nav a:focus {
background: #999 ;
color:#333;
}
#nav li {
height: 30px;
display:block;
position:relative;
float:left;
}
#nav li ul {
height: 30px;
position: absolute;
left: -9999px;
}
#nav li:hover ul {
position: relative;
left: 0;
clear: both;
}
#nav li li a{
width: 50px;
text-decoration:none;
display:block;
font-size:9px;
font-family: Arial, Verdana, Helvetica, sans-serif;
text-align:center;
border-left: 1px solid #999999;
color:#666666;
padding: 5px 0 5px 0;
z-index: 10000;
vertical-align: middle;
}
Made a few changes and it's working, mainly:
#nav li:hover ul {
left: 0;
top: 100%;
white-space: nowrap;
word-spacing: -1em;
clear: both;
}
#nav li li {
word-spacing: normal;
display: inline-block;
float: none;
}
jsFiddle Demo
you can use it too
fiddle : http://jsfiddle.net/8GYbh/9/
#nav li {
height: 30px;
display:inline;
}
#nav li:hover ul {
position: absolute;
left: 0;
clear: both;
top:45px;
}
Related
I'm fairly new to CSS/HTML and am trying to make a horizontal nav with a dropdown menu. I have made the nav but I am having trouble getting it centred on the page.
#NavigationTop ul{
list-style: none;
position:relative;
margin:0 auto;
padding-right: 1px;
width: 1075px;
}
#NavigationTop ul a{
color:#ffffff;
text-decoration:none;
font-weight:700;
font-size:15px;
padding:0 15px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
line-height: 50px;
text-transform: uppercase;
}
#NavigationTop ul li{
background-color: #343434;
width: 215px;
height: 50px;
position:relative;
float: left;
margin:0 auto;
text-align: center;
border-top: 1px solid #000000;
border-bottom: 1px solid #000000;
}
#NavigationTop ul li:hover{
background-color: lightseagreen;
}
#NavigationTop ul li a:visited{
color: #ffffff
}
#NavigationTop ul ul{
display:none;
position:absolute;
top:100%;
left:0;
padding:0;
margin: 0;
}
#NavigationTop ul ul li{
float:none;
width:215px;
border-bottom: 1px solid;
}
#NavigationTop ul li:hover > ul{
display: block;
}
<nav id="NavigationTop">
<div id="main-nav">
<ul>
<li>HOME</li>
<li>BIOGRAPHY</li>
<li>DISCOGRAPHY</li>
<li>MEDIA
<ul>
<li>Audio
<li>Video
<li>Photos
</ul>
</li>
<li>CONTACT</li>
</ul>
</div>
</nav>
I'm sure there are many errors, but what could I do to make it centred? Also is it possible to have the nav hide the overflow from the main ul and still show the drop-down menu? Thank you.
Change only this:
#NavigationTop ul {
list-style: none;
position: relative;
margin: 0 auto;
padding-right: 1px;
display: inline-block;
}
div#main-nav {
text-align: center;
}
Whenever you want to make a div center or any html element. you need a container( eg. div or ul or any other) and place the html element inside that container and give specific width to that container with property margin: 0 auto .
By specifying width, your content will not spread across the viewport(brower screen) and when you specify Margin, in shorthand notion( 0 auto ) means 0 will hold the margin for Top & Bottom while auto will hold Left & Right automatically by browser.
Then whatever you will place that container, you can use Floating easily so that your design looks perfect.
Remove the width from #NavigationTop ul
Add specific width till your menu not breaking then add that on #main-nav
#main-nav{
width: 1120px; //in your design case i found it this in px.
margin: 0 auto;
}
#NavigationTop ul{
list-style: none;
position:relative;
margin:0 auto;
padding-right: 1px;
}
#main-nav{
width: 1120px; //in your design case i found it this in px.
margin: 0 auto;
}
#NavigationTop ul{
list-style: none;
position:relative;
margin:0 auto;
padding-right: 1px;
}
#NavigationTop ul a{
color:#ffffff;
text-decoration:none;
font-weight:700;
font-size:15px;
padding:0 15px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
line-height: 50px;
text-transform: uppercase;
}
#NavigationTop ul li{
background-color: #343434;
width: 215px;
height: 50px;
position:relative;
float: left;
margin:0 auto;
text-align: center;
border-top: 1px solid #000000;
border-bottom: 1px solid #000000;
}
#NavigationTop ul li:hover{
background-color: lightseagreen;
}
#NavigationTop ul li a:visited{
color: #ffffff
}
#NavigationTop ul ul{
display:none;
position:absolute;
top:100%;
left:0;
padding:0;
margin: 0;
}
#NavigationTop ul ul li{
float:none;
width:215px;
border-bottom: 1px solid;
}
#NavigationTop ul li:hover > ul{
display: block;
}
<nav id="NavigationTop">
<div id="main-nav">
<ul>
<li>HOME</li>
<li>BIOGRAPHY</li>
<li>DISCOGRAPHY</li>
<li>MEDIA
<ul>
<li>Audio
<li>Video
<li>Photos
</ul></li>
<li>CONTACT</li>
</ul>
</div>
</nav>
you can try this one https://jsfiddle.net/mnd1b51y/1/
#NavigationTop ul li { position: relative;}
#NavigationTop ul li:hover > ul {
left: 0;
max-width: 210px;
position: absolute;
top: 51px;
}
In #NavigationTop ul class remove padding-left:0 because by default ul takes padding and the default seems to be padding-left:40px for ul.
#NavigationTop ul{
list-style: none;
position:relative;
margin:0 auto;
padding-right: 1px;
width: 1075px;
overflow:auto;
padding-left:0;
}
#NavigationTop ul a{
color:#ffffff;
text-decoration:none;
font-weight:700;
font-size:15px;
padding:0 15px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
line-height: 50px;
text-transform: uppercase;
}
#NavigationTop ul li{
background-color: #343434;
width: 215px;
height: 50px;
position:relative;
float: left;
margin:0 auto;
text-align: center;
border-top: 1px solid #000000;
border-bottom: 1px solid #000000;
}
#NavigationTop ul li:hover{
background-color: lightseagreen;
}
#NavigationTop ul li a:visited{
color: #ffffff
}
#NavigationTop ul ul{
display:none;
position:absolute;
top:100%;
left:0;
padding:0;
margin: 0;
}
#NavigationTop ul ul li{
float:none;
width:215px;
border-bottom: 1px solid;
}
#NavigationTop ul li:hover > ul{
display: block;
}
<nav id="NavigationTop">
<div id="main-nav">
<ul>
<li>HOME</li>
<li>BIOGRAPHY</li>
<li>DISCOGRAPHY</li>
<li>MEDIA
<ul>
<li>Audio
<li>Video
<li>Photos
</ul>
</li>
<li>CONTACT</li>
</ul>
</div>
</nav>
This is my css code:
#navigation
{
width: 100%;
height:31px;
background:#333;
}
#navigation ul
{
margin: 0;
padding: 0;
}
#navigation ul li
{
list-style-type: none;
display: inline;
}
#navigation li a
{
display: block;
float: right;
color:#CCC;
text-decoration: none;
padding-top: 7px;
padding-right: 10px;
padding-bottom: 7px;
padding-left: 10px;
font-family: Tahoma, Geneva, sans-serif;
font-size: 14px;
font-weight:bold;
}
#navigation li a:hover
{
color:#FFF;
}
And this is my html code:
<div id="navigation">
<ul>
<li><div>menu1</div></li>
<li><div>menu2</div></li>
<li><div>menu3</div></li>
<li><div>menu4</div></li>
</ul>
</div>
Here every link tag has hover event. But I want to remove hover event for only menu1. I mean user can click on menu2, menu3 and menu4 but menu1 doesn't have hover event so when user click on it, nothing happen.
try this DEMO
#navigation
{
width: 100%;
height:31px;
background:#333;
}
#navigation ul
{
margin: 0;
padding: 0;
}
#navigation ul li
{
list-style-type: none;
display: inline;
}
#navigation li a
{
display: block;
float: right;
color:#CCC;
text-decoration: none;
padding-top: 7px;
padding-right: 10px;
padding-bottom: 7px;
padding-left: 10px;
font-family: Tahoma, Geneva, sans-serif;
font-size: 14px;
font-weight:bold;
}
#navigation li:hover:nth-of-type(2) a
{
color:#FFF;
}
#navigation li:hover:nth-of-type(3) a
{
color:#FFF;
}
#navigation li:hover:nth-of-type(4) a
{
color:#FFF;
}
#navigation li:not(:first-child) a:hover
{
color:#FFF;
}
Fiddle here
For selecting second elements and next, you can use + selector in CSS:
#navigation li + li a:hover
{
color: #FFF;
}
From W3C:
E + F: Matches any F element immediately preceded by a sibling element E.
For disabling the link, you should change your markup, or use JavaScript.
Add a id or class to the first element (The one you dont want to hover)
Add this css to that class or id
#id-you-used:hover {
color: #CCC;
}
.class-you-used:hover {
color: #CCC;
}
Your HTML is broken for first two links
<li><div><a href="">menu1/a></div></li>
<li><div><a href="">menu2/a></div></li>
should be :
<li><div>menu1</div></li>
<li><div>menu2</div></li>
One other easy answer:
css code:
#charset "utf-8";
/* CSS Document */
html, body
{
margin:0;
padding:0;
height:100%;
}
#navigation
{
width: 100%;
height:31px;
background:#333;
}
#navigation ul
{
margin: 0;
padding: 0;
}
#navigation ul li
{
list-style-type: none;
display: inline;
}
#navigation li a
{
display: block;
float: right;
color:#CCC;
text-decoration: none;
padding-top: 7px;
padding-right: 10px;
padding-bottom: 7px;
padding-left: 10px;
font-family: Tahoma, Geneva, sans-serif;
font-size: 14px;
font-weight:bold;
}
#username
{
display: block;
float: left;
color:#CCC;
text-decoration: none;
padding-top: 7px;
padding-right: 10px;
padding-bottom: 7px;
padding-left: 10px;
font-family: Tahoma, Geneva, sans-serif;
font-size: 14px;
font-weight:bold;
background-color:#078415;
}
#navigation a:hover
{
color:#FFF;
}
html code:
<div id="navigation">
<ul>
<li><div id="username">menu1</div></li>
<li><div>menu2</div></li>
<li><div>menu3</div></li>
<li><div>menu4</div></li>
</ul>
</div>
One more easy answer:
css code:
#charset "utf-8";
/* CSS Document */
html, body
{
margin:0;
padding:0;
height:100%;
}
#navigation
{
width: 100%;
height:31px;
background:#333;
}
#navigation ul
{
margin: 0;
padding: 0;
}
#navigation ul li
{
list-style-type: none;
display: inline;
}
#navigation li a
{
display: block;
float: right;
color:#CCC;
text-decoration: none;
padding-top: 7px;
padding-right: 10px;
padding-bottom: 7px;
padding-left: 10px;
font-family: Tahoma, Geneva, sans-serif;
font-size: 14px;
font-weight:bold;
}
#navigation a.active:hover
{
color:#FFF;
}
html code:
<div id="navigation">
<ul>
<li style="float:left; background-color:#055C06;"><div><a>menu1</a></div></li>
<li style="float:left; background-color:#DA4A38;"><div><a class="active" href="">menu2</a></div></li>
<li><div><a class="active" href="">menu3</a></div></li>
<li><div><a class="active" href="">menu4</a></div></li>
</ul>
</div>
I am new to CSS and I need help with the navigation bar. I need to get the sub-topic right under the ABOUT US part but I cannot get it, it appears all the way to the right. Anything would be appreciated.
I can provide the HTML file if you need.
CSS:
container{
position: relative;
height: 70px;
width: 1100px;
}
.masthead{
background: #039be5;
width: 100%;
top: 0;
position: fixed;
color: white;
}
.logo{
position: relative;
float: left;
left: 90px;
font-family: Josefin Slab;
font-size: 21px;
top: 17px;
}
.logo h1 a {color: white; text-decoration: none; }
h1{
margin: 0;
}
a:link {color: white; text-decoration: none; }
a:visited {color: white; text-decoration: none; }
a:hover {color: white; color: black; }
a:active {color: white; text-decoration: none; }
#navcontent{
word-spacing: 4px;
}
li{
position: relative;
left: 155px;
list-style: none;
font-family: Raleway;
float: left;
margin-left: 21px;
padding-top: 15px;
font-size: 20px;
}
.navigation{
float: right;
}
.navigation ul>li ul{
height: 100%;
position: relative;
bottom: 100%;
}
.navigation ul>li ul>li{
bottom: 0px;
display: none;
}
.navigation>ul>li:hover ul>li{
display: block;
}
.navigation>ul>li a:hover {
text-decoration: none;
cursor:pointer;
}
HTML
<div class="masthead">
<div class="container">
<div class="logo">
<h1> MY SITE</h1>
</div>
<div class="navigation">
<ul>
<li>Courses
</li>
<li>Places
</li>
<li>More
<ul>
<li>Share
</li>
<li> Help
</li>
</ul>
</li>
<li id="navcontent">|| Sign In
</li>
<li>Sign Up
</li>
</ul>
</div>
</div>
</div>
Fiddle
To the point,,
CSS
ul{
list-style:none;
position:relative;
float:left;
margin:0;
padding:0
}
ul a
{
display:block;
color:#333;
text-decoration:none;
font-weight:700;
font-size:12px;
line-height:32px;
padding:0 15px;
}
ul li
{
position:relative;
float:left;
margin:0;
padding:0
}
ul li:hover
{
background:#f6f6f6
}
ul ul
{
display:none;
position:absolute;
top:100%;
left:0;
background:#fff;
padding:0
}
ul ul li
{
float:none;
width:200px
}
ul ul a
{
line-height:120%;
padding:10px 15px
}
ul ul ul
{
top:0;
left:100%
}
ul li:hover > ul
{
display:block
}
Live result here.
Hope this help
UPDATE
Ok, due to what Jon P ask I will describe this code.
Actually the main keys are :
ul ul
{
display:none;
position:absolute;
top:100%;
left:0;
background:#fff; /*remove, no effect*/
padding:0 /*remove, no effect*/
}
First, we make the child(<ul>) become hidden with display: none and give it properties position:absolute, top:100%, left:0; to let it under the MORE link
And then what we want? show it when we hover the MORE link. to do so, just do simple by making that child(<ul>) visible. Of course, we use this property :
ul li:hover > ul
{
display:block
}
I'm trying to center the navigation, but I haven't succeeded yet. The idea is that the navigation moves along as the user scrolls the page. It has to be responsive as well, because navigation should always be displayed. So I've come up with this:
<nav id="nav" class="container">
<ul>
<li><a href="#avaleht" class="avaleht" title="Avaleht" >avaleht</a></li>
<li><a href="#massaazh" class="massaazh" title="Massaaž" >massaaž</a></li>
<li><a href="#kontakt" class="kontakt" title="Kontakt" >kontakt</a></li>
</ul>
</nav>
And the CSS:
#nav
{
position: fixed;
}
#nav ul
{
list-style: none;
padding-left: 0;
text-align: center;
}
#nav li
{
display: inline-block;
}
#nav a
{
color: rgb(255,255,255);
font-family: 'Open Sans', sans-serif;
font-weight: 500;
text-transform: uppercase;
font-size: 1.3em;
display: block;
margin-right: 80px;
height: 100px;
width: 100px;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
border-radius: 50%;
line-height: 100px;
}
#nav a.avaleht
{
background: rgb(168,191,18);
}
#nav a.massaazh
{
background: rgb(255,159,0);
}
#nav a.kontakt
{
background: rgb(0,170,181);
}
#nav a:hover
{
text-decoration: none;
background: rgb(66,64,62);
}
And this is how I'd like it to work:
Thank you very much.
Just add left:0; right:0; to the fixed #nav element:
#nav {
position: fixed;
left:0; right:0;
}
Then remove the margin-right for the last li element's child anchor element:
#nav li:last-child a {
margin-right:0;
}
Example Here
#nav {
position: fixed;
left:0;
right:0;
}
and replace margin-right:80 with margin:5px 40px
#nav a{
margin: 5px 40px;
}
How do I center my navigation bar links using CSS while keeping the sides grey?
Blogs and History have a drop a down menu. Here is a screenshot:
(source: gyazo.com)
CSS:
.navbar ul {
list-style: none;
margin: 0;
padding: 0;
}
.navbar li {
float: left;
width: 100px;
background-color: #444444;
text-align: center;
border-right: 1px solid white;
position: relative;
height: 30px;
line-height: 30px;
}
.navbar li ul li {
float: none;
width: 150px;
text-align: left;
padding-left: 5px;
border-top: 1px solid white;
}
.navbar a {
text-decoration: none;
color: white;
}
.navbar li ul {
position: absolute;
top: 30px;
left: 0;
visibility: hidden;
}
.navbar li:hover ul {
visibility: visible;
}
.navbar li:hover {
background-color: maroon;
}
Edited my answer.. I've made your Container div clear the float, and set a defined width to navbar.
http://jsfiddle.net/jFdhM/
#container {
border-radius: 10px;
box-shadow: 3px 3px 4px;
padding: 8px;
background-color: #FFFFFF;
width: 748px;
margin: 0 auto;
}
#top {
width: 748px;
}
.navbar {
width: 505px;
margin:0 auto;
}
.navbar ul {list-style:none; padding:0; margin: 0 auto;text-align: center;}
.navbar li {float:left; width:100px; background-color:#444444; text-align:center;
border-right:1px solid white; position:relative;
height:30px; line-height:30px;}
.navbar li ul li {float:none; width:150px; text-align:left; padding-left:5px;
border-top:1px solid white;}
.navbar a {text-decoration:none; color:white;}
.navbar li ul {position:absolute; top:30px; left:0; visibility:hidden;}
.navbar li:hover ul {visibility:visible;}
.navbar li:hover {background-color:maroon;}
#maincontent {
clear: both;
padding: 10px;
font-family: "Microsoft Sans Serif";
font-size: medium;
}