-
Hajipur, Bihar, 844101
CSS pseudo-elements allow you to style specific parts of an element without adding extra HTML tags. You can insert content, style specific letters or lines, and more.
selector::pseudo-element {
property: value;
}
Pseudo-element | Description |
---|---|
::before |
Inserts content before the element's content |
::after |
Inserts content after the element's content |
::first-letter |
Targets the first letter of text inside element |
::first-line |
Targets the first line of text |
::selection |
Styles text when highlighted by user |
::marker |
Styles bullets or numbers in lists (modern support) |
::before
Exampleh1::before {
content: "👉 ";
}
Adds content before <h1>
without changing the HTML.
::after
Examplep::after {
content: " ✔️";
color: green;
}
Adds a green checkmark after every <p>
.
::first-letter
Examplep::first-letter {
font-size: 2em;
font-weight: bold;
}
Enlarges the first letter of a paragraph (like in magazines).
::first-line
Examplep::first-line {
color: red;
}
Styles the first visible line of text in a paragraph.
::selection
Example::selection {
background-color: yellow;
color: black;
}
Applies when user selects/highlights text.
::marker
(for lists)ul li::marker {
color: red;
font-size: 20px;
}
Styles the bullet or number in a list.
💡
::before
and::after
requirecontent
property.
Q1. Add a star symbol before all <h2>
headings.
Q2. Insert a checkmark after each <li>
.
Q3. Style the first letter of a paragraph to be larger and bold.
Q4. Change color of first line in a <p>
to blue.
Q5. Highlight user-selected text with pink background.
Q6. Make bullets in <ul>
red and larger.
Q7. Append a copyright symbol after footer text.
Q8. Prefix "Note:" before every <blockquote>
.
Q9. Italicize only the first line of a quote.
Q10. Add an emoji after all links.
Q1: Which pseudo-element inserts content before an element?
Q2: Which pseudo-element targets the first letter of text?
Q3: Which property must be used with ::before or ::after?
Q4: What does ::selection do?
Q5: How to style the first line of a paragraph?
Q6: Which is a valid pseudo-element selector?
Q7: Which pseudo-element styles list markers (bullets/numbers)?
Q8: Which pseudo-element would be used to create a drop cap effect?
Q9: Which of the following can you use to insert icons without HTML?
Q10: What will a::after { content: "🔗"; } do?