Simple Methods of Using Pseudo-Classes and Pseudo-Elements in CSS

Simple Methods of Using Pseudo-Classes and Pseudo-Elements in CSS: To begin, first refer to how-to-make-use-of-pseudo-classes-and-pseudo-elements-in-css

Using Dynamic Pseudo-Classes

CSS’s dynamic pseudo-classes let you style components according to their DOM state or user interactions. By dynamically changing styles, these pseudo-classes enhance the interactivity and appeal of online sites. Let’s explore a few of the most important dynamic pseudo-classes with examples, explanations, and outputs:

  • :hover
    • The :hover pseudo-class applies styles when the user hovers their mouse over an element.
  • :focus
    • The :focus pseudo-class styles elements when in focus (e.g., clicked or navigated to using the keyboard).
  • :active
    • The active pseudo-class applies styles to an element when it is activated (e.g., a button or link is clicked).
  • :visited and :link
    • :link styles links that haven’t been visited.
    • :visited styles links that have already been visited.
  • :target
    • The :target pseudo-class styles an element when its id matches the URL fragment (e.g., #section1).
  • :checked
    • The :checked pseudo-class applies styles to selected checkboxes or radio buttons.

Here’s an example that combines multiple dynamic pseudo-classes into a single interactive design:

Explanation of Combined Dynamic Pseudo-Classes

  • :hover: Hovering over the button modifies its background.
  • :active: When pressed, the button turns into a blue background with white text.
  • :focus: When focused, the input field displays a green border.
  • :checked: The label of the checkbox becomes green when it is chosen. 
  • :link &:visited: When a navigation link is visited, it turns purple after first appearing blue.
  • :target: The target The backdrop of a part turns yellow with an orange border when it is targeted by the navigation links.

Figure 1 – Displaying the output of using Combined Dynamic Pseudo-Classes in CSS

Figure 2 – Click the link “Go to Form” or “Go to Info”: The respective section gets highlighted with a yellow background and orange border.

Figure 3 – Hover over the “Submit” button: The button turns light blue.

Figure 4 – Click the “Submit” button: The button temporarily changes to blue with white text.

Figure 5 – Focus on the input field: The border changes to green.
Check the checkbox “I agree to the terms.” The label turns green.
Click on navigation links: Links change from blue to red when hovered.

Websites become interactive with dynamic pseudo-classes, which style items according to user states or activities. Among them are:

  • User interaction: :focus, :active, :hover
  • The link states: :link, :visited
  • Special scenarios: :checked, :target

Developers can avoid employing JavaScript by using these pseudo-classes with standard CSS selectors to produce responsive, captivating, and aesthetically dynamic designs.

Using the :target Pseudo-Class

When an element’s id matches the fragment identifier in the current URL, the CSS :target pseudo-class adds styling to the element. When developing interactive components for single-page apps, such as dynamic sections, tabbed content, or navigational highlights, this is especially helpful.

Example: Highlighting Sections

Explanation

  • HTML Structure:
    • Using unique id properties (notification1, notification2, notification3), a list of notifications (li) is generated.
    • These IDs are pointed to by href properties in navigation links (e.g., #notification1).
  • CSS Styling:
    • By default, all list items (li) have a white backdrop and a light gray border.
    • To make the backdrop bright yellow and the border orange for the targeted notice, use the :target pseudo-class.
  • Behavior:
    • The :target pseudo-class is used to highlight the appropriate list item when a navigation link is clicked.

Figure 6 – Displaying the output of Using the :target Pseudo-Class in CSS (All notifications have a plain white background with a gray border.)

Figure 7 – When hovered, the URL is underlined

Figure 8 – Notification 1’s background turns light yellow, and its border becomes orange. (The same behavior applies for Notification 2 and Notification 3 when their respective links are clicked.)

Why Use This?
This example makes the :target pseudo-class easier to understand and may be used for interactive message displays, to-do lists, and navigation menus.

Using the Language Pseudo-Class (:lang)

CSS’s :lang pseudo-class styles elements according to the language that the HTML’s lang attribute specifies. This is especially helpful for giving multilingual websites customized designs.

Simple Example: Styling Text Based on Language

Explanation

  • HTML Structure:
    • Each <p> element has a lang attribute specifying its language:
    • lang=”en” for English
    • lang=”fr” for French
    • lang=”es” for Spanish
  • CSS Styling:
    • All <p> elements use the default styles.
    • For French, the :lang(fr) pseudo-class applies italic styling and changes the text color to blue.
    • For Spanish, the :lang(es) pseudo-class applies bold styling and red text coloration.
  • Behavior:
    • Depending on the language indicated by the lang parameter, the styles are applied dynamically.

Figure 9 – Displaying the output of Using the Language Pseudo-Class (:lang) in CSS

This example demonstrates how the :lang pseudo-class enables language-specific styles in a straightforward way.

Using the UI Element States Pseudo-Classes

You can use CSS’s UI element state pseudo-classes to style elements according on their interactive states. These include states such as checked, chosen, enabled, and disabled. :enabled, :disabled, :checked, and :indeterminate are typical pseudo-classes for UI element states.

Example: Styling a Form with UI Element States

Explanation

  • HTML Structure:
    • The form has a dropdown menu, checkboxes, and text inputs, among other UI components.
    • Certain fields display their various states using attributes like enabled, disabled, or checked.
  • CSS Styling:
    • :enabled: Styles enabled input fields with a white background.
    • :disabled: Styles disabled input fields with a gray background and muted text and removed interactivity with the cursor: not-allowed.
    • :checked: Styles a label associated with a checked checkbox by changing its color to green and making the text bold.
    • :focus: Styles a focused dropdown menu by changing its border color to blue.

Figure 10 – Displaying the output of Using the UI Element States Pseudo-Classes in CSS

Figure 11 – Checkbox: The label for the checkbox appears normal initially. When the checkbox is checked, the label turns green and bold.
Dropdown Menu: When you click on the dropdown menu, its border color changes to blue, indicating focus.

This example shows how UI state pseudo-classes improve user interactions by applying dynamic styling to items according to their states.

Using Structural Pseudo-Classes

You can style items in CSS according to their relationship or position within the document tree by using structural pseudo-classes. :first-child, :last-child, :nth-child(), :nth-last-child(), :only-child, :empty, and others are examples of common structural pseudo-classes.

Example: Styling List Items Based on Structure

Explanation

  • HTML Structure:
    • There are two lists in the document: one with multiple items and another with just one.
    • In order to illustrate the :empty pseudo-class, an empty <li> is added.
  • CSS Styling:
    • li:first-child: Gives the first list item a light blue background and bold text.
    • li:last-child: Gives the final list item a light green background.
    • The li:nth-child(even) style applies a light gray background to all even-numbered items.
    • li:nth-child(3n): Italicizes and styles every third item with red text.
    • li:only-child: Lists items that are the only child in their parent container using a special style.
    • li:empty: Styles add gray italic lettering and a dashed red border to items that have no content.

Figure 12 – Displaying the output of Using Structural Pseudo-Classes in CSS

This example shows how structural pseudo-classes can be used to enhance the design and usability of content based on its position or structure in the DOM.

Using the Negation Pseudo-Class (:not())

To prevent particular items from being targeted by a collection of styles, use CSS’s :not() pseudo-class. If an element doesn’t match a particular selector, you can still apply styles to it.

Example: Excluding Specific Elements

Explanation

  • HTML Structure:
    • The list contains four items.
    • The second item has a class exclude to demonstrate how it can be excluded using :not().
  • CSS Styling:
    • All list items have a light gray background, padding, and a border by default.
    • All list items except the first one are given a light blue backdrop by using li:not(:first-child).
    • li:not(.exclude): This adds bold text to every item in the list, excluding those that have the class exclude.
    • Li:not(:nth-child(2)): Makes all list items—aside from the second one—red in color.
  • Behavior:
    • Precise stylistic control is possible thanks to the :not() pseudo-class, which dynamically excludes components that match the specified selector.

Figure 13 – Displaying the output of Using the Negation Pseudo-Class (:not()) in CSS

This example shows how the :not() pseudo-class is an effective tool for CSS style since it provides fine-grained control over which items are targeted.

Using the ::first-line Pseudo-Element

The first line of text in a block-level element can be styled using CSS’s ::first-line pseudo-element. It is frequently employed to draw attention to or accentuate a paragraph’s opening line.

Example: Styling the First Line of a Paragraph

Explanation

  • HTML Structure:
    • Two <p> elements contain sample text.
    • Each paragraph has multiple lines of text to demonstrate how only the first line is styled.
  • CSS Styling:
    • p: Sets the default font size, font family, line height, and color for the paragraph.
    • p::first-line: Targets only the first line of the paragraph and applies:
    • Bold font weight.
    • Larger font size (18px).
    • Blue text color.
    • Uppercase text transformation.
  • Behavior:
    • Each paragraph’s opening line uses the styles specified in the ::first-line pseudo-element.
    • The normal paragraph styles are maintained in the remaining lines.

Figure 14 – Displaying the output of Using the ::first-line Pseudo-Element in CSS

This example shows how the ::first-line pseudo-element may be used to style particular text passages in a meaningful and visually unique way.

Using the ::first-letter Pseudo-Element

The first letter of the first line of a block-level element, like a paragraph, can be styled using CSS’s ::first-letter pseudo-element. It is frequently applied to typographic effects like highlighting and drop caps.

Example: Styling the First Letter of a Paragraph

Explanation

  • HTML Structure:
    • Two <p> elements contain text to demonstrate how the ::first-letter pseudo-element works.
  • CSS Styling:
    • p: Establishes the paragraphs’ default line height, font family, and font size.
    • p::first-letter: focuses on the paragraph’s initial letter and uses:
    • 36 pixels is a larger font size to produce a “drop cap” appearance.
    • bold font weight to bring emphasis on it.
    • To make it stand out, the wording is red.
    • To place the letter to the left of the text, use the float: left property.
    • a 10-px right margin to leave room between the text and the letter.
    • To align the drop cap vertically with the first line of text, use the line-height: 1 attribute.
  • Behavior:
    • While the rest of the text uses the usual paragraph styles, the first letter of each paragraph is styled using the ::first-letter rules.

Figure 15 – Displaying the output of Using the ::first-letter Pseudo-Element in CSS

This example shows how the ::first-letter pseudo-element can highlight the first letter in a block of text, improving the text’s readability and visual attractiveness.

Using the ::before Pseudo-Element

In CSS, the ::before pseudo-element is used to place content before the real content of an element. It is frequently used to add text, icons, or decorative components before the primary content of an element without changing the HTML structure.

Example: Adding a Decorative Icon Before a Paragraph

Explanation

  • HTML Structure:
    • Two <p> elements contain text to demonstrate the use of the ::before pseudo-element.
  • CSS Styling:
    • p: Configures the paragraphs’ default font size, family, line height, and color.
    • p::before: Uses the content property to add content before the paragraph text. The content in this instance is a bullet symbol (🔹).
    • Font-size: 24 pixels enlarges the icon.
    • margin-right: 10px creates a gap between the paragraph text and the icon.
    • The icon will align vertically in the center of the line height if vertical-align: middle is used.
  • Behavior:
    • Each paragraph begins with the icon, and the remainder of the paragraph’s text is styled normally.

Figure 16 – Displaying the output of Using the ::before Pseudo-Element in CSS

In order to improve presentation without changing the underlying HTML structure, this example shows how to utilize the ::before pseudo-element to inject content before an element’s actual content.

Using the ::After Pseudo-Element

To add content after an element’s real content, use CSS’s ::after pseudo-element. Similar to ::before, it lets you dynamically add material without changing the HTML structure. Adding decorations, icons, or other content after the element is a common application for it.

Example: Adding a Decorative Icon After a Paragraph

Explanation

  • HTML Structure:
    • Two <p> elements contain text to demonstrate the use of the ::after pseudo-element.
  • CSS Styling:
    • p: Configures the paragraphs’ default font size, family, line height, and color.
    • p::after: Uses the content property to add content after the paragraph text. The content in this instance is a checkmark (✔).
    • Font-size: 24 pixels enlarges the icon.
    • margin-left: adds 10 pixels to the distance between the icon and the paragraph text.
    • color: green makes the checkmark icon’s color green.
  • Behavior:
    • Every paragraph is followed by the checkmark icon, which is spaced 10 pixels from the text.

Figure 17 – Displaying the output of Using the ::After Pseudo-Element in CSS

Adding content after an element’s content with the ::after pseudo-element allows you to improve the design without changing the HTML, as this example shows.

Mobile & Audio

Optimizing Your Dev Environment: The Essential Mobile & Audio Gear for Programmers In the fast-paced…

Read More

Computing

Level Up Your Code: The Best Laptops & Computers for Programmers & Developers Power, Performance…

Read More
Scroll to Top