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 : in legacy CSS).
Syntax
selector::pseudo-element{
property: value
}
::(the double colon is a syntax for pseudo-elements)
Commonly Used CSS Pseudo-Elements
1. ::before
The ::before pseudo-element allows you to insert content before the actual content of an element.
<style>
p::before {
content: "✨ ";
color: gold;
}
</style>
2. ::after
Similar to ::before, the ::after pseudo-element inserts content after the element’s content. It is often used to add icons, styling cues, or additional details.
<style>
p::after {
content: " 🔥";
color: red;
}
</style>
3. ::first-letter
The ::first-letter pseudo-element targets the first letter of a block of text, generally used for decorative initials in paragraphs.
<style>
p::first-letter {
font-size: 2em;
color: blue;
}
</style>
4. ::first-line
The ::first-line pseudo-element is used to style the first line of a block of text. It is particularly useful for emphasizing the introduction of paragraphs.
<style>
p::first-line {
font-weight: bold;
color: green;
}
</style>
5. ::placeholder
The ::placeholder pseudo-element styles placeholder text inside input fields.
input::placeholder {
color: gray;
font-style: italic;
}
</style>
</head>
6. ::marker
Styles the marker of list items.
<style>
li::marker {
color: purple;
font-size: 1.5em;
}
</style>
7. ::selection
Styles the selected text.
<style>
::selection {
background: yellow;
color:green;
}
</style>
8. ::backdrop
Styles the backdrop of modal elements like <dialog>. It helps to style the background of the dialog box opened as a modal on the screen of the user.
dialog::backdrop {
background: rgba(232, 233, 0, 0.5);
}
Pseudo-elements for Specific Contexts
::file-selector-button for Media and Interactivity
This pseudo-element selector helps to style the button of a input type file.
<style>
::file-selector-button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
::file-selector-button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
::spelling-error for Error Handling
The ::spelling-error pseudo-element styles text that has been flagged as a spelling error by the browser’s spell-check functionality.
<style>
::spelling-error {
background-color: #ffdddd;
text-decoration: underline solid red;
}
</style>
CSS Pseudo Elements – FAQ’s
What is the purpose of ::-webkit-progress-bar and ::-webkit-progress-value?
The ::-webkit-progress-bar and ::-webkit-progress-value pseudo-elements are used to style the appearance of progress bars in WebKit-based browsers (like Chrome and Safari). ::-webkit-progress-bar targets the track or outer container of the progress bar, while ::-webkit-progress-value targets the filled portion (the progress indicator) inside the bar, allows to customize the look of both elements independently.
How do you apply styles using pseudo-elements?
To apply styles using pseudo-elements, you add a colon (single for CSS2, double for CSS3) followed by the name of the pseudo-element to a selector. This targets the styled part of the element directly in your CSS.
What are some common CSS pseudo-elements?
Some common CSS pseudo-elements include ::before, ::after, ::first-line, and ::first-letter. Each serves a different purpose, such as adding content before or after an element’s content, or styling the initial letter or line of a text block.
Can you generate content with CSS pseudo-elements?
Yes, the ::before and ::after pseudo-elements can be used to generate content in CSS. By using the content property, you can insert new content in the page layout before or after the content of an element.
What are the limitations of pseudo-elements?
Pseudo-elements cannot be applied to replaced elements (like <img> or <input>), as they don’t have content in the same way that a text container might. Additionally, some CSS properties are not applicable to certain pseudo-elements.