Pseudo elements let you style specific parts of an element (first letter, first line, before/after content) without adding extra HTML. They are useful for decorative effects and improving design without cluttering markup.
Step 1: Using ::before
What this code does: Puts an orange star in front of a heading by using the ::before pseudo element. The star is created by CSS with the content property, so the HTML stays a plain <h2>Featured Article</h2>. In the preview you will see the heading with an orange star before it, and only the star is orange.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the ::before demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo has one rule that creates the star. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
.demo-title selects the heading, and ::before creates a new box inside it, in front of its own text. The double colon marks a pseudo element (a pseudo class uses a single colon). The content property is required: without it nothing is created. The string holds the star and a space, so the star does not touch the heading text. Generated content is decoration, since users cannot select it and many screen readers skip it.
Properties inside this rule apply to the generated star only. So the star turns orange, while the heading text keeps its normal color. This is a quick way to make a decorative part look different, with no extra span in the HTML.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: one heading.
The HTML has only the text Featured Article. The star you see in the preview comes from the CSS rule, not from this line. The demo-title class links the heading to the rule.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 2: Using ::after
What this code does: Adds a green check mark after a heading with the ::after pseudo element. It works like ::before, but the generated content goes after the element’s text. In the preview the heading reads “Article Published โ”, with only the check mark in green.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the ::after demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo has one rule that creates the check mark. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
::after creates a box as the last child of .demo-title, after its text. The string starts with a space, so the check mark is separated from the word Published. The space matters: generated content is inserted exactly as written.
Only the generated check mark becomes green. The heading text keeps its normal color. A common use of ::after is to add icons, arrows or small labels to links and headings.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: one heading.
The HTML contains only the text Article Published. The check mark is added by the ::after rule through the demo-title class.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 3: Using ::first-letter
What this code does: Styles only the first letter of a paragraph, which gives a simple drop-cap look. The letter becomes blue, bold and larger (40px), while the rest of the paragraph keeps its normal style. No extra tag is needed around the letter in the HTML.
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the ::first-letter demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo has one rule that targets the first letter. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
::first-letter selects the first letter of the first line of .demo-text. The browser finds the letter for you, so you do not wrap it in a span. If the text changes, the style follows the new first letter. color: #007BFF makes this letter blue.
font-size: 40px makes the letter much larger than the 16px text, and font-weight: bold makes it heavier. Only a limited set of properties works with ::first-letter, mainly font, color, background, margin, padding and float. A true drop cap, where the text wraps around the letter, adds float: left.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: one paragraph.
The C at the start of the sentence is the letter that gets styled. It works because p is a block element. The ::first-letter selector does not work on inline elements.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 4: Using ::first-line
What this code does: Styles only the first line of a paragraph: it turns red and bold. The browser decides where the first line ends, so the amount of styled text changes when you resize the window. Try making the preview wider and narrower to see the effect move.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-text::first-line {
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<p class="demo-text">CSS allows developers to create beautiful websites with responsive layouts and modern user interfaces. Resize the preview window to see how the first line changes.</p>
</body>
</html>
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the ::first-line demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo has one rule that targets the first line. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
::first-line selects the first formatted line of .demo-text, whatever its length. It is calculated by the browser when the page is drawn, so it is not a fixed number of words. color: red makes that line red.
font-weight: bold makes the first line bold. As with ::first-letter, only text-related properties are allowed, such as font, color, background and text-decoration. Layout properties such as width or margin do not work here.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: one long paragraph.
The paragraph is long enough to wrap onto several lines, so only the first line is red and bold. When the window gets wider, more words fit in the first line, and more of the text is styled.
</body> closes the body.
</html> is the final closing tag and ends the document.
Step 5: Using ::selection
What this code does: Changes the highlight color that appears when a user selects text with the mouse. Instead of the browser’s default blue highlight, the selected text shows white letters on a black background. Select the paragraph in the preview to see it.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-selection::selection {
background: black;
color: white;
}
</style>
</head>
<body>
<p class="demo-selection">Select this paragraph using your mouse to see the custom selection color.</p>
</body>
</html>
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the ::selection demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo styles the highlight of selected text. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
::selection styles the part of .demo-selection that the user has highlighted. It appears only while text is selected, so you cannot see it until you drag the mouse over the text.
background sets the highlight color, and color sets the color of the selected letters. Only a few properties work for ::selection: color, background color, text-decoration and text-shadow. Always keep strong contrast between the two colors, so the selected text stays readable.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: one paragraph.
Click and drag over this text in the preview. The demo-selection class makes the custom highlight apply to this paragraph only, instead of every selection on the page.
</body> closes the body.
</html> is the final closing tag and ends the document.
Complete CSS Pseudo Elements Example
What this code does: Combines four pseudo elements on one page. The heading gets a flame emoji before and after it (::before and ::after), the paragraph gets a large blue first letter (::first-letter), and selecting text shows white letters on black (::selection). The HTML contains only a heading and a paragraph, and all the decoration comes from CSS.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-title::before {
content: "๐ฅ ";
}
.demo-title::after {
content: " ๐ฅ";
}
.demo-text::first-letter {
font-size: 42px;
color: #007BFF;
font-weight: bold;
}
.demo-title::selection, .demo-text::selection {
background: black;
color: white;
}
</style>
</head>
<body>
<h1 class="demo-title">Academic Block</h1>
<p class="demo-text">CSS pseudo elements allow you to style specific parts of an element without adding extra HTML tags.</p>
</body>
</html>
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the pseudo elements demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo uses one rule for each pseudo element. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
::before adds a flame and a space in front of the heading text. Each pseudo element needs its own rule, because it creates its own box.
::after adds a space and a flame after the text. With both rules the heading is framed by two flames.
The first letter of the paragraph becomes 42px, blue and bold, like the opening of a magazine article.
A group selector gives the heading and the paragraph the same highlight: white text on a black background. The class names keep this rule limited to the demo, so it does not change selection colors anywhere else on the page.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: a heading and a paragraph.
The text is only Academic Block. The two flames come from ::before and ::after.
The first letter C is styled by ::first-letter. Select the text in the preview to see the custom selection colors.
</body> closes the body.
</html> is the final closing tag and ends the document.
Advantages of CSS Pseudo Elements
Advantage
Benefit
Description
Cleaner HTML
Less Markup
Adds visual content without extra HTML elements.
Better Design
Professional Look
Creates decorative effects easily.
Easy Styling
Flexible
Styles only specific parts of an element.
Improved UX
Interactive
Enhances readability and text selection.
Best Practices
Use pseudo elements to keep HTML clean and simple.
Always include the content property with ::before and ::after.
Use pseudo elements for decorative content, not important information.
Prefer the modern double-colon (::) syntax.
Keep decorative effects consistent throughout your website.
Test text selection colors for readability.
Avoid inserting large amounts of content using pseudo elements.
Combine pseudo elements with pseudo classes for advanced UI effects.