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.
Related
nav
{
display: table;
width: 50%;
background-color: #333;
padding: 0.50em 0.5em;
margin: 0.60em auto;
}
nav ul
{
display: table-row;
}
nav ul li
{
display: table-cell;
text-align: center;
}
a
{
text-decoration:none;
color: #d9d9d9;
}
a:hover
{
color: #e6e6e6;
}
<nav>
<ul>
<li> art <li>
<li> download </li>
<li> <img src="image/symbol.png">
<li> portfolio </li>
<li> product </li>
</ul>
</nav>
The above code is how I am so far designing my website; however, if I add the image in the middle of the bar, the bar widens to fit the image in it!
The answer has been posted, thanks again!
Try this
.navContain{
height:30px;
width:100%;
margin:0 auto;
background-color:red;
padding-top:30px;
}
nav{
width: 100%;
max-width:400px;
background-color: #333;
padding:0;
margin:0 auto;
overflow:visible;
height:30px;
}
nav ul{
overflow:visible;
height:30px;
margin:0;
padding:0;
}
nav ul li{
text-align: center;
overflow:visible;
height:30px;
margin:0;
padding:0;
list-style-type:none;
float:left;
width:20%;
max-width:80px;
}
a{
text-decoration:none;
margin:0;
padding:0;
color: #d9d9d9;
overflow:visible;
line-height:30px;
display:block;
position:relative;
}
a:hover{
color: #e6e6e6;
}
a img{
width:90%;
margin:0;
padding:0;
vertical-align:baseline;
position:absolute;
bottom:-30px;
right:5%;
left:5%;
}
<div class="navContain">
<nav>
<ul>
<li> art </li>
<li> download </li>
<li> <img src="http://www.chinabuddhismencyclopedia.com/en/images/thumb/b/b8/Nature.jpg/240px-Nature.jpg"></li>
<li> portfolio </li>
<li> product </li>
</ul>
</nav>
</div>
I'm trying to figure out how to get the .sub_menu to display when hovering over the main menu.
I know you hide the .sub_menu but how do you code when hovering over <li> to show sub menu <ul>? You would have to target hover for the first <li> but then change the display: none to however you want the sub menu to look like right?
I can't figure out how to do it.
#navagation{
float:left;
width:70%;
height:100%;border: 1px solid green;
margin:0;
padding:0;
}
#btmenu{
position:relative;
width:100%;
list-style: none;
margin:0;
padding:0;
border: 1px solid blue;
height:62px;text-align:center;
}
ul#btmenu li{
padding:0;
margin:0;
display: inline;
height:62px;
width:200px;
}
#btmenu li > a{ height:62px;
line-height:62px;
margin-left:1%;
margin-right:1%;
border: 1px solid green;
text-align:center;
display:inline-block;
text-decoration:none;
font-weight:bold;
font-size:15px;
color:#000;
padding-left:1%;
padding-right:1%;
text-transform:uppercase;
vertical-align: middle;
}
#btmenu > li > a:hover{
background-color:#000;
color:#FFF;
}
.sub_menu{
display:none;
position:absolute;
width:680px;
background-color:#666;
border: 1px solid red;
list-style: none;
margin:0;
padding:0;
}
#btmenu .sub_menu li{
}
.sub_menu li a{display:inline-block;
background-color:#F00;
height:100px;
width:100px;margin:10px;
text-decoration:none;
color:#FFF;
}
<div id="navagation">
<ul id="btmenu">
<li class="top_quote"><a href="services">services
<ul class="sub_menu">
<li class="auto body repair">1</li>
<li class="rental & loaner">2</li>
<li class="hail damage repair">3</li>
<li class="towing">4</li>
<li class="glass repair">5</li>
</ul></a></li>
<li class="top_quote">locations</li>
<li class="top_quote">partners</li>
<li class="top_quote">our work</li>
<li class="top_quote">contact</li>
</ul>
</div><!--navagation-->
You need to add this line to make the sub menu appears when you hover a li :
#btmenu li:hover ul {display: block}
See it here
You shouldn't have the submenu inside the a tag (this can cause problems with the a tags inside the submenu)
If you put the submenu just after the a tag (as a sibling) then you can act on the li:hover
something like:
#btmenu > li:hover .sub_menu{
display: block;
}
working example with your snippet:
#navagation{
float:left;
width:70%;
height:100%;border: 1px solid green;
margin:0;
padding:0;
}
#btmenu{
position:relative;
width:100%;
list-style: none;
margin:0;
padding:0;
border: 1px solid blue;
height:62px;text-align:center;
}
ul#btmenu li{
padding:0;
margin:0;
display: inline;
height:62px;
width:200px;
}
#btmenu li > a{ height:62px;
line-height:62px;
margin-left:1%;
margin-right:1%;
border: 1px solid green;
text-align:center;
display:inline-block;
text-decoration:none;
font-weight:bold;
font-size:15px;
color:#000;
padding-left:1%;
padding-right:1%;
text-transform:uppercase;
vertical-align: middle;
}
#btmenu > li > a:hover{
background-color:#000;
color:#FFF;
}
.sub_menu{
display:none;
position:absolute;
width:680px;
background-color:#666;
border: 1px solid red;
list-style: none;
margin:0;
padding:0;
}
#btmenu .sub_menu li{
}
.sub_menu li a{display:inline-block;
background-color:#F00;
height:100px;
width:100px;margin:10px;
text-decoration:none;
color:#FFF;
}
#btmenu > li:hover .sub_menu{
display: block;
}
<div id="navagation">
<ul id="btmenu">
<li class="top_quote">
services
<ul class="sub_menu">
<li class="auto body repair">1</li>
<li class="rental & loaner">2</li>
<li class="hail damage repair">3</li>
<li class="towing">4</li>
<li class="glass repair">5</li>
</ul>
</li>
<li class="top_quote">locations</li>
<li class="top_quote">partners</li>
<li class="top_quote">our work</li>
<li class="top_quote">contact</li>
</ul>
</div><!--navagation-->
I am trying to make a vertical menu, but everytime when i show the sub menu on hover, it expands the previous element, making a 'bigger box'. I have no idea how to style that.
Dont want to use some jquery plugin if there is an css solution. I have also bootstrap3, but there is no support for nested dropdowns, dropdowns inside dropdowns ... the nested ones did not open...
JSFiddle link: http://jsfiddle.net/WqW5j/
index.html
<div class="nav">
<ul class="main">
<li>1</li>
<li>2</li>
<li>
3
<ul class="sub">
<li>3-1</li>
<li>3-2
<ul class="sub">
<li>3-2-1</li>
<li>3-2-2
<ul class="sub">
<li>3-2-2-1</li>
<li>3-2-2-2</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
css
.main{
list-style: none;
padding:0px;
margin: 0px;
}
.main li{
background-color:#f1f1f1;
padding: 10px;
margin: 5px;
float:left;
clear:both;
}
.main li:hover{
background-color:#d8d8d8;
}
.main .sub{
display: none;
}
.sub > li > .sub{
display: none;
}
.main > li:hover > .sub:nth-of-type(1){
display: block;
position: relative;
left: 20px;
top:-30px;
list-style: none;
float:left;
width: 100px;
clear: both;
}
.sub > li:hover > .sub{
display: block;
position: relative;
left: 20px;
top:-30px;
list-style: none;
float:left;
width: 100px;
}
To get the nested menu work make all the li items position:relative and make the ul displayed on hover as position:absolute.
Check this fiddle
HTML:
<div class="nav">
<ul class="main">
<li>1
<ul class="sub">
<li>1-1
<ul class="sub">
<li>1-1-1</li>
<li>1-2-1</li>
</ul>
</li>
<li>1-2
<ul class="sub">
<li>1-2-1</li>
<li>1-2-2</li>
</ul>
</li>
</ul>
</li>
<li>2</li>
</ul>
</div>
CSS
.main{
list-style: none;
padding:0px;
margin: 0px;
}
.main li{
background-color:#f1f1f1;
padding: 10px;
margin: 5px;
float:left;
clear:both;
position:relative;
}
.main li:hover{
background-color:#d8d8d8;
}
.main .sub{
display: none;
list-style:none;
padding-left:0;
width:auto;
}
.main .sub li{
float:none;
}
.main > li:hover > .sub{
display:block;
position:absolute;
top:0;
left:100%;
}
.sub li:hover .sub{
display:block;
position:absolute;
top:0;
left:100%;
}
Is it possible to modify the following code to maintain the hover on for this combination of menu+submenu, using only CSS? The submenu should remain visible if either the main or submenu is hovered. Right now only the main menu hover is working. Thanks!
HTML + CSS below:
<div id="dropnav">
<ul>
<li id="main">One</li>
<li id="main">Two
<ul>
<li>Two A</li>
<li>Two B</li>
<li>Two C</li>
<li>Two D</li>
</ul>
</li>
<li id="main">Three</li>
<li id="main">Four</li>
<li id="main">Five</li>
</ul>
</div>
#dropnav {
height:50px;
width: 100%;
text-decoration: none;
margin-left:auto;
margin-right:auto;
margin-top: 10px;
}
#dropnav ul{
margin:0px;
padding:0px;
text-align: center;
}
#dropnav ul li{
display:inline;
float:left;
list-style:none;
margin-left:auto;
position:relative;
height:25px;
width: 20%;
font-size: 20px;
font-family: 'Cabin', Helvetica, Arial;
}
#dropnav ul li ul{
margin:0px;
padding:0px;
display:none;
position:absolute;
left:0px;
z-index: 99;
top:50px;
}
#dropnav ul li:hover > ul{
display:block;
width:500px;
left: -50%;
}
#dropnav ul li ul li:hover > a{
color:#FFFFFF;
text-decoration:none;
display:block;
width:500px;
left: -50%;
}
Try changing your submenu style to:
#dropnav ul li ul {
margin:0px;
padding:0px;
display:none;
position:relative;
left:0px;
z-index: 99;
top:0px;
padding-top: 50px;
}
Demo Fiddle
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/