[CSS Selector] E:last-of-type
Using selector:last-of-type, you can apply styles to the last child element of the same element type. This is commonly called a "pseudo-class." It is equivalent to ':nth-last-of-type(1)'. It does not work in IE8 and earlier.
The difference from ':last-child' is whether other element types are counted. For example, 'p:last-child' only applies if the last child is actually a p element.
Sample Code
style.css
/* make the last p element inside a div red */
div p:last-of-type { color: red;}
/* remove the border from the last li in a ul */
ul li:last-of-type { border-bottom: none;}
/* add extra margin below the last h2 in a section */
section h2:last-of-type { margin-bottom: 32px;}
/* right-align the last th column */
tr th:last-of-type { text-align: right;}
Browser Display Result
div p:last-of-type { color: red;} /* Makes the last p element inside a div red. */
<div> <p>This is a p element.</p> <div>This is a div element.</div> <p>This is a p element.</p> <div>This is a div element.</div> </div>
Note that when combining id, class, or attribute selectors with ':last-of-type', it first identifies the last child of each element type, then checks whether it also matches the specified id, class, or attribute. For example, 'p.hoge:last-of-type' applies only if the last p element has class 'hoge'. It does not mean "the last p element with class 'hoge'".
div p.hoge:last-of-type { color: red;} /* Applies in red if the last p element inside a div has class 'hoge'. */
<div> <p class="hoge">This is the first p with class 'hoge' inside the div.</p> <p class="hoge">This is the last p with class 'hoge' inside the div.</p> </div> <div> <p>This is the first p element inside the div.</p> <p class="hoge">This is a p with class 'hoge' inside the div.</p> <p class="hoge">This is a p with class 'hoge' inside the div.</p> <p>This is the last p element inside the div.</p> </div>
To target the last child of every element type, use '*:last-of-type' or just ':last-of-type'. This applies to the last element of each tag type among siblings. For example, if p and span elements both exist as children, it applies to the last p and the last span separately.
div :last-of-type { color: red;} /* Makes the last child of each element type red. */
<div> <div>This is a div element.</div> <p>This is a p element.</p> <div>This is a div element.</div> <p>This is a p element.</p> </div>
Browser Compatibility
2 and earlier ×
2 and earlier ×
8 ×
7 ×
6 ×
8 and earlier ×
1 and earlier ×
Android Browser
2+ ○※ Version data is based on MDN.
If you find any errors or copyright issues, please contact us.