CSS Menu (hide UL) - css

I'm new to CSS and I'm trying to make a menu where the sub-list is only visible (and takes up space) on hover-over.
I've found the visibility:collapse; property, but that only masks the sub-list and leaves a big gaping gap in my vertical menu.
Here's what I've got so far, but I'm not sure how to implement the expandable sub-menu on hover over:
CSS:
nav a {
text-decoration: none;
color: white;
font-family: 'Roboto', sans-serif;
}
nav ul {
list-style-type: none;
}
nav ul li ul {
visibility: collapse;
}
HTML:
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Gear
<ul>
<li>P1</li>
<li><a href="p2.html">P2>/a></li>
<li><a href="p3.html">P3/a></li>
<li>P4</li>
<li>P5</li>
</ul>
</li>
<li>News</li>
<li>Contact</li>
</ul>
</nav>
Any help you can offer is much appreciated

You can use Pseduo-selectors to target the sub-list when you hover over the parent list.
Here's the Fiddle
All you need is this:
nav ul {
position:relative;
list-style-type: none;
background-color:#eaeaea;
}
nav ul li ul {
position:absolute;
display:none;
left:60px;
background-color:#CCC;
}
nav ul li:hover ul {
display:block;
}
This targets the sublist when the parent is hovered over, and overrides the display:none; command

HTML CODE:
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Gear
<ul>
<li>P1</li>
<li>P2</li>
<li>P3</li>
<li>P4</li>
<li>P5</li>
</ul>
</li>
<li>News</li>
<li>Contact</li>
</ul>
</nav>
CSS CODE:
nav a {
text-decoration: none;
color: white;
font-family: 'Roboto', sans-serif;
}
nav ul {
list-style-type: none;
}
nav a{
color:#000000;
}
nav ul li >ul {
display: none;
}
nav ul li:hover >ul {
display:block;
}
JSFIDDLE DEMO

Instead of using visibility: collapse; use display:none property because once you use visibility property there a space exist always.
you should try like this.
nav ul li >ul {
display:none;
}
nav ul li:hover >ul {
display:block;
}
A working Demo http://jsbin.com/xivogawu/1/edit

The visibility property only hides an element without changing the layout (i.e. leaving gaps like you've noticed). Also, the collapse value only effects table rows and columns.
The best way to achieve your goal is to use the display property; try this instead:
nav ul li ul {
display: none;
}
nav ul li:hover ul {
display: block;
}

Related

Can only get CSS dropdown to appear on hover

I would like to change my current menu to allow for one dropdown: Products. My test page can be found here This is my HTML:
<nav>
<ul class="menu">
<li class="current">Home</li>
<li>About Us</li>
<li class="subNav"><a class="selected">Products</a>
<ul>
<li>Designer Bags
</li>
<li>Cowhides
</li>
<li>Hand-carved Geese
</li>
<li>Antler Chandeliers
</li>
</ul>
<li>Gallery</li>
<li>Shows</li>
<li>Contact</li>
<li>Shop</li>
</ul>
</nav>
My original CSS:
.nav-buttons{text-align:center;padding-bottom:17px;}
#nav{overflow:hidden;display:inline-block}
#nav li{float:left;overflow:hidden;margin:0 10px}
#nav li a{display:block;background:url(../images/pags.png) no-repeat 0 0;
width:19px;height:19px;
line-height:0;font-size:0;
}
#nav li a:hover,#nav li.showPage a{background-position: 0 bottom}
nav{float:right;padding:12px 0 0 0}
.menu {
font-size:0;
line-height:0;
padding:0;
z-index:99;
position:relative;
margin-right:21px;
}
.menu > li {
position:relative;
float:left;
margin-left:11px;
border-radius:5px;
-moz-border-radius:5px;
-webkit-border-radius:5px;
background:url(../images/point.png)
}
.menu li a{
color:#b3adad;
font-size:18px;
line-height:20px;
display:block;
position:relative;
text-decoration:none !important;
padding:7px 12px 9px;
font-family: 'Noto Sans', sans-serif;
}
.menu li.current,
.menu li:hover {
background:#9c6f51;
}
.menu li.current a,
.menu li:hover a{
color:#fff
}
And this bit of CSS I just added today:
nav a {
font-weight: 800;
padding: 5px 10px;
display: block;
}
nav > ul > li.subNav ul {
display: none;
position: absolute;
top: 100%;
left: 0;
white-space: nowrap;
background: #fff;
}
nav ul li.subNav:hover ul {
display: block;
}
It seems to want to work with the exception that I see no sub-menu unless I hover over it. I've come here for help because I'm afraid to mess with the original CSS and thus ruin my navigation throughout the rest of the site. Is there something I can add that will cause the menu to appear within just the "subNav" class, without affecting the rest of the menu or site navigation? (The original CSS came with this template and I do note that font-size:0 is used a few times. Since the menu worked well before I felt I needed to add a dropdown, I have been reluctant to change that, since I would only be experimenting without understanding.)
I've sorted it. I had to change the background color in the "subNav" class, in order for the site menu's overall font color to show up. It's there; I just couldn't see it.

Why does a percentage width in the top li collapse the children li's?

I can't figure out why If I change the top li in a menu from px's to a percentage the child menu items stop displaying normally and fall over each other.
JS fiddle working:
http://jsfiddle.net/mg6vx/
JSfiddle not working (added width:20%; to #nav li):
http://jsfiddle.net/4eyp5/
HTML:
<div id="nav">
<ul>
<li>About Us
<ul>
<li>Ab</li>
<li>out</li>
<li>Us</li>
</ul>
</li>
<li>Stuff
<ul>
<li>Stuffed</li>
<li>stuffs</li>
<li>Stuffeds</li>
</ul>
</li>
<li>Some Such
<ul>
<li>Doohickey</li>
<li>Widgets</li>
<li>Things that amazon sells for one star</li>
</ul>
</li>
<li>Careers
<ul>
<li>Interal postings</li>
<li>External postings</li>
<li>Theoretical postings</li>
</ul>
</li>
</ul>
</div>
CSS:
#nav * {
margin: 0;
padding: 0;
}
#nav li {
position:relative;
float:left;
list-style:none;
background-color:black;
/* if you add this the child li's fall over themselves width:20%; */
}
#nav li a {
width:100px;
height:30px;
display:block;
text-decoration: none;
text-align:center;
line-height:25px;
color: white;
}
#nav li a:hover {
background-color: #009;
text-decoration: underline;
}
#nav ul ul {
position: absolute;
top: 30px;
visibility: hidden;
}
#nav ul li:hover ul {
visibility:visible;
}
As #fizzix points out, each li has a width of 20% of the parent. The nested li elements therefore have a small width because they are 20% of 20%.. etc.
You should use the child combinator, >, in order to prevent styling from effecting nested li elements.
Something like this would work: (example)
#nav > ul > li {
width:20%;
}
#nav > ul > li > ul,
#nav > ul > li > ul > li,
#nav ul li a {
width:100%;
}
It is because you are giving them a specific width (being 20%), therefore it allows the list items to fit next to each other. In easier terms, you have 100% to work with and you are giving every list item 20%. Hence, you could then fit 5 list items on the same line.
If you can explain a little more on what you are trying to achieve exactly, we can help you find an alternative approach.

CSS unable to get rid of UL's first and last child's dividers

I have a nav that holds a ul with several ils. The problem is that im unable to get rid of the ul's first and last child's divider.
here's a demo: http://jsfiddle.net/Rvs3C/1/
i've added this code to my CSS file but seems like it makes no difference:
nav ul a li:first-child{
content: none;
}
nav ul a li:last-child{
content: none;
}
You markup is wrong. The corret way is <a> tag being the children of tag <li>.
The way you made ​​the correct selector would:
nav ul a:first-child li{
content: none;
}
nav ul a:last-child li{
content: none;
}
But remembering, this is not the right way to do.
The correct is change you html to:
<ul class="showing">
<li>home</li>
...
As mentioned elsewhere your HTML structure is off, li must me a direct child of ul, no other element as a child is valid. With this said, you also need to change your selectors per the below.
Updated Fiddle
HTML
<nav>
<ul class="showing">
<li>home
</li>
<li>about me
</li>
<li>content
</li>
<li>browse
</li>
<li>rate
</li>
<li class="nav_arr"><img src="http://www.irvinewelsh.net/images/arrow-left.png" />
</li>
</ul>
</nav>
CSS
nav {
margin-right: 150px;
}
nav ul {
background-color: #494A4C;
overflow: hidden;
text-align: right;
}
nav li img {
margin-left: 20px;
vertical-align: top;
}
nav ul li a {
color: white;
text-decoration: none;
font-family:arial;
font-size: 14px;
padding: 0px;
font-weight: bold;
}
nav ul li:first-child:before {
content: none;
}
nav ul li:last-child:before {
content: none;
}
nav li:before {
content:"|";
padding: 5px;
font-weight: bold;
color:lightblue;
font-size: 20px;
}
nav ul li {
display: inline-block;
}

CSS nav display

I'm a relative newbie and I'm having trouble following the css for my nav. I need to do 2 things:
1) run my sub menu inline
2) hide my sub-sub nav, and show it inline as well.
Here is the test site: http://gbetza.mydomain.com/webservice2/test/Basso56/test/home.html
Here is the CSS: http://gbetza.mydomain.com/webservice2/test/Basso56/test/css/style%20-%20Copy.css
I'm having trouble understanding which class I need to adjust as well as how.
Thank you
Copy + Paste from a similar answer of mine.
I have created a really simple implementation of this. It is barebones so that you can get a real easy look at the basic concept.
In the fiddle here: http://jsfiddle.net/WS3QQ/2/
HTML - Note how the sub-menus are nested
<div id="nav">
<ul>
<li>Top Menu
<ul>
<li>Sub-Menu</li>
<li>Sub-Menu</li>
<li>Sub-Menu</li>
</ul>
</li>
<li>Top Menu</li>
<li>Top Menu</li>
<li>Top Menu</li>
</ul>
</div>
CSS - note how you can easily target the sub-menus (#nav li li). By default the sub-menu li is hidden (display:none). When the li is hovered over, the sub menu li is shown (display:block).
#nav ul { list-style-type: none; margin: 0; padding: 0; }
#nav li { float: left; }
#nav li li { clear: left; display: none; }
#nav li:hover li { display: block; }
#nav li:hover a { background: #111; }
#nav li a { background: #333; padding: 10px; display: block; color: #FFF; font-weight: bold; text-decoration: none; }
#nav li a:hover { background: #3914AF; }

customising first level and sub level of a menu separatly

I'm using a menu in wordpress, using wp-menu.
it generates my menu automaticaly. I can then customize my menu with css.
I'm trying to customize separatly my first level li, and all the sub level li.
for example, I've would like to apply a padding left of 20 px in my first level, and no padding in the sub level.
In this case, I've added this code to add content after the 3rd element of my first level menu, but it is added alors to all my sub level menu...
ul#menu-menu_top li:nth-child(3):after{content:" | ";color:yellow}
here is my CSS :
.menu-menu_top-container{background-color: #1C2336}
ul#menu-menu_top, ul#menu-menu_top ul.sub-menu {
padding:0;
margin: 0;
}
ul#menu-menu_top li, ul#menu-menu_top ul.sub-menu li {
list-style-type: none;
display: inline-block;
}
ul#menu-menu_top li:nth-child(3):after{content:" | ";color:yellow}
/*Link Appearance*/
ul#menu-menu_top li a {
text-decoration: none;
color: #fff;
padding: 5px;
display:inline-block;
padding-left: 20px;
}
ul#menu-menu_top li {
position: relative;
}
ul#menu-menu_top li ul.sub-menu {
display:none;
position: absolute;
left: 0;
width: 100px;
}
ul#menu-menu_top li ul.sub-menu li a{background-color: white;color:#1C2336;width:300px;}
ul#menu-menu_top li ul.sub-menu li a:hover{background-color: #1C2336;color: white}
ul#menu-menu_top li:hover ul.sub-menu {
display:block;
}
and a jsfiddle with my html :
http://jsfiddle.net/JpqNs/
can anybody help me with this ?
thanks a lot
This can be simply solved by using the > CSS selector. For example, if you had the following code:
<ul class="layer1">
<li>
Link
</li>
<li>
Link
</li>
<li>
<ul class="layer2">
<li>
Link
</li>
<li>
Link
</li>
</ul>
</li>
</ul>
You can use the CSS .layer1>li{YourStylesHere} to only affect the li elements that are direct children of your layer1 <ul>. This will not affect children of children, such as the <li> elements of your layer2 <ul>

Resources