Selecting Last Child Element with a Specific Class in CSS
In CSS, targeting the last child element of a parent element with a specific class name can be achieved using attribute selectors. Consider the following HTML markup:
<ul> <li class="list">test1</li> <li class="list">test2</li> <li class="list">test3</li> <li>test4</li> </ul>
To select the last child element with the class name "list," you can use the following CSS:
[class~='list']:last-of-type { background: #000; }
Understanding the CSS Selector:
Advantages of Using Attribute Selectors:
Unlike CSS pseudo-classes like :nth-child(), attribute selectors offer the following advantages:
Additional Resources:
The above is the detailed content of How to Select the Last Child Element with a Specific Class in CSS?. For more information, please follow other related articles on the PHP Chinese website!