1

I'm new to html/css and i've been working on this drop down menu: http://cssdeck.com/labs/full/k0aytbkc

And this is what I'm trying to achieve. enter image description here

I'm trying to add a background color on the drop down menu, but for some reason the code won't let me do that and I can't figure out where to add the background color.

Does anyone know what I need to change in the code or where I can add the background color to the drop down menu?

HTML CODE:

<div class="nav">

<ul>
<li><a href="#">MEN'S WEAR</a>
  <ul>
    <li>TOPS
      <ul>

        <li><a href="#">Jackets & Coats</a></li>
        <li><a href="#">Shirts</a></li>
        <li><a href="#">Overshirts</a></li>
        <li><a href="#">T-Shirts</a></li>
        <li><a href="#">Basic T-Shirts</a></li>
        <li><a href="#">Knitwear</a></li>
        <li><a href="#">Sweats</a></li>   
      </ul>
    </li>
    <li>BOTTOMS
      <ul>
        <li><a href="#">Jeans</a></li>
        <li><a href="#">Colour Jeans</a></li>
        <li><a href="#">Pants</a></li>
        <li><a href="#">Shorts</a></li> 
      </ul>
    </li>
    <li>FOOTWEAR
      <ul>
        <li><a href="#">Boots</a></li>
        <li><a href="#">Sandals</a></li>
        <li><a href="#">Shoes</a></li>
        <li><a href="#">Sneakers</a></li> 
      </ul>
    </li>
    <li>ACCESSORIES
      <ul>
        <li><a href="#">Belts</a></li>
        <li><a href="#">Caps</a></li>
        <li><a href="#">Hats</a></li>
        <li><a href="#">Scarves</a></li>
        <li><a href="#">Gloves</a></li>
        <li><a href="#">Sunglasses</a></li>
        <li><a href="#">Watches</a></li>
        <li><a href="#">Jewelry</a></li>
      </ul>
    </li>
    <li>SALE
      <ul>
        <li><a href="#">Jeans</a></li>
        <li><a href="#">Caps</a></li>
        <li><a href="#">Belts</a></li>
        <li><a href="#">Sweats</a></li>
        <li><a href="#">Jewelry</a></li>
        <li><a href="#">Shirts</a></li>
      </ul>
    </li>
  </ul>
</li>
</ul>
</div>

CSS CODE:

 .nav {
 position: relative;
 background-color: #ddd;
 width: 1024px;
 height: 42px;
 margin: 0 auto;
-webkit-font-smoothing:antialiased;

}

.nav a {

font: bold 16px/20px sans-serif;
padding: 11px 20px;
text-align: center;
text-decoration: none;
-webkit-transition: all .25s ease;
   -moz-transition: all .25s ease;
    -ms-transition: all .25s ease;
     -o-transition: all .25s ease;
        transition: all .25s ease;
}

.nav ul {

list-style: none;
left: 10px;
}

.nav ul li {
position: relative;
top: 1px;
right: 70px;
}



.nav li {

float: left;
margin: 0;
padding: 0;
position: relative;
min-width: 15%;

}

.nav li ul {

visibility: hidden;
z-index: 1px;
opacity: 0;
-webkit-transition: all .25s ease;
   -moz-transition: all .25s ease;
    -ms-transition: all .25s ease;
     -o-transition: all .25s ease;
        transition: all .25s ease;  



}

.nav li a:hover {
background: #000;
color: #fff;
}

.nav li li li a:hover {
background: transparent;
color: #cd2323;
text-decoration: underline;


position: relative;
padding-left: 20px;
padding-right: 20px;
}

.nav li:hover ul {
visibility: visible;
opacity: 1;


}

.nav li li {
position: relative;
top: 30px;
margin-left: 15px;
right: 10px;
padding: 2px;
font-weight: bold;
color: 8e8e8e;
}



.nav li a {

top: 10px;
left: 30px;
position: relative;
text-decoration: none;
color: #000;
font-weight: bold;
}

.nav li li ul {
position: relative;
bottom: 10px;

}
.nav li li ul li {
right: 115px;
top: 0px;

float: none;
position: relative;

}

.nav li li ul li a {

text-decoration: none;
color: #595959;
font-size: 13px;
font-weight: normal;

}

.nav li li li ul li {
padding-right: 10px;
}
Adrian Enriquez
  • 7,236
  • 7
  • 39
  • 62
Nohman
  • 121
  • 1
  • 2
  • 16
  • Where do you want it exactly? Under .nav li li you could specify "background-color:black", but as you'll see the links are overlapping over each other because of all of the top and bottom attributes on many of the elements. – user1795832 Feb 25 '14 at 03:49
  • i want a background colour added like this http://bldr.no/3og – Nohman Feb 25 '14 at 04:14

4 Answers4

1

New Answer:

I think you need to reconstruct your html. But for the meantime this is the fix for that issue. add the following styles to your css.

CSS

.nav > ul > li > ul{
overflow:hidden;
background:orange;
left:30px;
top:20px;
height:300px;
width:983px;
}

Screenshot

enter image description here

Adrian Enriquez
  • 7,236
  • 7
  • 39
  • 62
  • i tried, but when i add overflow some of the text disappears – Nohman Feb 25 '14 at 04:19
  • also when i add the background color the enire menu gets filled with the background color – Nohman Feb 25 '14 at 04:25
  • try adding the style to your `.nav > ul > li > ul > li` . I've added an image on my answer, please confirm if this is what you want to achieve. – Adrian Enriquez Feb 25 '14 at 04:29
  • This is what I want to achieve: http://bldr.no/3og so the background color fills the entire drop down menu and not only around the text – Nohman Feb 25 '14 at 04:32
0

I hope this helps you, Change the class of all of your starting list items, as found below, and change the background color of that class

HTML

<li class="yo">SALE
<li class="yo">ACCESSORIES
<li class="yo">FOOTWEAR
<li class="yo">BOTTOMS
<li class="yo">TOPS

CSS

.yo{
background-color:grey;
}

JSFiddle Demo

Cup of Java
  • 1,479
  • 2
  • 17
  • 31
  • i did, but then only the text gets marked up with a background and not the whole drop down menu, do you know what i can do after that? – Nohman Feb 25 '14 at 04:20
0

Do you want the background color to the entire drop down menu? Or do you want the background color to each lost item?

For the entire drop down menu you want to place background:color=COLOR; attribute under Nav Ul because this item is the entire Unordered list or drop down menu. Then for each list item place it under Nav Ul Li, however as listed above you could clean the code by giving each Li the same class name.

ChonBonStudios
  • 203
  • 3
  • 14
  • Yes I want the background color to the entire drop down menu. When i place the background color attribute under nav ul the background color only covers the text and not the entire drop down menu. Do you know what I can do to make it fill the entire drop down menu? – Nohman Feb 25 '14 at 04:21
0

As a general note, it's always a good idea to use appropriately named classes rather than selectors such as this:

.nav li li li a:hover

That same effect could be achieved by giving the anchor tags a class of 'page-link' or something like that and then using a selector like this:

.page-link:hover

Then you're not counting li tags until you get to the right level. I find that it tends to make debugging simpler.

But to your question. As Adrian pointed out, the reason you aren't seeing the background color is because of the floats. When an element is floated, it's parent element does not automatically adopt the height of the floated element inside of it. Here is a very simple example: http://jsfiddle.net/jCEhL/

The i-float element has a height of 100px, but you can see the the i-dont element is completely collapsed. Even though it has a 100px element inside of it, it has no height. I gave it a blue border, just so you could see that it's collapsed. There are a whole bunch of ways to deal with this, as outlined in this post: How do you keep parents of floated elements from collapsing?

Here is a fixed version of my little example, using the 3rd method. I find it the simplest: http://jsfiddle.net/jCEhL/2/

I tweaked your css deck a little bit and I think it's somewhat close to what you're talking about. I used the 3rd method again, where you add a spacer to the end of the floated section that has clear:both on it, which fixes the height issue. I also added a clear: both to the .nav ul selector because it was colliding with the header a little bit. Have a look here: http://cssdeck.com/labs/c4zdkxrh

Once I did those two things and added a little padding to the bottom of the spacer, it looks pretty close to your example. A few more hover styles, and you'll be good to go!

Hope that helps!

Community
  • 1
  • 1
developering
  • 1,325
  • 14
  • 16
  • thanks alot for your help. Made the float attribute much clearer for me to understand. Appreciate it. – Nohman Feb 25 '14 at 15:27