CSS Pseudo-elements
CSS pseudo-elements are special selectors that allow you to style specific parts of an element. They are denoted by double colons (::) and are used to add decorative or functional elements to the document’s structure. Here are some commonly used CSS pseudo-elements:
- ::before: The ::before pseudo-element allows you to insert content before the content of an element. It is often used to add decorative elements or icons to elements. It must have a “content” property defined in the CSS, even if it is empty.Example:css
.element::before {
content: "";
/* Other styles */
}
::after: The ::after pseudo-element is similar to ::before but inserts content after the content of an element. It is also commonly used for decorative purposes.
Example:
css
.element::after {
content: "";
/* Other styles */
}
::first-line: The ::first-line pseudo-element allows you to style the first line of text within an element. You can apply styles such as font properties, text decorations, and colors specifically to the first line.
Example:
css
.element::first-line {
/* Styles for the first line */
}
::first-letter: The ::first-letter pseudo-element allows you to style the first letter of text within an element. You can apply styles such as font properties, text decorations, and colors specifically to the first letter.
Example:
css
.element::first-letter {
/* Styles for the first letter */
}
::selection: The ::selection pseudo-element targets the portion of the text that is selected by the user. You can apply styles to the selected text, such as changing the background color or text color.
Example:
css
::selection { background-color: yellow; color: black; }
These are just a few examples of CSS pseudo-elements. There are others like ::placeholder, ::marker, ::backdrop, etc., each serving a specific purpose. Pseudo-elements are powerful tools for adding visual enhancements and manipulating specific parts of your HTML content using CSS.