The :only-of-type<\/code> pseudo-class selector<\/a> in CSS<\/abbr> represents any element that has no siblings of the given type.<\/p>\n
p:only-of-type {\r\n color: red;\r\n}<\/code><\/pre>\nIf no tag precedes the selector, it will match any tag (e.g. :only-of-type<\/code>). If another selector precedes it, it will matched based on the type of tag that selector matches. For example .example:only-of-type<\/code> will behave like p:only-of-type<\/code> if .example<\/code> is applied to a paragraph element.<\/p>\n
Simple Example<\/h3>\nOne list contains only a single list item. Another list contains three list items. We can target the list item that is alone with :only-of-type<\/code>.<\/p>\n
<\/code>
Check out this Pen!<\/a><\/pre>\nAlthough perhaps that isn’t the best example, because :only-child<\/code> would work just as well there since list items are the only possible children of lists. If we use divs instead, we could target a paragraph inside a div if it’s the only paragraph, despite other elements being in there as well.<\/p>\n
<\/code>
Check out this Pen!<\/a><\/pre>\nTo Note<\/h3>\n
As a fun aside, you could achieve the same selection as :only-of-type<\/code> with :first-of-type:last-of-type<\/code> or :nth-of-type(1):nth-last-of-type(1)<\/code>. Those use two chained selectors though, meaning the specificity is double that of :only-of-type<\/code>.<\/p>\n
Related Properties<\/h3>\n
\n- Pseudo Class Selectors<\/a><\/li>\n
- :only-child<\/a><\/li>\n
- :nth-of-type<\/a><\/li>\n
- :nth-last-of-type<\/a><\/li>\n
- :first-of-type:<\/a><\/li>\n
- :last-of-type:<\/a><\/li>\n<\/ul>\n
Other Resources<\/h3>\n\n- W3C CSS2.1 Spec<\/a><\/li>\n
- W3C CSS Level 3 Selectors Spec<\/a><\/li>\n
- MDN Docs<\/a><\/li>\n<\/ul>\n
Browser Support<\/h3>\n\n\n\n\n\n
Chrome<\/span><\/th>\n Safari<\/span><\/th>\n Firefox<\/span><\/th>\n Opera<\/span><\/th>\n IE<\/span><\/th>\n Android<\/span><\/th>\n iOS<\/span><\/th>\n<\/tr>\n<\/thead>\n Any<\/td>\n Any<\/td>\n Any<\/td>\n Any<\/td>\n IE9+<\/td>\n Any<\/td>\n Any<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"The :only-of-type pseudo-class selector in CSS represents any element that has no siblings of the given type. p:only-of-type { color: […]<\/p>\n","protected":false},"author":1036,"featured_media":0,"parent":14240,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"page-almanac-single.php","meta":{"_acf_changed":false,"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"footnotes":"","_share_on_mastodon":"0","_share_on_mastodon_status":"%title% %permalink%"},"tags":[5657],"acf":[],"share_on_mastodon":{"url":"","error":""},"jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages\/14244"}],"collection":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/users\/1036"}],"replies":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/comments?post=14244"}],"version-history":[{"count":10,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages\/14244\/revisions"}],"predecessor-version":[{"id":295422,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages\/14244\/revisions\/295422"}],"up":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/pages\/14240"}],"wp:attachment":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media?parent=14244"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/tags?post=14244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}