{"id":835,"date":"2024-08-15T10:00:48","date_gmt":"2024-08-15T10:00:48","guid":{"rendered":"http:\/\/www.diveintoaccessibility.com\/?p=835"},"modified":"2024-10-28T16:13:44","modified_gmt":"2024-10-28T16:13:44","slug":"comprehensive-guide-to-css-pseudo-classes-and-their-usage","status":"publish","type":"post","link":"http:\/\/www.diveintoaccessibility.com\/index.php\/2024\/08\/15\/comprehensive-guide-to-css-pseudo-classes-and-their-usage\/","title":{"rendered":"Comprehensive Guide to CSS Pseudo-Classes and Their Usage"},"content":{"rendered":"
Whether you\u2019re new to CSS or have years of experience, you\u2019ve likely encountered pseudo-classes<\/strong>. The most commonly recognized pseudo-class is probably Building on concepts from our previous discussions on margin:auto<\/a> and CSS Floats<\/a>, this post provides a detailed examination of pseudo-classes. We\u2019ll explore what pseudo-classes are<\/strong>, how they function, how they can be categorized, and how they differ from pseudo-elements<\/strong>.<\/p>\n A pseudo-class<\/a> is a keyword added to CSS selectors to define a specific state<\/strong> of an HTML element. You can add a pseudo-class to a CSS selector using the colon syntax<\/strong> A CSS class<\/a> is an attribute applied to HTML elements to enforce the same style rules, such as for top menu items or sidebar widget titles. Essentially, CSS classes group or classify HTML elements<\/strong> that share common traits.<\/p>\n Pseudo-classes are similar to CSS classes because they also apply style rules to elements with shared characteristics<\/strong>. However, while standard classes are user-defined<\/strong> and visible in the source code<\/strong> (e.g., Pseudo-classes and pseudo-elements can be used in CSS selectors but do not exist in the HTML source code. Instead, they are \u201cinserted\u201d by the user agent under certain conditions for use in style sheets. \u2013 W3C<\/a><\/p>\n The purpose of regular CSS classes<\/strong> is to classify or group elements<\/strong>. Developers group elements based on intended styling, creating classes like \u201cmenu-items,\u201d \u201cbuttons,\u201d or \u201cthumbnails\u201d to ensure consistent design across similar elements. These groupings are based on characteristics defined by the developers<\/strong>.<\/p>\n For example, a developer might use a However, HTML elements possess inherent characteristics<\/strong> based on their state, position, nature, and interaction with the page and user. These traits are not typically marked in HTML code<\/strong>, but can be targeted with pseudo-classes<\/strong> in CSS. Examples include:<\/p>\n These are the types of characteristics typically addressed by pseudo-classes. To better understand the difference between classes and pseudo-classes, let\u2019s consider using the class You can style these last-child elements with the following CSS:<\/p>\n But what happens when the last element changes? You\u2019ll need to manually update the This hassle of updating classes can be avoided<\/strong> for certain common characteristics. For example, using a predefined CSS offers a variety of pseudo-classes that allow developers to target elements based on specific characteristics that might otherwise be difficult to access. You can find a comprehensive list of standard pseudo-classes<\/a> on MDN.<\/p>\n Dynamic pseudo-classes are applied to HTML elements dynamically<\/strong>, based on the state they transition into due to user interactions<\/strong>. Some examples include State-based pseudo-classes are applied to elements when they are in a specific static state<\/strong>. Common examples include:<\/p>\n The Structural pseudo-classes target elements based on their position within the document structure<\/strong>. Some of the most commonly used examples include There are also pseudo-classes that don\u2019t fit neatly into other categories, such as:<\/p>\n One of the most challenging aspects of pseudo-classes is understanding the difference between the Both of these are structural pseudo-classes that target specific elements within a parent element<\/strong> (container), but they do so in distinct ways.<\/p>\n Assume n<\/em> is 2. The Let\u2019s take a look at an example to illustrate this difference:<\/p>\n Now, let\u2019s see how this CSS affects the HTML in two different scenarios.<\/p>\n In Case 1, the second element inside a If, however, the parent element contains a second paragraph, the In our example, the In Case 2, we move the unordered list to the third position, placing the second paragraph before it. Now, both the To explore the differences between When discussing pseudo-classes, it\u2019s crucial to understand how they differ from pseudo-elements<\/strong> to avoid confusion.<\/p>\n The key difference is that pseudo-classes are used to select HTML elements that already exist in the document tree<\/strong>, though they may not be explicitly marked, while pseudo-elements<\/a> allow us to style elements that don\u2019t typically exist<\/strong> in the DOM on their own. Examples include There is also a difference in syntax<\/strong>: pseudo-elements are generally written with double colons Additionally, there are differences in how we can target pseudo-classes and pseudo-elements with CSS<\/strong>.<\/p>\n Pseudo-elements must appear after<\/em> the sequence of selectors, while pseudo-classes can be positioned anywhere within the CSS selector sequence.<\/p>\n For instance, you can target the last list item in a OR<\/p>\n The first selector targets the last child inside the Let\u2019s attempt something similar with pseudo-elements.<\/p>\n The CSS above is valid, and the text \u201cred\u201d will appear after<\/em> the However, the following code will not work because we cannot change the position of a pseudo-element<\/strong> within the selector sequence.<\/p>\n Only one pseudo-element can be used per selector, whereas pseudo-classes can be combined<\/strong> as long as the combination makes sense. For example, to target first-child elements that are also read-only, you can combine the pseudo-classes Not all selector syntax that includes a It\u2019s important to note that these are not<\/em> CSS pseudo-classes<\/strong> being targeted by jQuery. Instead, they are known as jQuery selector extensions<\/a>.<\/p>\n jQuery selector extensions allow you to target HTML elements with simpler keywords<\/strong>. Many of these extensions are shorthand for combinations of standard CSS selectors, represented by a single keyword.<\/p>\n The post Comprehensive Guide to CSS Pseudo-Classes and Their Usage<\/a> appeared first on Hongkiat<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":" Whether you\u2019re new to CSS or have years of experience, you\u2019ve likely encountered pseudo-classes. The most commonly recognized pseudo-class is probably :hover, which allows us […]<\/p>\n","protected":false},"author":1,"featured_media":837,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12],"tags":[],"_links":{"self":[{"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/posts\/835"}],"collection":[{"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/comments?post=835"}],"version-history":[{"count":3,"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/posts\/835\/revisions"}],"predecessor-version":[{"id":840,"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/posts\/835\/revisions\/840"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/media\/837"}],"wp:attachment":[{"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/media?parent=835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/categories?post=835"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.diveintoaccessibility.com\/index.php\/wp-json\/wp\/v2\/tags?post=835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}:hover<\/a><\/code>, which allows us to style an element when it\u2019s in the hover state<\/strong>, such as when a mouse pointer hovers over it.<\/p>\n
Understanding Pseudo-Classes<\/h4>\n
:<\/code>, like this:
a:hover { ... }<\/code>.<\/p>\n
<div class=\"myClass\"><\/code>), pseudo-classes are applied by user agents (e.g., web browsers)<\/strong> based on the current state of the HTML element.<\/p>\n
The Role of Pseudo-Classes<\/h4>\n
<div><\/code> as a thumbnail and classify it with a \u201cthumbnail\u201d class.<\/p>\n
\r\n <div class=\"thumbnail\">[...]<\/div>\r\n<\/pre>\n
\n
.last<\/code> to identify the last elements in various parent containers.<\/p>\n
\r\n <ul>\r\n <li>item 1<\/li>\r\n <li>item 2<\/li>\r\n <li>item 3<\/li>\r\n <li class=\"last\">item 4<\/li>\r\n <\/ul>\r\n \r\n <select>\r\n <option>option 1<\/option>\r\n <option>option 2<\/option>\r\n <option>option 3<\/option>\r\n <option class=\"last\">option 4<\/option>\r\n <\/select>\r\n<\/pre>\n
\r\n li.last {\r\n text-transform: uppercase;\r\n }\r\n \r\n option.last {\r\n font-style: italic;\r\n }\r\n<\/pre>\n
.last<\/code> class from the previous last element to the new one.<\/p>\n
:last-child<\/code> pseudo-class is highly beneficial. With this, there\u2019s no need to mark the last element<\/strong> in the HTML code, and you can still style it with the following CSS:<\/p>\n
\r\n li:last-child {\r\n text-transform: uppercase;\r\n }\r\n \r\n option:last-child {\r\n font-style: italic;\r\n }\r\n<\/pre>\n
Main Types of Pseudo-Classes<\/h4>\n
1. Dynamic Pseudo-Classes<\/h5>\n
:hover<\/a><\/code>,
:focus<\/a><\/code>,
:link<\/a><\/code>, and
:visited<\/a><\/code>, all of which are commonly used with the
<a><\/code> anchor tag.<\/p>\n
\r\n a:visited {\r\n color: #8D20AE;\r\n }\r\n \r\n .button:hover,\r\n .button:focus {\r\n font-weight: bold;\r\n }\r\n<\/pre>\n
2. State-Based Pseudo-Classes<\/h5>\n
\n
:checked<\/a><\/code> for checkboxes (
<input type=\"checkbox\"><\/code>)<\/li>\n
:fullscreen<\/a><\/code> to target any element currently displayed in full-screen mode<\/li>\n
:disabled<\/a><\/code> for elements that can be disabled, such as
<input><\/code>,
<select><\/code>, and
<button><\/code>.<\/li>\n<\/ul>\n
:checked<\/code> pseudo-class is particularly popular, as it indicates whether a checkbox is selected.<\/p>\n
\r\n .checkbox:checked + label {\r\n font-style: italic;\r\n }\r\n \r\n input:disabled {\r\n background-color: #EEEEEE;\r\n }\r\n<\/pre>\n
3. Structural Pseudo-Classes<\/h5>\n
:first-child<\/a><\/code>,
:last-child<\/a><\/code>, and
:nth-child(n)<\/a><\/code>. These can be used to style specific child elements based on their position within a container. Another example is
:root<\/a><\/code>, which targets the highest-level parent element in the DOM.<\/p>\n
4. Miscellaneous Pseudo-Classes<\/h5>\n
\n
:not(x<\/i>)<\/a><\/code>, which selects elements that don\u2019t match the specified selector x<\/i><\/li>\n
:lang(language-code<\/i>)<\/a><\/code> for targeting elements based on the language of their content<\/li>\n
:dir(directionality<\/i>)<\/a><\/code>, which selects elements with content in a specific directionality (e.g., left-to-right or right-to-left).<\/li>\n<\/ul>\n
\r\n p:lang(ko) {\r\n background-color: #FFFF00;\r\n }\r\n \r\n :root {\r\n background-color: #FAEBD7;\r\n }\r\n<\/pre>\n
nth-child<\/em> vs nth-of-type<\/em> Pseudo-Classes<\/h4>\n
:nth-child<\/a><\/code> and
:nth-of-type<\/a><\/code> pseudo-classes.<\/p>\n
:nth-child(n)<\/code> selector targets an element that is the second child of its parent element<\/strong>, regardless of the type of element. On the other hand,
:nth-of-type(n)<\/code> targets the second occurrence of a specific type of element<\/strong> (e.g., a paragraph) within the parent element.<\/p>\n
\r\n \/* Styles the second child element inside its parent, which can be of any type *\/\r\n p:nth-child(2) {\r\n color: #1E90FF; \/* Light blue *\/\r\n }\r\n \r\n \/* Styles the second paragraph inside its parent element *\/\r\n p:nth-of-type(2) {\r\n font-weight: bold;\r\n }\r\n<\/pre>\n
Case 1<\/h5>\n
<div><\/code> is an unordered list, so the
:nth-child(2)<\/code> rule does not apply to the paragraphs. Although the unordered list is the second child, it is not<\/em> a paragraph.<\/p>\n
:nth-of-type(2)<\/code> rule will apply, since this rule specifically targets
<p><\/code> elements and ignores other types of elements (like unordered lists) within the parent element.<\/p>\n
:nth-of-type(2)<\/code> rule will style the second paragraph, which is Child 3 in this case.<\/p>\n
\r\n <!-- Case 1 -->\r\n <div>\r\n <p>Paragraph 1, Child 1<\/p>\r\n <ul>Unordered List 1, Child 2<\/ul>\r\n <p>Paragraph 2, Child 3<\/p>\r\n <\/div>\r\n<\/pre>\n
<\/figure>\n
Case 2<\/h5>\n
:nth-child(2)<\/code> and
:nth-of-type(2)<\/code> rules will apply, as the second paragraph is both the second child of its parent
<div><\/code> and the second
<p><\/code> element.<\/p>\n
\r\n <!-- Case 2 -->\r\n <div>\r\n <p>Paragraph 1, Child 1<\/p>\r\n <p>Paragraph 2, Child 2<\/p>\r\n <ul>Unordered List 1, Child 3<\/ul>\r\n <\/div>\r\n<\/pre>\n
<\/figure>\n
:nth-child<\/code> and
:nth-of-type<\/code> further, CSS Tricks has a great post<\/a> on the topic. If you use SASS, Family.scss<\/a> can help you create complex nth-child<\/em> based elements.<\/p>\n
Pseudo-Classes vs Pseudo-Elements<\/h4>\n
Pseudo-elements<\/a><\/code>, like
::before<\/code> and
::after<\/code> (see this tutorial on how to use them<\/a>), are also added by user agents<\/strong> and can be targeted and styled with CSS<\/strong> in a manner similar to pseudo-classes.<\/p>\n
::before<\/a><\/code> and
::after<\/a><\/code>, or parts of existing elements like
::first-letter<\/a><\/code> and
::placeholder<\/a><\/code>.<\/p>\n
::<\/code>, while pseudo-classes use a single colon
:<\/code>. This can be confusing because, in CSS2, pseudo-elements were also marked with a single colon\u2014browsers still support this older syntax.<\/p>\n
1. Their Place in the CSS Selector Sequence<\/h5>\n
<ul><\/code> element in two different ways:<\/p>\n
\r\n<ul>\r\n <li class=\"red\"><\/li>\r\n <li><\/li>\r\n <li class=\"red\"><\/li>\r\n <li class=\"red\"><\/li>\r\n<\/ul> \r\n<\/pre>\n
\r\nul > :last-child.red {\r\n color: #B0171F;\r\n}\r\n<\/pre>\n
\r\nul > .red:last-child {\r\n color: #B0171F;\r\n}\r\n<\/pre>\n
<ul><\/code> element that has the class
.red<\/code>, while the second selector targets the last child among the elements that possess the
.red<\/code> class inside
<ul><\/code>. As you can see, the position of the pseudo-class can vary<\/strong>.<\/p>\n
\r\nul > .red::after {\r\n display: block;\r\n content: 'red';\r\n color: #B0171F;\r\n}\r\n<\/pre>\n
<li><\/code> items with the class
.red<\/code>.<\/p>\n
\r\nul > ::after.red {\r\n display: block;\r\n content: 'red';\r\n color: #B0171F;\r\n}\r\n<\/pre>\n
2. Number of Occurrences in a Selector Sequence<\/h5>\n
:first-child<\/a><\/code> and
:read-only<\/a><\/code> as follows:<\/p>\n
\r\n:first-child:read-only {\r\n color: #EEEEEE;\r\n}\r\n<\/pre>\n
jQuery Selector Extensions<\/h4>\n
:<\/code> is a proper CSS pseudo-class. If you\u2019ve used jQuery, you might be familiar with selectors like
$(':checkbox')<\/code>,
$(':input')<\/code>, and
$(':selected')<\/code>.<\/p>\n
\r\n\/* Change the font of all input-related HTML elements,\r\n like button, select, and input *\/\r\n \r\n$( \":input\" ).css(\"font-family\",\"courier new\");\r\n<\/pre>\n