CS Engineering Gyan

CSS Pseudo Classes & Elements

Modern websites are expected to provide interactive and visually appealing user experiences. Simply applying styles to HTML elements is often not enough to create engaging webpages. Developers frequently need to change the appearance of elements when users interact with them or style only specific portions of an element without modifying the HTML structure. CSS Pseudo Classes and CSS Pseudo Elements make this possible.

Pseudo Classes allow developers to apply styles based on the current state of an element. For example, a button can change color when the mouse pointer moves over it, an input field can display a different border when selected, or a visited hyperlink can appear differently from an unvisited one.

Pseudo Elements, on the other hand, allow developers to style specific parts of an element or insert decorative content before or after an element using only CSS. This reduces unnecessary HTML code and keeps webpages clean and maintainable.

In this tutorial, you will learn the fundamentals of CSS Pseudo Classes and Pseudo Elements, understand their syntax, explore commonly used selectors, and create interactive webpages using practical examples.

What are CSS Pseudo Classes?

A CSS Pseudo Class is a special keyword added to a selector that defines the unique state of an HTML element. Instead of permanently styling an element, pseudo classes apply styles only when certain conditions are satisfied.

For example, a button may look different when a user hovers over it, clicks it, or selects it using the keyboard. These changes improve usability and provide visual feedback during user interaction.

Pseudo Classes begin with a single colon (:) followed by the pseudo class name.

Main Features

Why are CSS Pseudo Classes Important?

Interactive websites rely heavily on visual feedback. Without pseudo classes, users may not know whether a button is clickable, an input field is active, or a hyperlink has already been visited.

Pseudo Classes make webpages more intuitive by automatically changing styles according to user actions. This improves navigation, readability, and overall website usability.

Benefits

Syntax of CSS Pseudo Classes

A pseudo class is written after a selector using a single colon (:).

Syntax


selector:pseudo-class{

property:value;

}

Example


button:hover{

background:green;

}

When the mouse pointer moves over the button, the background color automatically changes to green.

The :hover Pseudo Class

The :hover pseudo class is activated when the user places the mouse pointer over an element. It is one of the most frequently used pseudo classes in modern web development.

Example


button{

background:#0d6efd;

color:white;

padding:12px 24px;

border:none;

}

button:hover{

background:#084298;

cursor:pointer;

}

The button changes its background color and displays a pointer cursor when the user hovers over it.

The :focus Pseudo Class

The :focus pseudo class styles elements that currently receive keyboard input or have been selected by the user. It is commonly used with text fields, textareas, and buttons.

Example

input{

border:1px solid gray;

padding:10px;

}

input:focus{

border:2px solid #0d6efd;

outline:none;

}

When the input field is selected, its border becomes blue, making it easier for users to identify the active field.

The :active Pseudo Class

The :active pseudo class applies styles while an element is being clicked or activated. It provides immediate feedback that the user's action has been recognized.

Example


button:active{

transform:scale(0.95);

}

While the button is pressed, it becomes slightly smaller, creating a realistic clicking effect.

The :link Pseudo Class

The :link pseudo class targets hyperlinks that have not yet been visited. Developers use it to define the default appearance of links.

Example


a:link{

color:#0d6efd;

text-decoration:none;

}

Unvisited hyperlinks appear with the specified blue color and without underlines.

The :visited Pseudo Class

The :visited pseudo class styles hyperlinks that the user has already opened. This helps visitors distinguish between previously visited and unvisited pages.

Example


a:visited{

color:purple;

}

Visited links automatically appear purple, improving website navigation.

Practical Example Using Multiple Pseudo Classes

The following example demonstrates how several pseudo classes can work together to create an interactive button.

HTML


<button>

Register Now

</button>

CSS


button{

background:#198754;

color:white;

padding:14px 30px;

border:none;

border-radius:6px;

transition:0.3s;

}

button:hover{

background:#157347;

}

button:active{

transform:scale(0.96);

}

button:focus{

outline:3px solid lightblue;

}

This single button responds differently when hovered, clicked, or focused, providing users with a more interactive and professional experience.

Commonly Used CSS Pseudo Classes

Pseudo Class Purpose
:hover Styles an element when the mouse pointer is over it.
:focus Styles the currently selected input element.
:active Styles an element while it is being clicked.
:link Targets unvisited hyperlinks.
:visited Targets visited hyperlinks.
:first-child Selects the first child element.
:last-child Selects the last child element.
:nth-child() Selects elements based on position.

What are CSS Pseudo Elements?

CSS Pseudo Elements allow developers to style a specific part of an HTML element or generate additional content without modifying the HTML document. Unlike pseudo classes, which apply styles based on an element's state, pseudo elements target portions of an element such as the first letter, first line, or insert content before or after an element.

Pseudo Elements help developers create attractive user interfaces while keeping HTML clean and easy to maintain. They are widely used for decorative icons, quotation marks, labels, separators, badges, and various visual effects.

Modern CSS uses two colons (::) before the pseudo element name.

Main Features

Difference Between Pseudo Classes and Pseudo Elements

Pseudo Classes Pseudo Elements
Style an element based on its current state. Style a specific part of an element.
Begin with a single colon (:). Begin with two colons (::).
React to user interaction. Create or style content inside an element.
Examples: :hover, :focus, :active Examples: ::before, ::after, ::first-letter

The ::before Pseudo Element

The ::before pseudo element inserts content before the actual content of an element. It is commonly used to display icons, symbols, decorative lines, or labels without changing the HTML structure.

Example


h2::before{

content:"โ˜… ";

color:orange;

}

Every heading automatically displays a star symbol before its text.

The ::after Pseudo Element

The ::after pseudo element inserts content after the content of an element. It is useful for adding arrows, decorative symbols, labels, or separators.

Example


a::after{

content:" โ†’";

}

Each hyperlink automatically displays an arrow after its text, improving visual appearance without additional HTML elements.

The ::first-letter Pseudo Element

The ::first-letter pseudo element styles only the first letter of a block-level element. It is frequently used in magazines, blogs, and news websites to create decorative opening letters.

Example


p::first-letter{

font-size:40px;

font-weight:bold;

color:#0d6efd;

}

The first letter of every paragraph becomes larger and more noticeable, giving the article a professional appearance.

The ::first-line Pseudo Element

The ::first-line pseudo element styles only the first visible line of a paragraph. It is useful for emphasizing introductions or important content.

Example


p::first-line{

font-weight:bold;

color:#198754;

}

Only the first line of the paragraph receives the specified styles, while the remaining text remains unchanged.

The ::selection Pseudo Element

The ::selection pseudo element changes the appearance of text when users select it with the mouse or keyboard.

Example


::selection{

background:#0d6efd;

color:white;

}

Selected text displays a blue background with white text, creating a cleaner and more attractive selection effect.

The ::placeholder Pseudo Element

The ::placeholder pseudo element styles placeholder text displayed inside input fields and textareas.

Example


input::placeholder{

color:gray;

font-style:italic;

}

The placeholder text becomes gray and italic, making it visually different from user-entered content.

The :first-child Pseudo Class

The :first-child pseudo class selects the first child element inside its parent. It is useful when the first item requires unique styling.

Example


li:first-child{

font-weight:bold;

color:#198754;

}

The first list item automatically appears bold and green.

The :last-child Pseudo Class

The :last-child pseudo class targets the final child element within its parent.

Example


li:last-child{

color:red;

}

The last item in the list becomes red, making it easy to highlight important information.

The :nth-child() Pseudo Class

The :nth-child() pseudo class selects elements according to their position within the parent element. It is commonly used for tables, lists, cards, and grid layouts.

Example


tr:nth-child(even){

background:#f5f5f5;

}

Every even table row receives a light gray background, improving readability by creating alternating row colors.

Practical Example Using Multiple Pseudo Selectors

The following example combines pseudo classes and pseudo elements to create a more interactive and visually appealing webpage.

HTML


<h2>CSS Tutorial</h2>

<button>Start Learning</button>

CSS


h2::before{

content:"๐Ÿ“˜ ";

}

button{

padding:12px 24px;

background:#0d6efd;

color:white;

border:none;

border-radius:6px;

transition:0.3s;

}

button:hover{

background:#084298;

}

button:active{

transform:scale(0.96);

}

In this example, the heading automatically displays a book icon using ::before, while the button responds to user interaction with :hover and :active. This combination creates an attractive and interactive user interface without adding unnecessary HTML elements.

Combining Pseudo Classes and Pseudo Elements

One of the strengths of modern CSS is the ability to combine pseudo classes and pseudo elements within the same selector. This allows developers to create interactive and visually attractive components without adding extra HTML elements or JavaScript.

For example, a button can change its background color when hovered while also displaying an icon before its text using a pseudo element. Combining these features produces professional and responsive user interfaces.

Example


button::before{

content:"โœ“ ";

}

button:hover{

background:#0b5ed7;

color:#fff;

}

In this example, every button automatically displays a check mark before its text, and its background changes when the user moves the mouse pointer over it.

Real-World Applications

CSS Pseudo Classes and Pseudo Elements are widely used in modern websites because they improve appearance without increasing HTML complexity.

Application Purpose
Navigation Menus Highlight links on hover and active pages.
Buttons Create hover and click effects.
Forms Highlight focused input fields.
Articles Create decorative first letters.
Cards Add decorative labels using ::before.
Notifications Display icons without extra HTML.
Tables Highlight alternate rows using nth-child().
Lists Style first and last items differently.

Best Practices

Common Mistakes

Performance Tips

Pseudo Classes and Pseudo Elements are lightweight CSS features, but following good coding practices improves website performance and maintainability.

Advantages of CSS Pseudo Classes and Elements

Pseudo Classes vs Pseudo Elements

Pseudo Classes Pseudo Elements
Style an element based on its state. Style a specific part of an element.
Begin with one colon (:). Begin with two colons (::).
React to user interaction. Create or modify visual content.
Examples: :hover, :focus, :active Examples: ::before, ::after, ::selection
Improve interactivity. Improve visual presentation.

Frequently Asked Interview Questions

1. What is a CSS Pseudo Class?

A CSS Pseudo Class is a keyword added to a selector that styles an element when it enters a particular state, such as hover, focus, or active.

2. What is a CSS Pseudo Element?

A CSS Pseudo Element styles a specific part of an element or inserts generated content before or after an element without modifying the HTML.

3. What is the difference between :hover and ::before?

:hover changes styles when the user places the mouse pointer over an element, while ::before inserts content before an element's existing content.

4. Which property is required for ::before and ::after?

The content property must be specified; otherwise, these pseudo elements will not appear.

5. Can pseudo classes and pseudo elements be used together?

Yes. They can be combined to build interactive and visually appealing components while keeping HTML clean and maintainable.

Summary

Concept Description
:hover Applies styles when the pointer moves over an element.
:focus Styles the currently focused element.
:active Applies styles while an element is being clicked.
::before Inserts generated content before an element.
::after Inserts generated content after an element.
::first-letter Styles the first letter of an element.
::first-line Styles the first visible line of text.
::selection Styles selected text.
:nth-child() Selects elements according to their position.

Conclusion

CSS Pseudo Classes and Pseudo Elements are essential tools for creating modern, interactive, and visually appealing websites. Pseudo Classes respond to user interactions such as hovering, focusing, or clicking, while Pseudo Elements allow developers to style specific parts of an element or generate decorative content without changing the HTML structure.

By mastering these selectors, developers can build cleaner stylesheets, improve accessibility, enhance user experience, and create professional interfaces with less code. When combined with Flexbox, Grid, CSS Variables, Media Queries, and other modern CSS features, Pseudo Classes and Pseudo Elements become powerful tools for developing responsive and maintainable websites.

โ† Previous: CSS Variables Next: Modern UI Styling โ†’
Home Visit Our YouTube Channel