I have a nested UL structure that represents a folder tree which can grow very deep. I'm stuck at doing a simple :hover effect for the LI elements. The problem is that doing a li:hover won't work as it affects all the parent "li's" aswell. Usually I would have tried to apply the hover effect to a link element or something in the LI, to avoid parents taking the style aswell, but due to circumstances that's not an option now. I have a working solution by using javascript to place a class on the hovered LI and then style this class instead, but i'm really interested in seeing if there's actually a way of accomplishing this through pure css.
I imagine there may be a way of doing a very "hardcoded" css solution but i am more interested in a dynamic and clean one, since the structure can nest indefinitely.
Maybe there's some pseudo selector i'm not aware of? Note that it doesn't have to be IE<8 compatible
<ul>
<li>
This LI should not recieve the hover effect
<ul>
<li>
A li:hover will place the effect on this LI,
but also the parent LI, since that element is
also techincally being hovered.
</li>
</ul>
</li>
</ul>
If you want to use pure CSS then you will need to us parent, child, elements.
For the hover elements:
ul li:hover{
"Style"
}
For the other elements:
ul li ul li{
"Style"
}
UPDATE: I just reread your question, in which you state:
"Usually I would have tried to apply the hover effect to a link
element or something in the LI, to avoid parents taking the style as
well, but due to circumstances that's not an option now."
If that is true, then the solution below is not viable for your circumstance, and you cannot achieve what you desire with pure CSS. I've left my answer, however, as others who want to achieve this but can use a nested element may find it useful.
Pure CSS Only by Adding HTML
The only way you can possibly achieve something of what you seek by pure CSS is to add an extra element (like a span) within the li and perform the hover on that. I assume that whatever folder is being hovered, that folder alone is what you want to highlight. If so, this fiddle illustrates what I am saying, using this code:
HTML
<ul>
<li>
<span>Folder 1</span>
<ul>
<li>
<span>Folder 1.1</span>
<ul>
<li>
<span>Folder 1.1.1</span>
<ul>
<li>
<span>Folder 1.1.1.1</span>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
CSS
li span:hover {
color: red;
background-color: yellow;
}
Now, if you want child folders to also highlight on hover of a parent folder, then perhaps this fiddle illustrates what you want with this code change:
CSS
li span:hover,
li span:hover + ul span {
color: red;
background-color: yellow;
}
They key point is to utilize the extra element to control the hover, whether of the item itself or any later generation elements that the hover should affect.
Not clear at all... but if you want to style nested LI when you are hovered the parent LI without styling the parent one...
Try this:
CSS
ul li ul li {
color: blue
}
ul li:hover ul li {
color: red
}
fiddle example: http://jsfiddle.net/EHp3n/
Your question is not very clear and also it will confuse. Let me explain, when the user hover the city (India / China / UK), style should be applied to State and Country through CSS.
<ul>
<li>India (Apply Style)
<ul>
<li>India State (Apply Style)
<ul>
<li>India City (On Hover)</li>
</ul>
</li>
</ul>
</li>
<li>China
<ul>
<li>China State
<ul>
<li>China City</li>
</ul>
</li>
</ul>
</li>
<li>United Kingdom
<ul>
<li>UK State
<ul>
<li>UK City</li>
</ul>
</li>
</ul>
</li>
</ul>
Related
I am changing a wordpress stylesheet and found this
.banner ul ul
This is all part of .banner ul already, so i.e.
.banner ul {blabla}
.banner ul ul {blabla}
. banner ul ul ul {blabla}
I am wondering what this does, does this only target the second/third ul?
Assuming it wasn't a mistake (which doesn't seem to be), that represents css to be applied to an ul inside another ul, like so:
<div class="banner">
<ul>
<li>
...
</li>
<li>
<ul> <----
<li>...</li>
<li>...</li>
</ul>
</li>
</ul>
</div>
Only what is inside that second ul will be affected by what's inside .banner ul ul
The author of the stylesheet probably intends to provide a selector with greater specificity in order to override other existing styles which might affect a <ul> inside a <ul> (as others have mentioned). It's very likely that your site may have inline <style> blocks or other stylesheets that also attempt to style the same element. Though it may be identical to parent styles, by being more specific with the selector, this style will be more likely to be applied over conflicting styles.
I was wondering if there is a way you can apply a hover effect on parent element with just css.
For example, I have a navigation:
<nav>
<ul>
<li id="firstLiElement" class="main-nav ieraksti">ieraksti!</li>
<ul id="audio_sub_menu">
<li class="mes">mēs 1</li>
<li id="klienti">mēs 2</li>
</ul>
<li class="main-nav" id="audio">paklausies!</li>
<li class="main-nav" id="video">paskaties!</li>
<li class="main-nav" id="kontakti">pasūti!</li>
</ul>
</nav>`
And what I want is to, when hovered on
nav ul ul li
element, it affects the
#firstLiElement
.
I know it can be done with JS, but there has to be also a way to do this with css. I found a lot of solutions when you need to affect the sibling or child, but didn't find anything for this situation.
EDIT:
Sorry, didn't even write what I was looking for... Yes, sub_menus are hidden, but when I hover the main navigation li element, they shove up, and they stay visible also when I hover on them. What I am missing is to change the background color of the main navigation li element when the sub menu is hovered.
I found this for you.
a < img { border: none; }
In this example, it would select a tags but only if they contained an img tag. (Aside: this would be a weird departure from the typical syntax where the actual elements being selected are on the right, this would be on the left).
a img:parent { background: none; }
The key difference being that the :parent syntax would only evaluate a single element, the parentNode available in the DOM for every element. This would be similar to forcing the :has selector to only evaluate children rather than all descendants.
https://css-tricks.com/parent-selectors-in-css/
I believe it can't be done. CSS or Cascade Style Sheet - keyword here is Cascade - executes in a cascade direction and not the other way around.
You can do this (assuming the audio_sub_menu, i.e., is hidden):
nav ul a:hover ul {
visibility: visible;
}
And you'll be targeting a child on its parent hover. So, you can either change your layout and design to use this knowledge, or go with the JS solution :)
edit
to get the same color, style the :hover on the parent as well and change the first </a> in your HTML code
<nav>
<ul>
<a href="#"><li id="firstLiElement" class="main-nav ieraksti">ieraksti!</li>
<ul id="audio_sub_menu">
<li class="mes">mēs 1</li>
<li id="klienti">mēs 2</li>
</ul>
</a>
<li class="main-nav" id="audio">paklausies!</li>
<li class="main-nav" id="video">paskaties!</li>
<li class="main-nav" id="kontakti">pasūti!</li>
</ul>
</nav>
And the CSS
nav ul a:hover {
background-color: your_color;
}
nav ul a:hover ul {
visibility: visible;
background-color: your_color;
}
edit - On the hetasbo's answer
those are suggested css selectors, they don't exist
Considering this markup:
<div id="container">
<div id="mainmenu">
<ul>
<li> <h1>My Dashboard</h1></li><br>
<li> <h1>About</h1></li><br>
<li> <h1>Contact</h1></li><br>
<li> <h1>Setttings</h1></li><br>
<li> <h1>Log Out</h1></li><br>
</ul>
</div>
</div>
Selecting this way is a valid thing? I am having issues with some properties.
#container ul li{
display: inline-block;
}
#mainmenu ul li a{}
#mainmenu ul li a:hover{}
full sample:
https://jsfiddle.net/jhr1q1q4/
I'm somewhat unsure what you're asking...
Strictly speaking, the selectors you provided are valid and would select the <a> elements within your #mainmenu element.
But I would ask: is the ul li part of the selectors necessary? You could rewrite both of them as
#mainmenu a {}
#mainmenu a:hover {}
and they would work the same and require less parsing. If you wanted to only select <a> elements that are descendants of <li> elements, you could keep the li in your selector; however, the ul is not necessary -- it is implied your <li> elements will be children of a <ul> (assuming you're writing valid HTML).
Another note: <a> elements are inline, meaning they're meant to act at the text level. <h1> elements, on the other hand, are block-level elements, and thus do not belong inside <a> elements. In fact, you usually shouldn't have more than one <h1> on a page, let alone be using <h1>'s to mark up menu items. If you want your menu items to be big like headers, use your CSS rules to style them that way.
I am having issues with some properties.
What properties are causing trouble?
In Wordpress i created a menu with sub menus and i want to scroll down so the menus are not going to be so big. so i add
Blockquote
.sub-menu li:hover ul and then overflow-y
Blockquote
But only the first submenu becomes a scrolling on. If i have submenu in a submenu then the first submenu doesn't work..
check what i mean here http://www.intereuropetravel.com
Read your rule from right to left.
.sub-menu li:hover ul
This says "for any ul within an li:hover which is within a .sub-menu - { apply these rules }
I can't tell exactly what you want - but I think your selector is not doing what you intend. - so write out what you want to happen, and think about it from right to left.
The only reason why WordPress is relevant, is that it gives you some default classes for your menu.
<ul class="menu">
<li>List item
<ul class="sub-menu">
<li>Sub menu item
<ul class="sub-menu">
<li>etc
<ul class="sub-menu">
<li>etc</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
So, this is some pretty gnarly nesting and, I'm just going to say - that maybe you should rethink this approach, but otherwise - I suggest you build out more specific selectors for each level and control them independently. In WordPress admin under menu - you can reveal a field that will allow you to give unique classes to each of these levels with which you can target more easily.
in your case, you can probably put
.menu-main-menu-container li:hover ul {} -
which would be --- "any list inside of any li in your container {}"
maybe even:
.menu-main-menu-container li:hover > ul {}
which would be --- "any list directly inside of any li in your container {}"
I have an unordered list (ul) with some list items (li). One of this have another unordered list inside (ul). Now, I want to se to all these li the same stylesheet, except for the li that have another unordered list inside.
<ul>
<li>Element</li>
<li>Another Element</li>
<li>Special Element
<ul>
<li>Child</li>
<li>Child</li>
</ul>
</li>
</ul>
How can I set CSS without set a specific class for the special li?
ul ul li { your-rules: here; }
I recommend giving this a read, it will help you understand the cascade and specificity: http://reference.sitepoint.com/css/inheritancecascade
-- edit --
Sorry, just realised you want to style the li, not the ul li inside.
li:nth-child(3) { your-rules: here; }
NOTE: this will affect your inner lis as well if there are three or more.
Read http://css-tricks.com/how-nth-child-works/ to understand the rule I've suggested you use.
In the current CSS specs, this is not possible. There is no selector for "an element that contains some specific content". It may be possible in CSS4, but as of now, you will have to take special measures to single out the "special" li.
<ul>
<li>Element</li>
<li>Another Element</li>
<li class="specialelement">Special Element
<ul>
<li>Child</li>
<li>Child</li>
</ul>
</li>
</ul>
and give class .specialelement a style of its own.
try with
ul li > li {
some style
}