CSS pseudo elements every CSS developer should know
Last Updated :
18 May, 2023
CSS pseudo-elements are used to add special effects/styles to a specific part of the selected elements. A CSS selector selects the HTML element(s) for styling purposes. CSS selectors select HTML elements according to its id, class, type, attribute etc.
For example, the ::before and ::after pseudo-elements can be used to insert content before or after an element’s content.
Pseudo-elements are always declared using a double colon (::) syntax. They are always followed by the name of the pseudo-element and must be used in conjunction with a selector.
Reason to learn about CSS pseudo-elements:
- CSS pseudo-elements can be a valuable tool for front-end web developers. It can help you create better, more visually appealing websites and improve the overall user experience.
- With pseudo-elements, you have the ability to add content before or after an element, insert a first letter or line, and select a portion of an element which results in increased flexibility.
- It helps you create reusable styles that can be applied to multiple elements, saving time and making it much easier to maintain the styles in the future.
- Using pseudo-elements you add extra content to your HTML code without adding additional HTML elements.
- CSS pseudo-elements help in increasing visual enhancements of the code as well.
CSS pseudo-elements every CSS developer should know:
CSS ::selection Selector: The selection selector is used to set the CSS property to the part of the document that is selected by the user (such as clicking and dragging the mouse across text).
- It is used to style the text that the user in an HTML document selects.
- It applies styles to the selected text, allowing you to customize the look and feel of text selections.
- It is a custom text selector that is used to override the default text selection color.
Example:
CSS
::selection {
color: green;
background: white;
}
|
Output: In this example, the background color of the selected text is set to white and the text color is set to green. You can use any CSS properties that are applicable to text, such as font size, font weight, etc., to style the selected text.

CSS ::selection Selector
Note: This selector is not supported in internet explorer but works on all other major browsers.
CSS ::placeholder Selector: The placeholder selector in the CSS pseudo-element is used to design the placeholder text by changing the text color and it allows to modify the style of the text.
- It is used to style the placeholder text of an input element.
- It is the text that appears in an input field when it is empty and has not been focused.
- The placeholder text is set with the placeholder attribute, which specifies a hint that further describes the expected value of an input field.
Example:
CSS
::-webkit-input-placeholder {
color: green;
}
:-ms-input-placeholder {
color: green;
}
::placeholder {
color: green;
}
|
Output: In this example, the color of the text that appears in the input field when it is empty is set to green.

CSS ::placeholder Selector
Note: This selector is relatively new and may not be supported in all browsers.
CSS ::marker Selector: The ::marker selector in CSS is used to change the marker properties. Here marker is the marker box of a list item.
- It is used to style the marker box of a list item, which is the content that is displayed before the list item text, typically as a bullet point or number.
- It is used in customizing the appearance of the marker, such as its color, font, and size, without affecting the content of the list item itself.
Example:
CSS
::marker {
color: green;
font-size: 30px;
}
|
Output: In this example, the “::marker” pseudo-element is used to change the color of the markers in an ordered list

CSS ::marker Selector
Note: It is not supported in all browsers, so it’s always better to check the compatibility of your styles before only.
CSS ::first-letter Selector: The ::first-letter selector is used to applying the style to the first letter of the first line of a block-level element, the condition is it should not be preceded by other content ( such as images or inline tables).
- It is used to select and style the first letter of a block-level element.
- It is commonly used to format the initial letters of a paragraph, such as in drop caps or other typographic effects.
- The below properties can be used with::first-letter: font properties, color properties, background properties, margin properties, padding properties, border properties, text-decoration, vertical-align (only if the float is ‘none’), text-transform, line-height, float, and clear.
Example:
CSS
p::first-letter {
font-size: 300%;
color: #006400;
}
|
Output: In this example, the first letter is displayed in the color & font size that we have set.

CSS ::first-letter Selector
Note: It is very important to know that it will work with block-level elements & not with inline elements.
CSS ::first-line Selector: The ::first-line selector in CSS is used to apply style to the first line of a block-level element.
- It is used to select and style the first line of a block-level element.
- It is commonly used to format the first line of a paragraph, such as to change its font size, font family, or color.
Example:
CSS
p::first-line {
background-color: lightgreen;
}
|
Output: In this example, the first line of the paragraph is displayed in green just like we have set.

CSS ::first-line Selector
Note: It is very important to know that it will work with block-level elements & not with inline elements.
Conclusion: Thus, we learned about the CSS pseudo-elements every CSS developer should know about & it’s the importance which ultimately results in better user experience, increased flexibility, better maintenance & increased visual enhancements of the code.
Similar Reads
CSS pseudo elements every CSS developer should know
CSS pseudo-elements are used to add special effects/styles to a specific part of the selected elements. A CSS selector selects the HTML element(s) for styling purposes. CSS selectors select HTML elements according to its id, class, type, attribute etc. For example, the ::before and ::after pseudo-el
5 min read
10 CSS Selectors Every Developer Should Know
What's the first thing for any website to create a good impression on a user? ... Yes...it's the user interface for any website. Every developer knows how important it is to create a beautiful design for users to interact with any website. Giving styling to web pages smartly in a minimal amount of
9 min read
7 CSS Hacks Every Developer Should Know
When it comes to web development, the user interface (UI) plays a crucial role. A well-designed layout not only attracts users but also sets the tone for their overall experience. While JavaScript often takes the spotlight, CSS remains a fundamental language, powering 96% of all websites. As a devel
6 min read
10 CSS functions every Front End developer Should Know
Like all other programming languages, CSS also have their functions. CSS functions are used as a value for various CSS properties. Unlike other programming languages, we cannot write our own functions in CSS. Let us take a look at some of the important CSS functions you should know - 1. url() Functi
2 min read
CSS Tricks That Every Web Developer Should Know
CSS (Cascading Style Sheets) is the magic ingredient that breathes life into the raw structure of HTML, transforming it into visually captivating and user-friendly interfaces. While the core concepts of CSS are relatively easy to grasp, mastering its nuances takes dedication and practice. But the re
7 min read
5 Amazing CSS Styles that Every Developer Should Know
CSS (Cascading Style sheets) helps the developer to enhance the visual appearance of web pages, In other words, we can say that CSS adds life to web pages and beautifies it. Using CSS, we can change the layout, color, fonts, images, and the best part is that for a particular style or layout we can w
5 min read
Difference Between Pseudo-Classes and Pseudo-Elements in CSS
Pseudo-classes and pseudo-elements in CSS are selectors that allow the styling of specific states or parts of elements. Pseudo-classes target states like ': hover' or ': active', while pseudo-elements like ' ::before ' or:: after' style specific parts of an element. They enhance CSS styling by provi
2 min read
Explain the concept of pseudo-elements in CSS
CSS pseudo-elements are used to style specified parts of an element and are used to add special effects to some selectors. To do this, we do not need any script like javascript to add the effects. Syntax: selector::pseudo-element { property: value; } Note: The double-colon replaced the single-colon
3 min read
CSS Viewer Chrome Extension for Web Developers
CSS viewer inspect and displays the CSS properties of a web page. It is a very smart and useful extension that identifies CSS properties anywhere one points his mouse. It is time-saving and very fast, and also it makes identifying CSS easy. It allows one to click on any image, button, text, etc on a
4 min read
How to style even and odd elements using CSS ?
In this article, we will learn how to style even and odd elements using CSS. Styling every element uniformly may not always be the best approach. you'll explore multiple approaches to differentiate between even and odd elements. This is particularly valuable for designing lists, tables, or structure
3 min read
CSS Pseudo Elements
A pseudo-element is a keyword added to a selector that lets you style specific parts of an element. For example, you can style the first line of a paragraph, add content before or after an element, or create complex effects with minimal code. Pseudo-elements are denoted by a double colon (::) (or :
5 min read
Why does CSS work with fake elements ?
CSS (Cascading Style Sheets) is a stylesheet language used to design a webpage to make it attractive. The reason for using this is to simplify the process of making web pages presentable. It allows you to apply styles on web pages. More importantly, it enables you to do this independent of the HTML
2 min read
Explain the term “pseudo-class” in CSS3
Cascading Style Sheets referred to as CSS is a stylesheet language used to design and describe the presentation of the webpage to make it attractive. The main use of CSS is to simplify the process of making web pages presentable. The way elements should be rendered on screen is described by CSS. CSS
4 min read
Different ways to hide elements using CSS
While working on UI/UX we have to take care of every web element that appears on the webpage to make the user experience easier. While designing such pages we often need to hide or show some particular HTML elements on any event or on any particular interval. In this article, we could do that easily
8 min read
How does a browser determine what elements match a CSS selector ?
When a browser loads a web page, it first parses the HTML and creates a Document Object Model (DOM) tree. The DOM tree is a hierarchical representation of the HTML elements on a web page. Each element in the DOM tree is represented as a node, and the relationships between nodes represent the structu
4 min read
How to use SVG with :before or :after pseudo element ?
Using SVG with :before or :after pseudo-elements in CSS allows for the dynamic insertion of vector graphics into HTML elements. This technique enhances design flexibility by leveraging SVG's scalability and interactivity while maintaining a clean HTML structure and reducing HTTP requests for image a
3 min read
Most Common CSS Mistakes that Web Developers Make
Cascading Style Sheets (CSS) is a style sheet language used to apply styles on HTML tags. Most people think that CSS is easy to learn but the fact is that CSS is really straightforward to learn but it is difficult to master. It requires a lot of practice to master CSS. There are some common mistakes
6 min read
How to Select and Manipulate CSS pseudo-elements using JavaScript / jQuery ?
In this article, we will learn how to select and manipulate CSS pseudo-elements using JavaScript (or jQuery). CSS pseudo-elements allow developers to style and enhance specific parts of an HTML document with the help of selectors like::before and::after, which provide the flexibility to add decorati
2 min read
How to make div elements display inline using CSS ?
Displaying div elements inline allows them to share horizontal space and appear in a single line, making your layout more dynamic and responsive. This guide explores different CSS techniques to achieve this, including flexbox, inline-block, float, and span elements. Why Display Div Elements Inline?R
4 min read