styling two level menu (second level) - html

I have two level horizontal menu that works fine.
Second level is not a drop down, it appears on first level menu item click and stays horizontally just under the first level menu.
I need first and second level menu always start from the left side of the container and be full width of the container Currently only first level works like this, but second level doesn't. It starts just under active first level menu item.
You can see it in JSFiddle: http://jsfiddle.net/GrBXa/1/
HTML
<div class="header">
<nav id="site-navigation" class="main-navigation" role="navigation">
<div class="menu-top-menu-container">
<ul id="menu-top-menu" class="main_nav">
<li><a>H1</a>
<ul class="sub-menu">
<li class="menu-item">Prevent</li>
<li>Avoid</li>
<li><a>P2</a></li>
</ul>
</li>
<li class="current-menu-item">Sol
<ul class="sub-menu">
<li><a>Jan</a></li>
<li>Janu2</li>
<li>Janu3</li>
</ul>
</li>
<li>Why
<ul class="sub-menu">
<li>Electri</li>
<li>Envir</li>
</ul>
</li>
<li>Manag</li>
</ul>
</div>
</nav>
</div>
CSS:
ol, ul {
list-style: none;
}
.main-navigation {
width: 100%;
height: 38px;
border-top: 1px solid #4a4a4a;
}
.main-navigation ul {
}
.main-navigation li, .main-navigation li a {
text-decoration: none;
color: black;
}
#menu-top-menu {
height: 38px;
line-height: 38px;
display: inline-block;
}
#menu-top-menu>li>a {
border-bottom: 0;
white-space: nowrap;
text-decoration: none;
}
#menu-top-menu>li>a, #menu-top-menu>li {
display: inline-block;
text-decoration: none;
}
#menu-top-menu>li >a {
padding-left: 40px;
padding-right: 40px;
}
#menu-top-menu>li:hover, .main-navigation ul>li>a:hover {
background-color: #061361;
}
.sub-menu {
display: none;
margin: 0;
padding: 0;
position: absolute;
z-index: 1;
background-color: #061361;
height: 26px;
line-height: 26px;
}
.sub-menu li {
display: inline;
}
.sub-menu li a {
display: inline-block;
padding-left: 14px;
padding-right: 14px;
color: white;
}
#menu-top-menu>li:first-child a {
padding-left: 14px;
}
#menu-top-menu li.current-menu-item ul, #menu-top-menu li.current-menu-parent ul {
display: inline;
}
#menu-top-menu li.current-menu-item a, #menu-top-menu li.current-menu-parent a {
background-color: #061361;
color: white;
}
I believe that this could be done using some relative positioning, but I was not able to achieve this. I have problems with positioning. Please, give me some guidelines.

Add left:0; width:100%; to your .sub-menu rules.
.sub-menu {
display: none;
margin: 0;
padding: 0;
position: absolute;
z-index: 1;
background-color: #061361;
height: 26px;
line-height: 26px;
left:0;
width:100%;
}
jsFiddle example

Hmmm, I'm a little unclear as to what you were looking for - however, I'll try to help out how I can.
I added the following styles to your existing CSS definitions:
#menu-top-menu {
position:relative;
}
.sub-menu {
left:0;
bottom:-26px;
margin-left:40px;
width:100%;
}
You'll note that I did use position:relative, as you suspected would be the case. Here's an updated JSFiddle demonstrating what these additional styles achieve.
If this isn't what you were looking for, feel free to let me know and I'll be happy to help you further. Good luck!

Related

List Collapsing on itself?

I have recently been using tabindex="1" and :focus with divs to make drop down lists in my menu.
But when these drop down lists are clicked they make my links below collapse on themselves, does anyone know why?
Here is the example https://jsfiddle.net/ugjgng5u/4/
When List 1 is clicked all links below shrink and collapse.
<li class=collapse tabindex="1"><a class=red> List 1 </a>
<div class="inside">Content 1....<br>
hi<br>
hi<br>
hi<br>
hi</div></li>
I thought it was to do with clearing the floats after the div? But didn't seem to help.
Thanks!
If I had to guess, it's because the li is inside the menu and you can't detach it. A work around is to make the div absolute.
https://jsfiddle.net/ugjgng5u/7/
HTML
<div id=container>
<div id=top-bar>
<div class=top-links>
<toplinks>
<ul id=menu>
<li><a>A </a></li>
<li class=collapse tabindex="1">
<a class=red> List 1 </a>
<div class="inside">Content 1....
<br> hi
<br> hi
<br> hi
<br> hi
</div>
</li>
<li> <a> C</a></li>
<li><a>B </a></li>
</ul>
</toplinks>
</div>
</div>
</div>
CSS
#container {
background-color: #fff;
max-width: 350px;
z-index: 1;
position: relative;
}
#top-bar {
display: block;
position: relative;
height: auto;
line-height: 1.7;
font-size: 16px;
font: Arial, Helvetica, sans-serif;
}
.top-links li a:hover {
color: #808080;
}
.top-links li ul {
display: none;
}
.top-links li ul li {
display: block;
float: none;
}
.top-links ul li a:hover + .hidden,
.hidden:hover {
display: block;
}
.top-links li > a {
display: block;
font-size: 12px;
font-weight: 600;
height: 44px;
color: #999;
text-decoration: none;
}
li.collapse > a {
cursor: pointer;
display: block;
}
li.collapse:focus {
outline: none;
}
li.collapse > div.inside {
display: none;
}
li.collapse:focus div.inside {
display: block;
}
.inside {
z-index: 10;
position: absolute;
top: 40%;
left: 11%;
background: white;
width: 300px;
padding-left: 20px;
border: 1px solid black;
}
You got some odd choices going on in your JSFiddle.
None-the-less, don't float .indside. Not sure why it's being floated to begin with. When you float and item you take it out of the normal document flow an it no longer takes up space like it did prior to floating. This means the parent element will treat it as if wasn't there.
If you're looking to do a fly-out menu then you should use position: absolute; on the dropdown menu and position: relative; on it's containing element.
Basic fly-out menu below.
ul,
li {
margin: 0;
padding: 0;
list-style: none;
}
ul {
width: 300px;
background-color: #f1f1f1;
}
li {
position: relative;
line-height: 1.5;
}
li:hover {
background-color: #ccc;
cursor: pointer;
}
li:hover > ul {
display: block;
}
li > ul {
display: none;
position: absolute;
top: 0;
left: 100%;
background-color: #eee;
}
<ul>
<li>One</li>
<li>Two
<ul>
<li>Two A</li>
<li>Two B</li>
</ul>
</li>
<li>Three</li>
</ul>

Center Nav Bar when window resized?

My navigation bar is centered, but when the window is smaller, it just goes onto the next line, rather than getting smaller to fit the size of the window, and I don't know how to resolve it. It's got drop down elements on it. I'll also be looking at turning this to a vertical list when viewed on mobile devices, but nowhere near doing media queries yet.
Here's my HTML:
<nav id="page-navigation">
<ul>
<li>Home</li>
<ul class="top-menu">
<li>Photography
<ul class="sub-menu">
<li>BMC Himley Mini Show 2015</li>
<li>Kinver Snow</li>
<li>"Mini Runs" Collection</li>
<li>Hofner Bass</li>
<li>Nature</li>
<li>Haynes Motor Museum</li>
<li>Miscellaneous</li>
<li>Classic Mini</li>
</ul>
</li>
<li>Graphic Design
<ul class="sub-menu">
<li>"Story Bag" Artwork</li>
<li>Business Cards</li>
<li>Logo Design</li>
<li>"The Mexican Job"</li>
<li>Magazine Covers</li>
<li>WPAP Artwork</li>
<li>Lyrics Posters</li>
</ul>
</li>
<li>3D Modelling</li>
</ul>
<li>About</li>
<li>Recognition</li>
</ul>
</nav>
And here's my CSS:
/*navigation*/
#page-navigation
{
width: 60%;
height: 53px;
margin-left: auto;
margin-right: auto;
margin-bottom: 10px;
}
#page-navigation ul li
{
color: white;
list-style: none;
background-color: darkslategray;
width: 9em;
float: left;
}
li
{
position: relative;
}
li.title
{
display: none;
}
li a
{
display: block;
color: white;
line-height: 1.3em;
padding: 1em;
text-align: center;
}
li a:link
{
text-decoration: none;
}
li a:visited
{
text-decoration: none;
color: white;
}
li a:hover, .top-menu > li:hover > a
{
background-color: rgb(48,48,48);
}
li a:active
{
background-color: dimgray;
}
ul.sub-menu
{
width: auto;
height: auto;
position: absolute;
left: -9000em;
overflow: hidden;
}
ul.sub-menu li
{
clear: left;
float: none;
margin-left: -2.5em;
z-index: 1000;
}
.top-menu li:hover ul
{
left: 0;
}
ul.sub-menu li a
{
height: auto;
border-bottom: 1px solid gray;
padding: .4em 1em;
background-color: dimgray;
padding-top: 1em;
padding-bottom: 1em;
}
ul.sub-menu li:last-child a
{
border-bottom: none;
}
ul.sub-menu li a:hover
{
background-color: darkslategray;
}
ul.sub-menu li a:active
{
background-color: gray;
}
Thank you.
Your menu is specified as a variable width of 60%:
#page-navigation
{
width: 60%;
...
}
This will cause the width of the bar to scale with the window, and affect the position of the elements within it. To prevent this, specify a static width, such as:
#page-navigation
{
width: 1000px;
...
}
I just insert a line of code and I think it looks pretty nice right now :)
ul.top-menu{
padding: 0;
}
If you resize the screen there is in front of the navigation (next line) a small space and this resolves the problem.
See the resolution also on jsfiddle.
Answer:
Because of the way your HTML document is structured, it's not possible for you to get the intended effect for the following reason:
You have an unordered list nested directly in another unordered list which is (1) not considered correct (see this discussion); but more importantly, while it looks like your navigation has 6 top level items, you really only have 4. So no matter what CSS you apply to it, it won't work.
Recommendations:
Fix the structure of your HTML document first by using the proper classes only on the top navigation items and properly nest your navigation items.*
I would advise restructuring you information architecture to contain less navigation items on the menu. For example, the recognition would make sense to go in your About page. And if this is a portfolio type website, collapsing your Photography, Graphic Design, and 3D Modeling into Projects would work well. And if you're concerned with the separation, that will happen within the page as a sub-navigation.
If you are set on keeping the navigation structure, it's advisable to either collapse your menu into a select menu or hamburger menu on mobile devices since having a large chunk someone's mobile device screen consumed by your navigation is not a good experience for your user. On top of it, you have to consider that users can't "hover" on mobile devices and the size of those dropdowns would be difficult to navigate at best.
*Solution: Demo
HTML (Fixed):
<nav id="page-navigation">
<ul>
<li>Home</li>
<li class="top-menu">Photography
<ul class="sub-menu">
<li>BMC Himley Mini Show 2015</li>
<li>Kinver Snow</li>
<li>"Mini Runs" Collection</li>
<li>Hofner Bass</li>
<li>Nature</li>
<li>Haynes Motor Museum</li>
<li>Miscellaneous</li>
<li>Classic Mini</li>
</ul>
</li>
<li class="top-menu">Graphic Design
<ul class="sub-menu">
<li>"Story Bag" Artwork</li>
<li>Business Cards</li>
<li>Logo Design</li>
<li>"The Mexican Job"</li>
<li>Magazine Covers</li>
<li>WPAP Artwork</li>
<li>Lyrics Posters</li>
</ul>
</li>
<li>3D Modelling</li>
<li>About</li>
<li>Recognition</li>
</ul>
</nav>
CSS (Fixed and Updated):
/*navigation*/
#page-navigation {
width: 100%;
margin-left: auto;
margin-right: auto;
margin-bottom: 10px;
}
#page-navigation ul {
text-align: center;
}
#page-navigation ul li {
color: white;
list-style: none;
background-color: darkslategray;
width: 9em;
/* float: left removes any possibility of it centering */
display: inline-block;
}
li {
position: relative;
}
li.title {
display: none;
}
li a {
display: block;
color: white;
line-height: 1.3em;
padding: 1em;
text-align: center;
}
li a:link {
text-decoration: none;
}
li a:visited {
text-decoration: none;
color: white;
}
li a:hover,
.top-menu > li:hover > a {
background-color: rgb(48, 48, 48);
}
li a:active {
background-color: dimgray;
}
ul.sub-menu {
width: auto;
height: auto;
position: absolute;
left: -9000em;
overflow: hidden;
}
ul.sub-menu li {
clear: left;
float: none;
margin-left: -2.5em;
z-index: 1000;
}
.top-menu:hover ul {
left: 0;
}
ul.sub-menu li a {
height: auto;
border-bottom: 1px solid gray;
padding: .4em 1em;
background-color: dimgray;
padding-top: 1em;
padding-bottom: 1em;
}
ul.sub-menu li:last-child a {
border-bottom: none;
}
ul.sub-menu li a:hover {
background-color: darkslategray;
}
ul.sub-menu li a:active {
background-color: gray;
}
ul.top-menu {
padding: 0;
}
There are still some minor stylings to adjust, but this should get you what you wanted based on your question.

css navbar hover drop down

I am looking to make a navbar menu that drops down when hovering over a specific navbar li.
My navbar looked and worked fine until I tried to get a hover drop down to work. Specifically this is what I am looking for:hover over "work" and get a drop down menu of "videos" and "photography". I don't think that I am nesting anything wrong, so I figure that it is the CSS that is wrong. I have tried a few different suggestions, but nothing has worked.
Side note: I recently gave the nav items the id of "menu". I had it so that the current page on the nav would be a certain darker color and when the current page nav was hovered it would stay that same color. This worked before I changed to id to "menu" (before it was "nav ul li"). Now when you hover, it changes the color. what made this change happen?
* {
padding: 0;
margin: 0;
}
ul, ol, dl {
padding: 0;
margin: 0;
}
ul#menu {
list-style: none;
text-align: center;
background-color: #bac9a9;
padding-top: 5px;
padding-bottom: 5px;
overflow: hidden;
}
ul#menu:after {
content:"";
background-image: url("../images/navbar-shadow-green.jpg");
height: 8px;
width: 100%;
display: block;
position: absolute;
left: 0;
margin-top: 4px;
}
ul#menu li {
display: inline;
}
ul#menu li a {
text-decoration: none;
color: #f3ffcf;
font-size: 22px;
padding: 10px 25px;
margin: 0 -2px;
}
ul#menu li a:hover {
background-color: #b2c1a2;
}
a.selected-page, a.selected-page:hover {
background-color: #a6b396;
}
li#sub ul {
display: none;
}
ul#menu li#sub:hover ul {
display: block;
}
<nav>
<ul id="menu">
<li>about
</li>
<li id="sub">work
</li>
<ul>
<li>videos
</li>
<li>photography
</li>
</ul>
<li>services
</li>
<li>contact
</li>
</ul>
</nav>
JSFiddle
I think you have got the nesting wrong. You want the list which is revealed when you roll over the work list item to be a child of that list item. Try updating your HTML / CSS as follows (see fiddle):
HTML:
<nav>
<ul id="menu">
<li>about
</li>
<li id="sub">
work
<ul>
<li>videos</li>
<li>photography</li>
</ul>
</li>
<li>services
</li>
<li>contact
</li>
</ul>
</nav>
CSS:
* {
padding: 0;
margin: 0;
}
ul, ol, dl {
padding: 0;
margin: 0;
}
ul#menu {
list-style: none;
text-align: center;
background-color: #bac9a9;
padding-top: 5px;
padding-bottom: 5px;
overflow: hidden;
}
ul#menu:after {
content:"";
background-image: url("../images/navbar-shadow-green.jpg");
height: 8px;
width: 100%;
display: block;
position: absolute;
left: 0;
margin-top: 4px;
}
ul#menu li {
display: inline;
}
ul#menu li a {
text-decoration: none;
color: #f3ffcf;
font-size: 22px;
padding: 10px 25px;
margin: 0 -2px;
}
ul#menu li a:hover {
background-color: #b2c1a2;
}
a.selected-page, a.selected-page:hover {
background-color: #a6b396;
}
li#sub ul {
display: none;
position: absolute;
top: 35px; left: 115px;
background-color: #b2c1a2;
}
li#sub ul li {
display: block;
}
ul#menu li#sub:hover ul {
display: block;
}

Strange Padding on Menu Unordered List - Cannot Remove

I have a very strange padding on my menu. The padding appears both on the top of the menu buttons and below the second level menu buttons.
I have experimented with all sorts of combinations of margins, borders and padding but I just cannot get rid of this extra bit of color!
*JUST A NOTE: people have recommended (and deleted their comment) I delete the CSS:
margin-top: -0.5em;
However, I would like this to stay put if possible.*
Unfortunately, I cannot post a picture as I need more reputation points (am a new to coding and even newer to Stackoverflow) but if anyone can take a look at my code below and see where I have gone wrong that would be great!
My HTML Code:
<ul id="menu" >
<li style="margin-left: 3em;">Home</li>
<li class="sub">
Our Services
<ul>
<li>Solar PV</li>
<li>Air Tightness Testing</li>
<li>Thermal Imaging</li>
<li>Wind Turbines</li>
<li>Energy Consultancy</li>
</ul>
</li>
<li>Recent Projects</li>
<li>About</li>
<li>Contact</li>
</ul>
My CSS code:
#menu {
margin: 0;
padding: 0;
background: #201f5f;
height: 3em;
list-style: none;
font-family:arial;
}
#menu > li {
margin-right: 3em;
margin-top: -0.5em;
background:#201f5f;
vertical-align: bottom;
}
#menu > li > a {
height: 3em;
color: #ffffff;
text-decoration: none;
line-height: 3;
font-weight: bold;
text-transform: uppercase;
}
#menu > li > a:hover {
color: #41A044;
text-decoration: underline;
}
#menu > li.sub {
position: relative;
}
#menu > li.sub ul {
font-size:15px;
margin: 0;
padding: 0;
list-style: none;
background: #000000;
position: absolute;
top: -1000em;
width: 649px;
left:-87px;
}
#menu > li.sub ul li {
display: inline-block;
}
#menu > li.sub ul li a {
height: 100%;
display: inline;
float: left;
padding-left: 0.4em;
padding-right: 0.4em;
padding-top: 0;
padding-bottom: 0;
color: #fff;
font-weight: bold;
text-decoration: none;
}
#menu > li.sub ul li a:hover {
background: #41A044;
text-decoration: underline;
position: relative;
}
#menu > li.sub:hover ul {
top: 2.15em;
}
#menu{
text-align:center;
}
li{
display:inline-block;
}
I thank you in advance for your help!
I removed a margin value you had set DEMO http://jsfiddle.net/gSCr4/4/
margin-top: -0.5em; //Removed

How to give a different color to a selected list item with CSS?

I know this question has been asked so many times before. But I just can't find the right trick for my code. I want a different color for my active list item in the navigation bar. Obviously. Silly little thing. I know. But please try to help.
Here's my HTML code:
<div id="container">
<ul id="nav">
<li class="active">Home</li>
<li>Teaching Assistants</li>
<li>Course Info</li>
<li>Time Table</li>
</ul>
</div>
and Here's the CSS file:
#container {
position: relative;
top: -2em;
z-index: 2;
width: 1200px;
margin: auto auto;
}
#nav {
position: relative;
float: left;
margin-left: 400px;
}
#nav li {
list-style: none;
float: left;
border-right: 1px solid #afc4cc;
}
#nav li a {
position: relative;
z-index: 2;
float: left;
padding: 5px 45px;
font-size: 15px;
font-weight: bold;
font-family: helvetica, arial, sans-serif;
text-decoration: none;
color: #39aea8;
}
ul, li {
margin: 0;
padding: 0;
}
ul#nav li a:link,ul#nav li a:visited {
color: #39aea8;
text-decoration: none;
}
ul#nav li a:hover,ul#nav li a:active {
color: #f4ba51;
text-decoration: none;
}
There's something wrong with your CSS code. Just replace this:
ul#nav li a:hover,ul#nav li a:active{
}
with this:
ul#nav li a:hover,ul#nav li.active a{
// here styling
}
and you are good to go. You just made a mistake while calling the active class in CSS.
ul#nav li.active a { color: #f4ba51 ; }