CSS: onHover button to display multiple links - html

I have a button that will be used as a part of a navigation bar.
What I need is when a user hovers over the button, the buttons text becomes hidden, and three different links appear inside the button.
The initial button itself shouldn't have a link attached to it, just the three options that appear after it is hovered over.
Hopefully this makes sense; Here is a fiddle which might make it clearer.
<ul class="ulmenu">
<li>Test</li>
</ul>

At its simplest, I'd suggest the following:
<ul id="nav">
<li>Navigation</li>
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
</ul>
With the following CSS:
/* default display for all 'li' elements:
*/
#nav li {
display: inline-block;
}
/* hides the 'li' elements that follow an 'li' element,
hides the 'li:first-child' when the 'ul' is hovered:
*/
#nav li + li,
#nav:hover li:first-child {
display: none;
}
/* shows the 'li' elements that follow other 'li' elements,
when the 'ul' is hovered:
*/
#nav:hover li + li {
display: inline-block;
}
JS Fiddle demo.
Edited with regard to question left by Rygh2014, in comments below:
Would this work for a vertical menu as well? I take it I would just have to switch out the inline-block?
It's absolutely possible to have a vertical menu, simply switch from display: inline-block to display: list-item:
#nav,
#nav li {
list-style-type: none;
}
#nav li {
display: list-item;
}
#nav li + li,
#nav:hover li:first-child {
display: none;
}
#nav:hover li + li {
display: list-item;
}
JS Fiddle demo.
Note that I've also set the list-style-type property to none, as with any other display property there is no marker (under Chrome, at least), whereas with list-item the glyph is there by default and, if you don't want it you have to explicitly remove it.

Related

hide submenus on nav

I'm trying to create a drop-down menu. I had it working for a minute.
My code is as follows:
<nav id="nav">
<ul>
<li class="subNav">Some Page1
<ul>
<li>Related Page1<li>
<li>Related Page2<li>
</ul>
<li>
</ul>
</nav>
My CSS is as follows:
#nav li.subNav ul{
display: none;
}
#nav li.subNav:hover ul{
display: block;
}
I have three CSS files that relate to this page. One is basically a web-kit for font, and the other two are bowlerplate.css and my custom file customFile.css. The tag <#nav li.subNav:hover ul> show up in customFile.css, and <#nav li.subNav ul> diplays in bout custom and boilerplate when I check computed styles.
There are two things I wish to fix; the submenu lines up horizontally (I need it to go vertical) and the submenu isn't hidden. I had to nest /li tag around the ul, so that took care of one problem (they're now aligned under the parent tag).
I also noticed that the height and width have changed on my parent li. I understand it expanding to accommodate the list items, but the increased height seems a little odd.
Here's my solution to the above problem
#nav li.subNav:hover ul li {
visibility: visible;
width: 171px;
padding: 0;
cursor: pointer;
float: none !important;
display: block !important;
}

How to select inner div in an unordered list

I am trying to create a navigation menu with markup as follows:
<ul id="ul1">
<li>Link 1
**<div>**
<p>SOME STUFF</p>
<ul>
<li>Link 1.1<**div** class="innerdiv"></div></li>
<li>Link 1.2<**div** class="innerdiv"></div></li>
<li>Link 1.3<**div** class="innerdiv"></div></li>
<li>Link 1.4<**div** class="innerdiv"></div></li>
</ul>
</div>
</li>
.
.(WITH MORE <LI>'S OFC :))
.
</ul>
Now, I want to make the outer div visible when I hover on Link 1...This is easily done using CSS:
#navbar ul li div
{
min-width:500px;
min-height:130px;
background-color:#dfdfdf;
position:absolute;
left:0px;
top:32px;
visibility:hidden;
}
#navbar ul li:hover div
{
visibility:visible;
}
Next I want that inner div be visible only when I hover on inner links like link 1.1,1.2..
This is causing problems cause I'm using #(id)-selector taking the #navbar as base ID and this is causing the inner divs to inherit the css from outer div ...THAT IS TO SAY THAT WHEN I HOVER ON OUTER LINKS THE BEHAVIOUR OF THE INNER DIV CHANGE TOO WHICH I DON'T WANT...if u understand what I mean..
I WANT THAT OUTER DIV SHOULD BE VISIBLE WHEN I HOVER ON OUTER LINKS(LINK 1,2,3) AND INNER DIV BE VISIBLE WHEN I HOVER ON INNER LINKS(LINK 1.1,1.2,1.3)
SCREENSHOT OF THE NAVMENU
God this is confusing...
Kindly help in this respect by telling how to specifically select the inner div's or tell a workaround using JS or Jquery....currently I'm using the selectors
#navbar ul li div ul li div , and
#navbar ul li div ul li a:hover div
If your div is initially not displayed:
#navbar div {
display: none;
}
You can make them visible when the container LI is hovered:
#navbar li:hover > div {
display: block;
}
The > operator is used to select direct descendants, so here only the div elements that are immediate child nodes of the li that is currently being hovered will be made visible. For more information: https://developer.mozilla.org/en-US/docs/Web/CSS/Child_selectors
I would just change the visibillity of the list item.
ul li ul li {
....
}
Furthermore i don't see your "navbar" id anywhere? Neither a ..

display all sub-level lists inline with css

if i have a list like this one:
<ul>
<li>
item name
<ul>
<li>
item name
</li>
<li>
item name
<ul>
<li>
item name
</li>
</ul>
</li>
</ul>
</li>
</ul>
And for example i want each "li" to display inline like its all one sentence... I need this for breadcrumbs...
I tried this:
ul li {
display: inline;
float: left;
}
But this wont work because the "ul" or "li" doesn't have a defined width so it goes under.
Any help is welcome,
Thanks!
This one works:
ul{display:inline;}
ul li{display:inline;}
ul li ul li{display:inline;}
The jsfiddle: http://jsfiddle.net/naveed_ahmad/QWshN/
Use ul ul li as the selector if you only want the nested ULs to be styled. You also probably don't need float: left.
Just having text inside of the <li> elements is making the text behave like a block-level element itself (effectively wrapping each set of text in a <p> tag). If you wrap the text in a <span>, for example, and make the span inline as well, you will get the desired result
Also, use ul, li, span as the selector instead of ul li
Edit:
In fact, if you just change your original CSS to:
ul, li {display: inline}
it will work perfectly.
You only want the outer ul to be a block level element, so to make all other elements inline elements, you can use:
/* both the inner ul's and the li's */
ul ul,
li
{
display:inline;
}
or, if you want to float them instead:
ul
{
overflow: hidden; /* make sure the uls wraps around their contents, mainly for outer ´ul´ */
}
ul ul,
li
{
float: left;
}
I think this is sexier:
ul, li {
display: inline-block
}
http://jsfiddle.net/jzVcE/

list-style-type: none when list items are links and show bullets when the list items aren't links? CSS

If I have a couple of bullet lits on a page and some lists contains normal text (no paragraph elements) and some contains links, is it possible to have no bullets on the links but bullets on the text items?
To remove all bullets I do:
#WebPartWPQ1 li ,#WebPartWPQ2 li ,#WebPartWPQ3 li ,
#WebPartWPQ4 li,#WebPartWPQ5 li,#WebPartWPQ6 li,
#WebPartWPQ6 li {margin-left: -25px; list-style-type: none}
But if the item is a link I want to show the bullets. I tried with:
#WebPartWPQ1 li ,#WebPartWPQ2 li ,#WebPartWPQ3 li ,
#WebPartWPQ4 li,#WebPartWPQ5 li,#WebPartWPQ6 li,
#WebPartWPQ6 li {margin-left: -25px;}
#WebPartWPQ1 li a ,#WebPartWPQ2 li a ,#WebPartWPQ3 li a ,
#WebPartWPQ4 li a,#WebPartWPQ5 li a,#WebPartWPQ6 li a,
#WebPartWPQ6 li a {margin-left: -25px; li ast-style-type: none}
Without success, any ideas?
You would have to resort to server-side preprocessing or Javascript. There is no way in CSS to style an element based on what its children elements are, or to change a parent's style from a child element's style declaration. Here's the W3 CSS2 Selector Reference and the CSS3 working draft Selector reference, and neither of them mention parent selectors.
While you can not change the styling of a list element based whatever element(s) it contains, there is a pure CSS workaround that achieves more or less the same result :
Set block as the display property of all of your list elements
Set list-item as the display property of your link elements, along with a valid value for list-style-position and list-style-type.
So instead of making your list items look like a list item when they contain a link, you're just making the links inside your list items behave like a list item.
Your bullet will have the same color as your link, but that's about the only difference between this approach and the approach you had in mind (which is impossible).
EXAMPLE CODE :
#WebPartWPQ1 li {
margin-left: -25px;
display: block;
}
#WebPartWPQ1 li a {
display: list-item;
list-style-position: inside;
list-style-type: disk;
}
<ul id="WebPartWPQ1">
<li>Item 1 (ordinary item)</li>
<li><span>Item 2 (wrapped in a "span" tag)</span></li>
<li>Item 2 (wrapped in an "a" tag)</li>
</ul>
THE FIDDLE :
http://jsfiddle.net/6kt8jhfo/5/

How do I get this CSS text-decoration override to work?

Some days I swear I'm going mad. This is one of those days. I thought my CSS was fairly straight-forward here, but it just doesn't seem to be working. What am I missing?
My CSS looks like this:
ul > li {
text-decoration: none;
}
ul > li.u {
text-decoration: underline;
}
ul > li > ul > li {
text-decoration: none;
}
ul > li > ul > li.u {
text-decoration: underline;
}
And my HTML looks like this:
<ul>
<li>Should not be underlined</li>
<li class="u">Should be underlined
<ul>
<li>Should not be underlined</li>
<li class="u">Should be underlined</li>
</ul>
</li>
</ul>
Yet it comes up like this:
text-decoration does not behave the same as other font/text related styling like font-weight. Applying text-decoration will affect all nested elements as well.
Check this out:
http://www.w3.org/TR/CSS21/text.html#propdef-text-decoration
Excerpt:
Text decorations on inline boxes are
drawn across the entire element, going
across any descendant elements without
paying any attention to their
presence. The 'text-decoration'
property on descendant elements cannot
have any effect on the decoration of
the element
. . . .
Some user agents
have implemented text-decoration by
propagating the decoration to the
descendant elements as opposed to
simply drawing the decoration through
the elements as described above. This
was arguably allowed by the looser
wording in CSS2.
I've got the info from: http://csscreator.com/node/14951
You get rid of text-decoration applied to a parent element in those cases:
Out-of-flow elements, such as floated and absolutely positioned ones
li {
float: left; /* Avoid text-decoration propagation from ul */
clear: both; /* One li per line */
}
ul { overflow: hidden; } /* Clearfix */
ul {
overflow: hidden; /* Clearfix */
}
li {
float: left; /* Avoid text-decoration propagation from ul */
clear: both; /* One li per line */
}
li.u {
text-decoration: underline;
}
<ul>
<li>Should not be underlined</li>
<li class="u">Should be underlined
<ul>
<li>Should not be underlined</li>
<li class="u">Should be underlined</li>
</ul>
</li>
</ul>
Atomic inline-level elements, such as inline blocks and inline tables
But if you use li{display:inline-block}, then you don't have bullets (you lose display:list-item) and the items appear one next to the others.
Then, to have one item per line, you can use
li {
display: inline-block; /* Avoid text-decoration propagation from ul */
width: 100%; /* One li per line */
}
And to add the bullets, you can use ::before pseudo-elements. However, bullets shouldn't be underlined, so you will need to take them out-of-flow or make them atomic inline-level too.
li {
display: inline-block; /* Avoid text-decoration propagation from ul */
width: 100%; /* One li per line */
}
li:before {
content: '• '; /* Insert bullet */
display: inline-block; /* Avoid text-decoration propagation from li */
white-space: pre-wrap; /* Don't collapse the whitespace */
}
li.u {
text-decoration: underline;
}
<ul>
<li>Should not be underlined</li>
<li class="u">Should be underlined
<ul>
<li>Should not be underlined</li>
<li class="u">Should be underlined</li>
</ul>
</li>
</ul>
li {
display: inline-block; /* Avoid text-decoration propagation from ul */
width: 100%; /* One li per line */
}
li:before {
content: '•'; /* Insert bullet */
position: absolute; /* Avoid text-decoration propagation from li */
margin-left: -.75em;
}
li.u {
text-decoration: underline;
}
<ul>
<li>Should not be underlined</li>
<li class="u">Should be underlined
<ul>
<li>Should not be underlined</li>
<li class="u">Should be underlined</li>
</ul>
</li>
</ul>
This behavior is specified in CSS 2.1 and CSS Text Decoration Module Level 3:
Note that text decorations are not propagated to any out-of-flow
descendants, nor to the contents of atomic inline-level descendants
such as inline blocks and inline tables.
o.k.w.'s answer above explains perfectly why you can't do what you are asking without some other changes. No, you're not going mad!
Possible workarounds:
try border-bottom?
wrap the text you want underlined in a span class="u" tag? (to prevent the text-decoration from decorating nested elements)
if you aren't able to change the markup, you could add some scripting to accomplish the same as my previous suggestion.
Best of luck!
The reason you´re seeing what you're seeing is that your rule
ul > li.u
takes preference over:
ul > li > ul > li
as a class is specified and that has more weight than the element selectors together.
Edit: What you could try is:
.u ul {
text-decoration: none;
}
.u {
text-decoration: underline;
}
and play around with that (perhaps you will have to use li.u instead of just .u).
However, depending on the content you might want to wrap the underlined parts in q, em or strong tags and style these tags instead of using a class. That way you would be describing your content as well as styling it.
I ran into a similar issue when using an external theme/CSS, so I couldn't modify it to remove the text-decoration: none;. In my case, I had a child element with a link, but the link wasn't being underlined as expected. I tried using display: inline-block; as others mentioned, but it has no effect.
What worked for me was overriding the text-decoration as you had done, but also including !important to force it to override the parent's text-decoration.
// Defined in an external CSS, so I couldn't modify it.
.footer {
text-decoration: none;
}
// In my CSS file.
.footer a {
text-decoration: underline !important;
}
So for your particular example, I imagine this may do the trick (I did not test it to confirm):
ul > li > ul > li {
text-decoration: none !important;
}
The cleanest approach I’ve found is just to set the underline to the background’s colour.
.u {text-decoration: underline;}