Discover how to add stylish decorations before elements without cluttering your HTML!
Why Before pseudo-element in CSS? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you want to add a decorative icon or text before every heading on your webpage. You try typing the icon or text manually before each heading in your HTML.
If you want to change or remove that icon later, you have to edit every single heading manually. This is slow, error-prone, and makes your HTML messy.
The before pseudo-element lets you add content before an element using only CSS. You keep your HTML clean and can change the added content in one place.
<h2>★ Welcome</h2> <h2>★ About Us</h2>
h2::before {
content: "★ ";
}
<h2>Welcome</h2>
<h2>About Us</h2>You can decorate or add extra content before elements easily and consistently without touching your HTML.
Adding quotation marks before blockquotes or icons before menu items to improve design and user experience.
Manually adding content before elements is slow and messy.
before pseudo-element adds content via CSS, keeping HTML clean.
It allows easy, consistent styling and quick updates.
Practice
::before pseudo-element do?Solution
Step 1: Understand the role of
The::before::beforepseudo-element inserts content before the main content of an element without modifying the HTML structure.Step 2: Compare with other options
Options B, C, and D describe different CSS behaviors unrelated to::before. It adds content after the element's main content. describes::after, not::before.Final Answer:
It inserts content before the element's main content without changing HTML. -> Option BQuick Check:
::beforeadds content before = A [OK]
- Confusing ::before with ::after
- Thinking it changes HTML structure
- Assuming it styles background only
::before?Solution
Step 1: Identify the key property for ::before
Thecontentproperty is mandatory to show anything with::before. Without it, no content appears.Step 2: Check other properties
Properties likedisplay,position, andvisibilityaffect layout or visibility but do not create content.Final Answer:
content -> Option DQuick Check:
content property shows ::before content = B [OK]
- Forgetting to add content property
- Using display instead of content
- Assuming position creates content
p::before { content: "Hello "; }<p>Given HTML: <p>World</p>Solution
Step 1: Understand ::before content insertion
The::beforeadds "Hello " before the paragraph's original text "World".Step 2: Combine the inserted and original text
The final visible text is "Hello World" because the pseudo-element content appears before the original content.Final Answer:
Hello World -> Option AQuick Check:
Inserted content before text = D [OK]
- Ignoring the original text
- Thinking content replaces text
- Confusing order of content
h1::before { content; "Note: "; color: red; }Solution
Step 1: Check syntax of content property
The code usescontent; "Note: "which is incorrect. It should becontent: "Note: "with a colon.Step 2: Verify other parts
The pseudo-element syntaxh1::beforeis correct, color can be used, and content can be any string, so other options are wrong.Final Answer:
Missing colon after content property -> Option CQuick Check:
Property syntax needs colon = C [OK]
- Using semicolon instead of colon after property
- Miswriting pseudo-element syntax
- Thinking color can't style ::before
::before to add a red asterisk (*) before all required form labels for accessibility?Solution
Step 1: Target only required labels
Use the selectorlabel.required::beforeto add content only before labels with class "required".Step 2: Add red asterisk before label text
Setcontent: "*"andcolor: redto show a red star before the label text for accessibility.Final Answer:
label.required::before { content: "*"; color: red; } -> Option AQuick Check:
Use ::before with content and color on required labels = A [OK]
- Using ::after instead of ::before
- Adding content to label without ::before
- Applying content property directly on label without pseudo-element
