Css last/deepest match element or attribute to apply a rule that overrides everything prior
It appears that this is possible, using the :dir or :lang attribute.
Using the :lang is preferable in 2015 as it is supported by most browsers.
Example:
.container { padding:20px;}
:lang(ar) { direction:rtl;}
:lang(en) { direction:ltr;}
.container:lang(en) { background-color:blue;}
.container:lang(ar) { background-color:red;}
.container .a:lang(en) { background-color:orange;}
.container .a:lang(ar) { background-color:yellow;}
<div id="searchHere"> <div lang=en> <div class="container"> l t r <div class=a> a </div> </div> <div lang=ar> <div class="container"> r t l <div class=a> a </div> </div> <div> <div class="container"> r t l <div class=a> a </div> </div> <div lang=ar> <div class="container"> r t l <div class=a> a </div> </div> </div> <div lang=en> <div class="container"> l t r <div class=a> a </div> <div lang=ar> <div class="container"> r t l <div class=a> a <div lang=en> <div class="container"> l t r <div class=a> a </div> <div> <div> <div lang=en> <div class="container"> l t r <div class=a> a </div> </div> </div> <div lang=ar> <div class="container"> r t l <div class=a> a </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div>
Target the deepest nested ul/ol element with css
As far as I am personally aware, this is not possible as there is currently no parent selector in CSS, but you could use the jQuery selector $('ul:not(:has(ul))');
to target ul
elements without any ul
children, and add a class to them.
Example
How to select the deepest use of a CSS class?
You could use jQuery :not
and :has
selectors like below to select the deepest active li:
$('li.active:not(:has(.active))')
Explanation
jQuery( ":not(selector)" )
: Selects all elements that do not match the given selector.
jQuery( ":has(selector)" )
: Selects elements which contain at least one element that matches the specified selector.
The results show:
$('li.active:not(:has(.active))').css("border", "1px solid red");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><ul> <li class="active"> <span>Item 1</span> <ul> <li class="active"> <span>Item 1.1</span> <ul> <li> <span>Item 1.1.1</span> <ul> <li class="active"> <span>Item 1.1.1.1</span> <ul> <li> <span>Item 1.1.1.1.1</span> </li> <li> <span>Item 1.1.1.1.2</span> </li> </ul> </li> </ul> </li> </ul> </li> <li> <span>Item 1.2</span> <ul> <li> <span>Item 1.2.1</span> <ul> <li> <span>Item 1.2.1.1</span> <ul> <li> <span>Item 1.2.1.1.1</span> </li> <li> <span>Item 1.2.1.1.2</span> </li> </ul> </li> </ul> </li> </ul> </li> </ul> </li> <ul>
How to find deepest element from a HTML tree with a certain class?
And a vanilla version :
function deepest (subMenu, select) {
return [].slice.call (subMenu.querySelectorAll (select)).reduce (
function (deepest, el) {
for (var d = 0, e = el; e !== subMenu; d++, e = e.parentNode);
return d > deepest.d ? {d: d, el: el} : deepest;
}, {d: 0, el: subMenu}).el;
};
call with
deepest (subMenu, '.active');
where the second parameter selects the elements to be considered.
Tested on a modified version of @Rory's fiddle
Can you target a specific element among the results of a css selector independent of it's location? or relation?
I just saw some clarification to the question. Here is a much simpler fiddle to get all spans with "aClass" into a list that will let you target the nTh span. Still using Jquery instead of CSS.
https://jsfiddle.net/h2e0xgwf/6/
$(document).ready(function(){
var nTh = 5; // change this to whichever N you wish
var allSpans = $("div > span.aClass");
$(allSpans[nTh-1]).html($(allSpans[nTh-1]).html() + " : found the " + nTh + "th element").css("background-color", "blue").css("color","white");
});
Can you target a specific element among the results of a css selector independent of it's location? or relation?
I just saw some clarification to the question. Here is a much simpler fiddle to get all spans with "aClass" into a list that will let you target the nTh span. Still using Jquery instead of CSS.
https://jsfiddle.net/h2e0xgwf/6/
$(document).ready(function(){
var nTh = 5; // change this to whichever N you wish
var allSpans = $("div > span.aClass");
$(allSpans[nTh-1]).html($(allSpans[nTh-1]).html() + " : found the " + nTh + "th element").css("background-color", "blue").css("color","white");
});
Selecting the last element among various nested containers
If I understand your question correctly, you want to target the last li
tag in multiple ul
s, where the number of nesting levels in the ul
s is unpredictable.
You want a selector that targets the "last and deepest element" in a containing block where the number of elements preceding it in the block are unknown and irrelevant.
This doesn't appear to be possible with Selectors 2.1 or Selectors 3.
The :last-child
, :last-of-type
and nth-child
pseudo-classes work when the nesting levels are fixed. In a dynamic environment where there are multiple lists of varying nesting levels these selector rules will break.
This will select the last li
in the first level ul
:
div.case > ul > li:last-child
This will select the last li
in the second level ul
:
div.case > ul > li:last-child > ul > li:last-child
This will select the last li
in the third level ul
:
div.case > ul > li:last-child > ul > li:last-child > ul > li:last-child
and so on...
A solution, however, may exist in Selectors 4, which browsers haven't yet implemented:
li:last-child:not(:has(> li))
This rule targets last child li
s that have no descendant li
s, which matches your requirement.
For now, however, if you know the nesting level for each of your ul
containers you can apply a class to each targeted li
.
Thanks @BoltClock for help crafting the Selectors 4 rule (see comments).
Related Topics
How to Compile or Convert SASS/Scss to CSS with Node-Sass (No Ruby)
Display:Inline Resets Height and Width
Make Column Fixed Position in Bootstrap
Does Internet Explorer Support CSS Transitions
CSS Selector Involving Pseudo Class First-Child and Dropcap
How to Control Number of Items Per Row Using Media Queries in Flexbox
Put Background Image Over Text
Animated Cursor Support in Web Applications
Suggestions for Debugging Print Stylesheets
Overlay a Background-Image with an Rgba Background-Color
The Use of /Deep/ and >>> in Angular 2
Zoom Control and Streetview Not Showing on My Google Map
How to Stop Jquery Mobile to Apply Styles to My Specific Form Elements
Select Odd Even Child Excluding the Hidden Child
Expanding the Parent Container with 100% Height to Account for Floated Content