When working with CSS, it can be crucial to select specific elements based on their position and class names. This question explores how to target the "last child" element with a particular class name.
Problem:
Consider the following HTML structure:
<ul> <li>
The goal is to select the last child that has the class name "list" and apply a specific style to it.
Proposed Solution (that doesn't work):
ul li.list:last-child{background-color:#000;}
Drawback:
The proposed solution is incorrect because CSS does not support the :last-child selector for elements with classes.
The issue can be resolved using an attribute selector:
[class~=list]:last-of-type { background: #000; }
Explanation:
Advantages:
The above is the detailed content of How to Select the Last Child Element with a Specific Class Name in CSS?. For more information, please follow other related articles on the PHP Chinese website!