Having trouble with some css on my dropdown navigation bar. On the dropdown i would like it to be the same width as the standard navigation links and directly below it rather than slightly to the right as you can see when hovering over 'Page2'. Also why is half of the dropdown navigation link white unless you hover over it.
Here is my Fiddle.
CSS / HTML / Demo
nav {
width: 960px;
background-color: #3673a7;
height: 50px;
line-height: 50px;
margin: auto;
border-radius: 5px;
-webkit-box-shadow: 2px 2px 1px 0px rgba(48, 50, 50, 0.81);
-moz-box-shadow: 2px 2px 1px 0px rgba(48, 50, 50, 0.81);
box-shadow: 2px 2px 1px 0px rgba(48, 50, 50, 0.81);
}
nav ul {
text-align: center;
margin: auto;
}
nav ul li {
width: 65px;
display: inline-block;
padding: 0 30px 0 30px;
border-right: 1px solid #355e7f;
float: left;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul li:last-child {
border-right: none;
}
nav ul li:hover {
background-color: #3b8acc;
}
nav a:link,
a:visited {
color: #ffffff;
text-decoration: none;
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
font-size: 0.9em;
}
<nav>
<ul>
<li>Page 1
</li>
<li>Page 2
<ul class="drop">
<li>Drop1
</li>
<li>Drop2
</li>
<li>Drop3
</li>
</ul>
</li>
<li>Page 3
</li>
<li>Page 4
</li>
<li>Page 5
</li>
<li>Page 6
</li>
<li>Page 7
</li>
</ul>
</nav>
The padding that you have defined on the parent LIs was throwing off the alignment of the children, just add these two rules to your CSS(to cancel out the padding that is being applied to the parents):
nav ul li ul { padding:0; }
nav ul li ul li { padding:0; border:none; }
Updated Fiddle
Related
I am trying to build a simple CSS navigation bar with a drop down menu, I am new to CSS and I am having a hard time to get my drop down menu to work. My main problem from the my drop-down menu is that I cannot get my sub-menu to be the same size as my main menu. One of my sub-menu's work the other one does not work?
---- HTML CODE ---
<nav id = "navigation" role:navigation>
<img src="logo_dark.png" id = "logo" />
<ul id="menu">
<li id="intresting_stuff">
<a href = "#" >
Interesting Stuff
<ul class="sub_menu" id="sub_menu">
<li id="classic">Science</li>
<li id="strategy">Technology</li>
<li id="sports">Comedy</li>
</ul>
</a>
</li>
<li id="games">
<a href = "#" >
Games
<ul class="sub_menu" id="sub_menu">
<li id="classic">Classic Games</li>
<li id="strategy">Strategy Games</li>
<li id="sports">Sports Games</li>
<li id="adventure">Adventure Games</li>
<li id="random">Random Games</li>
</ul>
</a>
</li>
<li id="home">
<a href = "#" >
Homes
<span>
</span>
</a>
</li>
</ul>
</nav>
----- CSS CODE -----
#navigation
{
display:block;
background: linear-gradient(to bottom, rgba(0,81,222,0.9)0,rgba(212,212,212,0.15)100%);
color: white;
width : 100%;
height:100%;
border-radius:5px;
-webkit-box-shadow: 0 8px 6px -6px black;
-moz-box-shadow: 0 8px 6px -6px black;
box-shadow: 0 8px 6px -6px black;
}
#navigation li a
{
display:inline-block; makes the list go from left to right
list-style: none;
padding-top:15px;
padding-bottom:15px;
padding-left:23px;
padding-right:23px;
float: right;
font-family: 'Oswald', sans-serif;
font-style:bold;
font-size: 20px;
color: white;
text-decoration: none;
}
/*This is the stuff for the drop down menu..*/
/*Initialize*/
ul#menu, ul#menu ul.sub_menu {
float:right;
display: flex;// alows the website to flex if necessary
flex-wrap: wrap;
justify-content: flex-start;
padding:0;
margin: 0;
}
ul#menu li, ul#menu ul.sub_menu li {
list-style-type: none;
display: inline-block;
}
/*Link Appearance*/
ul#menu li ul.sub_menu li a {
text-decoration: none;
color: #fff;
background: linear-gradient(tobottom,rgba(201,198,189,.5)0,rgba(212,212,212,0.15)100%);
padding:0px;
display:inline-block;
text-align:center;
font-size:19px;
width: 100%;
}
#intresting_stuff
{
border-left: 1px solid gray;
}
#games
{
border-left: 1px solid gray;
border-right: 1px solid gray;
}
/*Make the parent of sub-menu relative*/
ul#menu li {
position: relative;
float:right;
}
/*sub menu*/
ul#menu li ul.sub_menu {
margin:0;
padding:0;
display:none;
position: absolute;
top: 30px;
left: 0;
}
#classic:hover, #strategy:hover, #sports:hover, #adventure:hover,#random:hover
{
background:linear-gradient(to bottom, rgba(222,152,0,0.8) 0,rgba(212,212,212,0.4)100%);
}
#science:hover, #technology:hover, #comedy:hover
{
background:linear-gradient(to bottom, rgba(222,152,0,0.8) 0,rgba(212,212,212,0.4)100%);
}
#games:hover, #intresting_stuff:hover, #home:hover
{
background:linear-gradient(to bottom, rgba(222,152,0,0.8) 0,rgba(212,212,212,0.4)100%);
}
ul#menu li:hover ul.sub_menu {
display:block;
}
Your code is pretty messy, and you have some random <a> tags in the <ul>. I dropped your code in a jsfiddle and I think I understand what you are trying to do.
A lot of your issue revolves around floating left and right in the same divs, etc...
Take a look HERE for a working solution you can copy and paste. Specifically, take note of the hover elements and use of the visibility: visible; class property.
This is how I want the navigation bar, as in : http://themediaoctopus.com/social-media/nostalgic-approach-advertising
How to change the complete color of <li> when hovered on or selected?
Any idea on how to get those seperators between those buttons?
Selection action doesn't work, why? I'm on a particular page and that button on navigation bar is not highlighted. Why and how can I do it?
Here is my current navigation bar when hovered:
Here is my HTML :
<body>
<nav>
<ul>
<li>HOME</li>
<li>HOW IT WORKS</li>
<li>GET IT</li>
<li>WHAT YOU CAN DO</li>
<li>ABOUT</li>
</ul>
</nav>
</body>
Here is my CSS :
body {
color : #F9F9F9;
}
nav {
background-color: #26AD60;
margin: 10px 10px 0px 10px;
}
nav ul {
margin: 0px;
list-style-type: none;
padding: 15px 0px 15px 0px;
}
nav ul li {
display: inline;
padding: 5px 10px 5px 10px;
}
nav ul li a:link, nav ul li a:visited {
color: #F9F9F9;
border-bottom: none;
font-weight: bold;
text-decoration: none;
}
nav ul li a:active {
background-color: #1C8148;
text-decoration: none;
}
nav ul li:hover {
background-color: #1C8148;
color: #F9F9F9;
}
Add this:
padding: 15px 10px 15px 10px;
To your nav ul li:hover{ CSS
Fiddle: http://jsfiddle.net/39Lzp/
In order to have that item be highlighted based on the page you are on you can add a class to it and style that class accordingly. Then, in each different HTML file, you add that class to the corresponding element. For example, index.html would look like this:
<li class="current">HOME</li>
<li>HOW IT WORKS</li>
But how_it_works.html would look like this:
<li>HOME</li>
<li class="current">HOW IT WORKS</li>
Now, for the borders, all you need to do is use the border property like so:
nav ul li {
border-left: 1px dashed white;
}
nav ul li:first-of-type {
border-left: none;
}
Also, in order for the border to span the entire height of the nav bar, change this:
nav ul li {
display: inline;
padding: 5px 10px 5px 10px;
}
To this:
nav ul li {
display: inline;
padding: 15px 10px 15px 10px;
}
http://jsfiddle.net/LbBEK/
Also, for future reference, you have 3 separate questions here. Next time, break your questions up to be more concise and you'll find yourself getting a much better response here on SO.
Its good if you use a:hover and the properties given to it... which allow user to have clickable area been selected and highlighted.
<nav>
<ul>
<li>Home</li>
<li>About Us</li>
<li>project</li>
</ul>
</nav>
CSS:
nav{
display:block;
background:#26AD60;
}
nav ul{
list-style:none;
margin:0px;
padding:0px;
overflow:hidden;
}
nav ul li{
float:left;
border-right: 1px dashed rgba(255, 255, 255, 0.25);
}
nav ul li:last-child{
border:none;
}
nav ul li a{
transition: all 0.25s linear 0s;
padding: 0px 20px;
line-height: 50px;
outline: medium none;
font-family:arial;
font-size:12px;
color: rgb(255, 255, 255);
text-shadow: none;
text-transform: uppercase;
text-decoration:none;
display:block;
}
nav ul li a:hover{
background: #229b56;
}
Please check this jsfiddle to see the same.
Just change the hover statement background-color
nav ul li:hover {
background-color: blue; // here
color: #F9F9F9;;
}
You may want to change the active statement too
nav ul li a:active {
background-color: red;
text-decoration: none;
}
For the border, you can like something like this :
nav ul li {
border-right: 1px dashed rgba(255,255,255,0.25)
}
nav ul li:last-child {
border: 0; // they dont do this, but I prefer with it. As you want.
}
Demo JSFiddle
Apply this on hover padding: 15px 10px 15px 0px;
See demo
Apply border property border-right: 1px dashed #fff for dashed separation between li.
I'm trying to hide part of my menu. When I call display:none The entire menu disappears. I have id's to separate them so I don't get why this happens. Here's the code:
HTML:
<ul id="menu">
<li>Categories 1
<ul id="cat1">
<li>temp1</li>
<li>temp2</li>
<li>temp3</li>
</ul>
</li>
</ul>
CSS:
#menu {
background-color: #0000FF;
height: 20px;
padding: 15px 0 10px;
margin-bottom: 20px;
font: 12px 'DroidSansBold', Tahoma,sans-serif;
font-weight: bold;
text-transform: uppercase;
letter-spacing: 1px;
box-shadow: 3px 2px 3px #000;
border-radius: 5px;
text-align: center;
}
#menu li{
display: inline-block;
}
#menu li a {
color: #fff;
text-decoration: none;
margin: 0 120px;
}
#cat1 li{
display: block;
padding: 10px;
}
#cat1 li a{
background-color: #0000FF;
padding: 10px;
border-radius: 5px;
}
Somewhat working demo: http://jsfiddle.net/ZfN7t/
When you're dealing with ul inside ul, it's usually easier to style if you give them different classes:
<ul id="menu">
<li class="menu-item">Categories 1
<ul id="cat1">
<li class="cat1-item">temp1</li>
<li class="cat1-item">temp2</li>
<li class="cat1-item">temp3</li>
</ul>
</li>
</ul>
Hide the temp1, temp2, temp3 like this:
.menu-item #cat1{
display:none;
}
To display on hover:
.menu-item:hover #cat1{
display:block;
}
I have originally created my navigation in Chrome in which the outcome fits perfectly to my needs. I have then found out that Mozilla Firefox won't output the same result, the drop-down menus under Member Action and Admin Related will display vertically instead on horizontally as i wanted. However my biggest dissapointment was testing the navigation in Internet Explorer which won't even show the drop-down menus.
I would really appreciate someone checking the below code and your feedback, Thanks.
Solved the problem by changing one of the lines in css;
navigation ul li {float: left; list-style:none; }
HTML
<div id="navigationContainer">
<div id="navigation">
<ul>
<li class="borderleft">Home </li>
<li>Register </li>
<li>Search cars</li>
<li>Display all cars</li>
<li>Member Actions
<ul> <!-- Open drop down menu -->
<li class="bordertop">Login</li>
<li class="floatLeft">Member Area</li>
<li>Reservation</li>
<li>Contact us</li>
<li>Admin Related
<ul>
<li class="bordertop">Insert new car</li>
<li>Delete a car</li>
</ul>
</li>
</ul>
</div>
</div>
</BODY>
</HTML>
CSS
* {padding: 0%; margin 0%; } /* Overwrites the browser stylesheet */
#navigationContainer {background:url(images/navi.png); width:100%;position: relative; white-space:nowrap; word-spacing:0; }
#navigation {width:1200px; height:65px; position: relative; font-family: Arial; margin: 2px auto; font-size: 125%; }
#navigation ul { list-style-type: none; }
#navigation ul li {float: left; position: relative; }
#navigation ul li a { border-right: 2px solid #e9e9e9; padding: 20px;
display: block; margin: 0 auto; text-align: center; color: black; text-decoration: none; }
#navigation ul li a:hover { background: blue; color: white; }
#navigation ul li ul { display: none; }
#navigation ul li:hover ul {display: block; position: absolute; }
#navigation ul li ul li {float:left; position:relative; }
#navigation ul li:hover ul li a { background: #12aeef; color: white; position:relative; margin: 0px auto; border-bottom: 1px solid white; border-right: 1px solid white; width: 119px; }
#navigation ul li:hover ul li a:hover { background: blue;}
.bordertop { border-top: 1px solid white; }
.borderleft { border-left: 2px solid #e9e9e9;}
Try this
http://jsfiddle.net/Vf3AJ/
Example from: http://www.cssnewbie.com/example/css-dropdown-menu/horizontal.html
EDITED
Misread horizontal for vertical. tested in IE10, FF, and Chrome
As a side note: horizontal menus have serious issues depending on the width of the viewers screen.
CSS
nav {
position: absolute;
top: 0;
right: 0;
margin: 0;
padding: 0;
}
nav li {
list-style: none;
float: left;
}
nav li a {
display: block;
padding: 3px 8px;
text-transform: uppercase;
text-decoration: none;
color: #999;
font-weight: bold;
}
nav li a:hover {
background: blue;
color: white;
}
nav li ul {
display: none;
}
nav li:hover ul, nav li.hover ul {
position: absolute;
display: inline;
left: 0;
width: 100%;
margin: 0;
padding: 0;
}
nav li:hover li, nav li.hover li {
float: left;
}
nav li:hover li a, navbar li.hover li a {
color: #000;
}
nav li li a:hover {
color: white;
}
HTML
<div id="navigationContainer">
<nav id="navigation">
<ul>
<li class="borderleft">Home
</li>
<li>Register
</li>
<li>Search cars
</li>
<li>Display all cars
</li>
<li>Member Actions
<ul>
<!-- Open drop down menu -->
<li class="bordertop">Login
</li>
<!-- A bordertop class is given to this listed element in order to style a top border for in in the external CSS file. -->
<li class="floatLeft">Member Area
</li>
<li>Reservation
</li>
</ul>
</li>
<li>Contact us
</li>
<li>Admin Related
<ul>
<li class="bordertop">Insert new car
</li>
<li>Delete a car
</li>
</ul>
</li>
</ul>
</nav>
</div>
If you try that in FireFox:
http://jsfiddle.net/rJUKT/2/
it works fine. The dropdown menu shows at the bottom of its parent like that:
But if you try it with IE7, the dropdown menu shows at the right, like that:
Any idea why it does that?
HTML
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<ul id="menu">
<li>
<span>Menu 1</span>
<ul>
<li>Link 1.1</li>
<li>Link 1.2</li>
<li>Link 1.3</li>
<li>Link 1.4</li>
</ul>
</li>
<li>
<span>Menu 2</span>
<ul>
<li>Link 2.1</li>
<li>Link 2.2</li>
<li>Link 2.3</li>
<li>Link 2.4</li>
</ul>
</li>
</ul>
CSS
#menu { width: 100%; float: right; list-style: none; margin-top: 5px; color: #2A4153; }
#menu li {
float: left;
font-size: 17px;
font-weight: bold;
background-color: #e1f1ff;
border: 1px solid #93b5d4;
margin: 0 1px 0 1px;
padding: 4px 0 0 0;
border-bottom: none;
height: 20px;
}
#menu li a, #menu li span { padding: 4px 7px 2px 7px; cursor: pointer; }
#menu li ul {
clear: both;
position:absolute;
display:none;
padding:0;
list-style:none;
width: 150px;
margin: -1px 0 0 -2px;
}
#menu li ul li {
float: left;
width: 150px;
border: 1px solid #93b5d4;
border-top: none;
font-size: 15px;
font-weight: normal;
background-image: url('16x16/eye.png');
background-position: 4px 4px;
background-repeat: no-repeat;
-moz-border-radius: 0;
}
#menu li ul li a, #menu li ul li span { display:block; padding-left: 25px; }
#menu li ul li:hover { background-color: #e5f6e8; border: 1px solid #93d4a2; border-top: none; }
#menu li:hover { background-color: #e5f6e8; border: 1px solid #93d4a2; border-bottom: none; }
JS
$(function() {
$('#menu li').hover(
function () {
//show its submenu
$('ul', this).slideDown(100);
},
function () {
//hide its submenu
$('ul', this).slideUp(100);
}
);
});
Thanks!
You need to set "top" and "left" properties for dropdown UL
You can check it out this. Some css properties updated in this
http://jsfiddle.net/vkVHC/
If that's the only reason you're using jQuery (for the dropdown effect on hover), you may as well do it using CSS instead (and thus save many kBs being loaded by the site). Also, Alexei's answer is correct.