{"id":14224,"date":"2013-03-12T11:00:48","date_gmt":"2013-03-12T18:00:48","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=14224"},"modified":"2022-06-01T13:53:48","modified_gmt":"2022-06-01T20:53:48","slug":"last-of-type","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/selectors\/l\/last-of-type\/","title":{"rendered":":last-of-type"},"content":{"rendered":"

The :last-of-type<\/code> selector allows you to target the last occurence of an element within its container. It is defined in the CSS Selectors Level 3 spec<\/a> as a \u201cstructural pseudo-class\u201d, meaning it is used to style content based on its relationship with parent and sibling content.<\/p>\n

Suppose we have an article with a title, several paragraphs and an image:<\/p>\n

<article>\n\n<h1>A Title<\/h1>\n\n  \nParagraph 1.\n  \nParagraph 2.\n  \nParagraph 3.\n  <img src=\"...\">\n<\/article><\/code><\/pre>\n

We want to make the last paragraph smaller, to act as a conclusion to the content (like an editor’s note). Instead of giving it a class, we can use :last-of-type<\/code> to select it:<\/p>\n

p:last-of-type {\n  font-size: 0.75em;\n}<\/code><\/pre>\n

Using :last-of-type<\/code> is very similar to :nth-child<\/code> but with one critical difference:<\/b> it is less specific<\/a>. In the example above, if we had used p:nth-last-child(1)<\/code>, nothing would happen because the paragraph is not the last child of its parent (the <\/code><\/p>\n

<\/code><\/p>\n

<\/code>). This reveals the power of :last-of-type<\/code>: it targets a particular type of element in a particular arrangement with relation to similar siblings, not all siblings<\/i>.<\/p>\n

The more complete example below demonstrates the use of :last-of-type<\/code> and a related pseudo-class selector, :first-of-type<\/code><\/a>.<\/p>\n

<\/code>Check out this Pen!<\/a><\/pre>\n

Other Resources<\/h3>\n