Academic Block

CSS Pseudo Elements Get Certified

What are CSS Pseudo Elements?

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>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-title::before {
    content: "โ˜… ";
    color: orange;
}
</style>
</head>
<body>
<h2 class="demo-title">Featured Article</h2>
</body>
</html>

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>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-title::after {
    content: " โœ”";
    color: green;
}
</style>
</head>
<body>
<h2 class="demo-title">Article Published</h2>
</body>
</html>

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>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-text::first-letter {
    color: #007BFF;
    font-size: 40px;
    font-weight: bold;
}
</style>
</head>
<body>
<p class="demo-text">CSS makes webpages beautiful and responsive.</p>
</body>
</html>

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>

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>

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>

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.

Web Resources on CSS Pseudo Elements

1. MDN Web Docs : Pseudo-elements
2. Web.dev : Pseudo-elements
3. CSS Tricks.com : A Little Reminder That Pseudo Elements are Children
4. Dev.to : Must-Know CSS Pseudo-elements

๐Ÿงช Test Your CSS Code

Edit the CSS code on the left and click “Run Code” to see the result on the right.

๐Ÿ“ CSS Code
๐Ÿ‘๏ธ Preview (HTML rendered with your CSS)

Click “Run Code” to see the result here.