This question already has answers here:
Combining :last-child with :not(.class) selector in CSS
(2 answers)
Closed 6 years ago.
How can I select last li that doesn't have the .hidden class?
I have HTML and CSS like this:
ul li:last-child:not(:first-child):not(.hidden) button {
background-color: red;
}
<ul>
<li>
<button>1</button>
</li>
<li>
<button>2</button>
</li>
<li class="hidden">
<button>3</button>
</li>
</ul>
At the current moment, there is no CSS way of being able to find an element that is then followed by another specific element.
Possibly soon, there will be the CSS Relational Pseudo-class :has() which will make what you want possible. This is currently in the CSS Selectors Level 4 Draft and looks unlikely to be rolled out across any browsers any time soon.
A demo is below but don't expect it to work until the Selectors 4 Draft is at least in Working Draft.
Keep an eye on CanIUse to see when it becomes readily available.
ul li:has(+ .hidden:last-child),
ul li:last-child:not(.hidden) {
background: red;
}
<ul>
<li>
<button>1</button>
</li>
<li>
<button>2</button>
</li>
<li class="hidden">
<button>3</button>
</li>
</ul>
:has() is available in jQuery though, so here's a jQuery alternative
Read more here from the Official jQuery Docs
$('ul li:has(+ .hidden:last-child), ul li:not(.hidden):last-child').css('background', 'red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li>
<button>1</button>
</li>
<li>
<button>2</button>
</li>
<li class="hidden">
<button>3</button>
</li>
</ul>
The CSS :nth and :last-child pseudo selectors can only work with tags and not other selectors like :not or class but if there is always going to be only one .hidden li at the end, then you could use something like this:
li:nth-last-child(2) { background: lightblue; }
<ul>
<li>1</li>
<li>2</li>
<li class="hidden">3 hidden</li>
</ul>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li class="hidden">4 hidden</li>
</ul>
Related
This question already has answers here:
CSS selector for targeting only immediate children and not other identical descendants
(2 answers)
Closed 2 years ago.
I use CSS Modules which generates random class names for my tags. How can I select an element of specific type without selecting descendants? So far I've tried to use selectors :first-of-type and :first-child like this:
.settings ul:first-of-type > i:first-child {
opacity: 0.5;
}
But the result is that my rule applies to every <i> element on my page.
HTML:
<div class="settings">
<ul>
<li>
<i>Select this element</i>
<ul>
<li>
<i>but not this one</i>
<span></span>
</li>
<li>
<i>or not this one</i>
<span></span>
</li>
</ul>
</li>
</ul>
</div>
Use the CSS child combinator >:
.settings > ul > li > i {
opacity: 0.5;
}
<div class="settings">
<ul>
<li>
<i>Select this element</i>
<ul>
<li>
<i>but not this one</i>
<span></span>
</li>
<li>
<i>or not this one</i>
<span></span>
</li>
</ul>
</li>
</ul>
</div>
The child combinator (>) is placed between two CSS selectors. It
matches only those elements matched by the second selector that are
the direct children of elements matched by the first.
This question already has answers here:
What does the ">" (greater-than sign) CSS selector mean?
(8 answers)
Closed 4 years ago.
I have the following link structure for my categories:
<ul>
<li class="current-cat cat-parent">
Parent-Cat
<ul class="children">
<li class="cat-item cat-item-71">
Children-Cat
</li>
</ul>
</li>
</ul>
Now I want only the current-cat class to change the color of it's a element:
#outer-section .outer-div ul .current-cat a {
color: red;
}
The problem is, that the children (cat-item cat-item-71) gets changed, too. How can I prevent that and change only the parent a of the current-cat?
Use the direct child selector: .foo > .bar
The child combinator (>) is placed between two CSS selectors. It matches only those elements matched by the second selector that are the children of elements matched by the first.
Documentation
.current-cat > a {
color: red;
}
<ul>
<li class="current-cat cat-parent">
Parent-Cat
<ul class="children">
<li class="cat-item cat-item-71">
Children-Cat
</li>
</ul>
</li>
</ul>
Note that I didn't use the whole selector (#outer-section .outer-div ...) for the example since you only provided the HTML structure from the ul element.
You can use the > that will prevent the inheritance:
#outer-section .outer-div ul .current-cat > a {
color: red;
}
<ul>
<li class="current-cat cat-parent">
Parent-Cat
<ul class="children">
<li class="cat-item cat-item-71">
Children-Cat
</li>
</ul>
</li>
</ul>
I m having a list with ul and li s.
Now I want to apply a css rule to the parents only and not to the children.
For this I'm using the > symbol but that is applied to the children as well.
The example here
The code I used at the css -
#nav > li a {
padding-bottom: 30px;
}
The html being -
<ul id="nav">
<li>
Home
</li>
<li>
About
<ul>
<li>The product</li>
<li>Meet the team</li>
</ul>
</li>
<li>
Services
<ul>
<li>Sevice one</li>
<li>Sevice two</li>
</ul>
</li>
<li>
Product
<ul>
<li>Small product (one)</li>
<li>Small product (two)</li>
</ul>
</li>
<li>
Contact
<ul>
<li>Out-of-hours</li>
<li>Directions</li>
</ul>
</li>
</ul>
I think you want to use #nav > li > a which covers a children of the <li>. Otherwise any <a> descendant of the <li> is also selected (which is everything).
As of CSS3, there is no way to select an element based on its children. I think that something like that is coming in CSS4, but I'm not sure.
Small note: the > selector selects only the children, not the parents and the children.
In my previous question, I've been using CSS to create auto generated list numbering for <li></li> tags. In another task, I need to create another list that will have title in between of the list as picture shown below.
Above example can be achieve using below code
HTML
<ol class="main">
<span class="title">Title</span>
<li>
Content
</li>
<li>
Content
</li>
<span class="title">Title</span>
<li>
Content
</li>
</ol>
CSS
ul {counter-reset:section}
li {margin:15px 0;text-align:justify}
li:before {counter-increment:section;content:""}
.main {list-style-position:inside;list-style-type:none;padding:0}
.main span {font-weight:700;text-decoration:underline}
.inner {padding:0}
.inner ul {counter-reset:section}
.inner ul > li:before {content:""}
ul {list-style-type:lower-alpha}
However, this code doesn't work in some browser like Opera. This is because in HTML 5, <span></span> tag can't be nested within element <ol></ol>.
jsFiddle that work in Firefox, Chrome and Internet Explorer.
Here's a pure CSS version which works in Chrome 26 and FF 20 (Haven't tested on other browsers). The change from your earlier question is that you don't need to reset your counter every time.
/* Don't reset every time!!! */
/* ol.inner {counter-reset:section;} */
ol.inner li {counter-increment:section;}
ol.inner li:before {content: counters(section,"") ". ";}
You can nest the <ol> inside an unordered list and assign a start position for each:
<ul class="main">
<li>Title Of Section
<ol>
<li>Content 1</li>
</ol>
</li>
<li>Title Of Section
<ol start="2">
<li>Content 1</li>
<li>Content 2</li>
<li>Content 3</li>
</ol>
</li>
</ul>
I put together a quick fiddle which uses jquery to automatically update the atart positions of each ordered list.
Is there a way through pure CSS to change the style of a parent LI on the hover of a child LI?
<ul>
<li>
1
<ul>
<li>10</li>
<li> 20</li>
</ul>
</li>
<li>2</li>
</ul>
So that when I hover over 10 or 20, a style is applied to 1 or 2?
No, there is no parent selector in CSS.
See here for why this is the case:
http://snook.ca/archives/html_and_css/css-parent-selectors
In short: performance reasons.