CSS Pseudo-classes

A CSS pseudo-class selector matches components based on a criteria that isn't always provided by the document tree.


What is Pseudo-class

The CSS pseudo-classes provide the ability to style various dynamic states of an element, such as hover, active, and focus states. They also allow targeting elements in the document tree that can't be selected using other selectors without adding IDs or classes to them. For instance, you can target the first or last child elements.

A pseudo-class is denoted by a colon (:), and its syntax can be defined as follows:

selector:pseudo-class { property: value; }

Anchor Pseudo-classes

By utilizing anchor pseudo-classes, you can customize the appearance of html links in different ways. These pseudo-classes enable you to style unvisited links differently from visited ones. A common technique is to remove underlines from visited links.

<style> 
a:link {
    color: purple;
}
a:visited {
    text-decoration: none;
}
</style>

Certain anchor pseudo-classes are dynamic and are applied based on user interactions with the document, such as hover or focus. These pseudo-classes alter how links are rendered in response to user actions.

  • :hover applies when a user hovers over an element without selecting it.
  • :active applies when the element is activated or clicked.
  • :focus applies when the element has keyboard focus.
<style> 
a:hover {
    color: blue;
}
a:active {
    color: orange;
}
a:focus {
    color: green;
}
</style>

Note: To ensure the proper functioning of these pseudo-classes, it's important to define them in the correct order: :link, :visited, :hover, :active, :focus.


The :first-child Pseudo-class

The :first-child pseudo-class targets an element that is the first child of another element. For example, the selector ol li:first-child selects the first list item of an ordered list and removes its top border.

<style>
ol li:first-child {
    border-top: none;
}
</style>

Note: To make :first-child work in Internet Explorer 8 and earlier versions, a <!DOCTYPE> declaration must be included at the beginning of the document.


The :last-child Pseudo-class

The :last-child pseudo-class matches an element that is the last child of another element. In the example below, the selector ul li:last-child selects the last list item of an unordered list and removes its right border.

<style>
ul li:last-child {
    border-right: none;
}
</style>

The :nth-child Pseudo-class

CSS3 introduces the :nth-child pseudo-class, which allows targeting specific children of a parent element. The basic syntax of this selector is :nth-child(N), where N can be a number, a keyword (even or odd), or an expression in the form of xn+y, wherex and y are integers (e.g., 1n, 2n, 2n+1, ...).

<style>
table tr:nth-child(2n) td{
    background: yellow;
}
</style>

The example above demonstrates using :nth-child to highlight alternate table rows without adding any IDs or classes to the <table>elements.

Tip: The :nth-child(N) selector is particularly useful when selecting elements that appear at specific intervals or patterns within the document tree, such as at even or odd positions.


The :lang Pseudo-class

The :lang pseudo-class allows constructing selectors based on the language setting of specific tags. In the example below, the :lang pseudo-class defines quotation marks for <q> elements that have an explicit language value of no.

<style>
q:lang(no) {
    quotes:"~" "~";
}
</style>

Pseudo-classes and CSS Classes

Pseudo-classes can be combined with CSS classes. In the example below, a link with the class red will be displayed in the color red.


<style>
    a.red:link {
		color:red;
	}
</style>

FAQ

What is a CSS pseudo-class, and how is it used in styling web elements?

A CSS pseudo-class is a keyword added to a selector, allowing you to apply styles to elements based on specific conditions or states. These conditions can include user interactions, element attributes, or other factors that go beyond simple selectors. Pseudo-classes enable the creation of dynamic and interactive styles for web elements.

Could you provide an example of a CSS pseudo-class used to select the first child element of a parent element?

The :first-child pseudo-class is used to select the first child element of a parent element. Here's an example:

ul li:first-child {
    font-weight: bold;
}

In this CSS rule, the first <li> element within a <ul> will have its text displayed in bold.

How does the :hover pseudo-class work in CSS, and what are some common use cases for it?

The :hover pseudo-class is employed to select an element when a user hovers their mouse pointer over it. It's commonly used for creating interactive and responsive user interfaces. Here's an example:

button:hover {
    background-color: #3498db;
    color: #fff;
}

In this example, when a user hovers their mouse over a button element, the background color changes to a shade of blue, and the text color changes to white.

What is the purpose of the :nth-child() pseudo-class in CSS, and how can it be used to target specific elements within a container or list?

The :nth-child() pseudo-class allows you to select elements based on their position within a parent container. It takes an argument in the form of an equation to determine which elements to select. For instance:

ul li:nth-child(odd) {
    background-color: #f2f2f2;
}

In this CSS rule, all odd-numbered elements within a <ul> will have a background color of light gray (#f2f2f2).

What is the :not() pseudo-class in CSS, and how can it be used to exclude specific elements from selection?

The :not() pseudo-class allows you to exclude specific elements from being selected by a CSS rule. It is particularly useful when you want to style elements except for a certain condition. Here's an example:

p:not(.special) {
    color: #333;
}

In this CSS rule, all <p> elements except those with the class "special" will have a text color of #333.

How does the :focus pseudo-class work in CSS, and when is it commonly used?

The :focus pseudo-class is used to select an element when it gains focus, typically through keyboard navigation or user interaction (such as clicking or tapping). It is commonly used to style form input elements, making it clear which element is currently active for user interaction.

input:focus {
    border-color: #007bff;
}

In this example, when an input field gains focus, its border color changes to blue (#007bff).

What is the :first-of-type pseudo-class, and how can it be used to select the first element of a specific type within a container?

The :first-of-type pseudo-class selects the first element of a specific type within a parent container. It is useful when you want to style the first occurrence of a particular element type. For example:

div p:first-of-type {
    font-weight: bold;
}

In this CSS rule, the first <p> element within a <div> will have its text displayed in bold.

What is the purpose of the :checked pseudo-class, and when is it commonly used in CSS?

The :checked pseudo-class selects input elements (such as checkboxes and radio buttons) that are checked by the user. It is commonly used to style the checked state of form elements and create custom styles for checkboxes and radio buttons.

input[type="checkbox"]:checked {
    background-color: #4caf50;
    color: #fff;
}

In this example, when a checkbox input is checked, it will have a green background and white text.

What is the :empty pseudo-class in CSS, and how can it be used to target elements with no content?

The :empty pseudo-class selects elements that have no children or text content. It's useful for styling elements dynamically based on their content. Here's an example:

p:empty {
    display: none;
}

none.

Can you explain the :enabled and :disabled pseudo-classes in CSS, and provide use cases for each?

The :enabled pseudo-class selects form elements that are currently enabled for user interaction, while :disabled selects elements that are disabled and cannot be interacted with. Here are some examples:

input:enabled {
    border-color: #ccc;
}

button:disabled {
    background-color: #999;
    color: #fff;
}

In the first rule, enabled input elements will have a light gray border. In the second rule, disabled buttons will have a gray background with white text.

What is the :read-only pseudo-class, and how can it be used to style form elements?

The :read-only pseudo-class selects form elements that are in a read-only state, such as text inputs with the readonly attribute set. It allows you to apply styles to elements that users cannot edit. For example:

input:read-only {
    background-color: #eee;
}

In this rule, read-only input elements will have a light gray background.

How does the :valid and :invalid pseudo-classes work in CSS, and when are they commonly used for form validation?

The :valid and :invalid pseudo-classes are used to style form elements based on their validation status. :valid selects elements with valid input, while :invalid selects elements with invalid input. They are commonly used for providing visual feedback on form validation. For example:

input:valid {
    border: 1px solid #0f0;
}

input:invalid {
    border: 1px solid #f00;
}

In this example, valid inputs will have a green border, while invalid inputs will have a red border.

Explain the :first-line pseudo-class in CSS and provide an example of its usage?

The :first-line pseudo-class is used to select and style the first line of text within an element. It's commonly used to add special styling to the initial line of a paragraph. For example:

p:first-line {
    font-weight: bold;
    color: #333;
}

In this CSS rule, the first line of every <p> element will have bold text and a color of dark gray (#333).

What is the :first-letter pseudo-class, and how can it be used to style the first letter of a block-level element?

The :first-letter pseudo-class selects and styles the first letter of a block-level element, such as the initial character of a paragraph. It's often used to create drop caps or apply decorative styles to the first letter. For example:

p:first-letter {
    font-size: 2em;
    color: #ff6600;
}

In this CSS rule, the first letter of every <p> element will be enlarged and have an orange color.

Can you explain the :fullscreen pseudo-class in CSS and its common use cases?

The :fullscreen pseudo-class is used to select elements that are in fullscreen mode, typically in the context of a browser's Fullscreen API. It allows you to apply specific styles when an element is displayed in fullscreen mode. For example:

:fullscreen {
    background-color: black;
    color: white;
}

In this rule, when an element enters fullscreen mode, its background becomes black, and text color becomes white, providing a custom fullscreen styling.

What is the :in-range and :out-of-range pseudo-classes used for in CSS, and provide an example?

The :in-range and :out-of-range pseudo-classes are used to style form elements based on their value being within or outside a specified range, respectively. These are commonly used to provide visual feedback in form validation. Here's an example:

input[type="number"]:in-range {
    border-color: #0f0;
}

input[type="number"]:out-of-range {
    border-color: #f00;
}

In this example, a number input field with a value within its valid range will have a green border, while a value outside the valid range will have a red border.


Conclusion

CSS Pseudo-classes provide developers with a powerful way to style elements based on specific states or conditions. By using pseudo-classes like ":hover," ":active," ":visited," and ":focus," designers can add dynamic and interactive effects to elements, enhancing user interaction and engagement. Pseudo-classes also enable developers to target specific elements based on their position, structure, or content, allowing for more precise styling and customization. Whether it's changing the appearance of links, highlighting form fields, or adding interactive behaviors, CSS Pseudo-classes offer a versatile toolset for creating engaging and user-friendly web experiences.