How can I select the last and deepest element in CSS?
Is there a way to improve this css code?
What solution do you proposed for a deep tree? (~15-25).
I'm avoiding using javascript. But SASS solutions are welcome.(maybe using @for?)
/*level 1*/
div.case > ul:last-child > li.leaf:last-child > div {
font-weight: bold;
background: red;
}
/*level 2*/
div.case > ul:last-child > li.expanded:last-child > ul > li.leaf:last-child {
font-weight: bold;
background: blue;
}
/*level 3*/
div.case > ul:last-child > li.expanded:last-child > ul > li.expanded:last-child > ul > li.leaf:last-child {
font-weight: bold;
background: green;
}
/*level 4*/
div.case > ul:last-child > li.expanded:last-child > ul > li.expanded:last-child > ul > li.expanded:last-child > ul > li.leaf:last-child {
font-weight: bold;
background: yellow;
}
<div class="case">
<h1>Case 0</h1>
<ul>
<li class="expanded">
<div>1. content</div>
<ul>
<li class="leaf">
<div>1. content</div>
</li>
<li class="leaf">
<div>2. content</div>
</li>
</ul>
</li>
<li class="expanded">
<div>2. content</div>
<ul>
<li class="leaf">
<div>1. content</div>
<ul>
<li class="leaf">
<div>1. content</div>
</li>
<li class="leaf">
<div>2. content</div>
</li>
</ul>
</li>
</ul>
</li>
<li class="leaf">
<div>3. content (bold)</div>
</li>
</ul>
</div>
<div class="case">
<h1>Case 1</h1>
<ul>
<li class="expanded">
<div>1. content</div>
<ul>
<li class="leaf">
<div>1. content</div>
</li>
<li class="leaf">
<div>2. content</div>
</li>
</ul>
</li>
<li class="expanded">
<div>2. content</div>
<ul>
<li class="leaf">
<div>1. content</div>
<ul>
<li class="leaf">
<div>1. content</div>
</li>
<li class="leaf">
<div>2. content</div>
</li>
</ul>
</li>
</ul>
</li>
<li class="expanded">
<div>3. content</div>
<ul>
<li class="leaf">
<div>1. content</div>
</li>
<li class="leaf">
<div>2. content</div>
</li>
<li class="leaf">
<div>3. content (bold)</div>
</li>
</ul>
</li>
</ul>
</div>
<div class="case">
<h1>Case 2</h1>
<ul>
<li class="expanded">
<div>1. content</div>
<ul>
<li class="leaf">
<div>1. content</div>
</li>
<li class="leaf">
<div>2. content</div>
</li>
</ul>
</li>
<li class="leaf">
<div>2. content</div>
<ul>
<li class="leaf">
<div>1. content</div>
</li>
<li class="leaf">
<div>2. content</div>
</li>
<li class="leaf">
<div>3. content</div>
</li>
</ul>
</li>
<li class="expanded">
<div>3. content</div>
<ul>
<li class="expanded">
<div>1. content a</div>
<ul>
<li class="leaf">
<div>1. content</div>
</li>
<li class="leaf">
<div>2. content (bold)</div>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div class="case">
<h1>Case 3</h1>
<ul>
<li class="expanded">
<div>1. content</div>
<ul>
<li class="leaf">
<div>1. content</div>
</li>
<li class="leaf">
<div>2. content</div>
</li>
</ul>
</li>
<li class="expanded">
<div>2. content</div>
<ul>
<li class="leaf">
<div>1. content</div>
<ul>
<li class="leaf">
<div>1. content</div>
</li>
<li class="leaf">
<div>2. content</div>
</li>
</ul>
</li>
</ul>
</li>
<li class="expanded">
<div>3. content</div>
<ul>
<li class="expanded">
<div>2. content</div>
<ul>
<li class="expanded">
<div>1. content</div>
<ul>
<li class="leaf">
<div>1. content</div>
</li>
<li class="leaf">
<div>2. content (bold)</div>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div class="case">
<h1>Case 4</h1>
<ul>
<li class="expanded">
<div>1. content</div>
<ul>
<li class="leaf">
<div>1. content</div>
</li>
<li class="leaf">
<div>2. content</div>
</li>
</ul>
</li>
<li class="expanded">
<div>2. content</div>
<ul>
<li class="expanded">
<div>2. content</div>
<ul>
<li class="expanded">
<div>1. content</div>
<ul>
<li class="leaf">
<div>1. content</div>
</li>
<li class="leaf">
<div>2. content</div>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li class="expanded">
<div>3. content</div>
<ul>
<li class="expanded">
<div>1. content</div>
<ul>
<li class="leaf">
<div>1. content</div>
</li>
<li class="leaf">
<div>2. content(bold)</div>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div class="case">
<h1>Case 5</h1>
<ul>
<li class="leaf">
<div>content 1</div>
</li>
<li class="leaf">
<div>content 2</div>
</li>
<li class="leaf">
<div>content 3(bold)</div>
</li>
</ul>
</div>
See Question&Answers more detail:os