HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials
  • Insights
  • Verify Certificate
HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials
HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials

HTML5andCSS3.org helps developers learn modern frontend skills with practical tutorials, production-ready snippets, and fast web tools built for everyday coding.

Quick Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Copyright
  • Disclaimer

Top Tools

  • Try It Editor
  • JSON Formatter
  • CSS Minifier
  • HTML Minifier
  • Base64 Encoder / Decoder

Contact

  • business@html5andcss3.org
Newsletter

Learn at your own pace, practice with useful tools, and build websites you're proud of.

© 2011-2026 html5andcss3.org. All rights reserved.

How to Use

CSS Tutorial

CSS Introduction

What is CSSCSS SyntaxHow to Add CSSCSS Comments

CSS Foundations

Cascade & InheritanceCSS SpecificityCSS UnitsBox SizingCSS VariablesCSS Math Functions

CSS Selectors

CSS Element SelectorCSS Class SelectorCSS ID SelectorCSS Universal SelectorCSS Group SelectorCSS CombinatorsCSS Attribute SelectorsCSS Pseudo ClassesCSS Pseudo Elements

CSS Colors and Backgrounds

CSS ColorsCSS HEX ColorsCSS RGB ColorsModern ColorsCSS Background ColorCSS Background ImageLayered Backgrounds

CSS Text & Fonts

CSS Text ColorCSS Text AlignmentCSS Text DecorationCSS Font FamilyCSS Font SizeWeb Fonts & TypographyText Wrapping

CSS Box Model

CSS MarginCSS PaddingCSS BorderCSS OutlineCSS Width and Height

CSS Layout

Normal FlowCSS DisplayCSS PositionAnchor PositioningCSS FloatCSS OverflowCSS Z-indexIntrinsic SizingAspect Ratio & Object FitMulti-column Layout

CSS Flexbox

Flex ContainerFlex DirectionJustify ContentAlign ItemsFlex WrapFlex Item Sizing

CSS Grid

CSS Grid ContainerCSS Grid ColumnsCSS Grid RowsCSS Grid GapCSS Grid Layout ExampleGrid PlacementGrid AreasCSS Subgrid

CSS Lists, Tables & Forms

CSS ListsCSS TablesCSS Form StylingCSS Input Fields

CSS Effects

CSS ShadowsCSS GradientsCSS FiltersCSS OpacityClipping & MaskingBlending & BackdropScroll Snap

CSS Animations & Transitions

CSS TransformCSS 2D TransformCSS 3D TransformCSS TransitionsEntry & Exit TransitionsCSS AnimationsScroll AnimationsView TransitionsMotion Paths

CSS Responsive Design

CSS Media QueriesResponsive LayoutMobile First DesignContainer QueriesPreference Media QueriesFeature Queries

Modern CSS Authoring

Cascade LayersNesting & ScopeLogical PropertiesGenerated Content

CSS Accessibility

Accessible CSSCSS ThemingModern Form StylingCSS User Interface

CSS Output & Performance

Print StylesCSS PerformanceCSS ArchitectureDebugging CSS

CSS Reference

CSS At-rulesCSS FunctionsCSS Tutorial PDF

CSS Projects

Responsive CSS Project
  1. Home
  2. /
  3. Tutorials
  4. /
  5. CSS Tutorial
  6. /
  7. CSS Pseudo Elements

CSS Selectors

CSS Pseudo Elements

CSS Pseudo Elements are used to style specific parts of an HTML element.

Unlike pseudo classes, which target an element's state, pseudo elements target a particular portion of an element's content. The supporting concepts are explained in CSS Pseudo Classes and Generated Content.

Pseudo elements begin with a double colon (::) and allow you to:

  • Style the first letter
  • Style the first line
  • Insert content before or after an element
  • Customize selected text

Common pseudo elements include:

  • ::before
  • ::after
  • ::first-letter
  • ::first-line
  • ::selection

Syntax

CSS Pseudo Elements Syntax

css

selector::pseudo-element {
    property: value;
}

CSS Pseudo Elements Example

css

p::first-letter {
    font-size: 32px;
}

Makes the first letter of every paragraph larger.

Attributes

Pseudo ElementDescriptionExample
::beforeInserts content before an elementp::before
::afterInserts content after an elementp::after
::first-letterStyles the first letterp::first-letter
::first-lineStyles the first linep::first-line
::selectionStyles selected text::selection

Style registered text ranges

The ::highlight() pseudo-element styles a named range registered through the CSS Custom Highlight API. It is useful for search results, annotations, and editor selections that should not require wrapping the document text in extra elements. The range still needs to be created and registered with JavaScript.

Focused example

css

::highlight(search-result) {
  color: #172554;
  background: #fde68a;
  text-decoration: underline;
}

Example

CSS Pseudo Elements Complete Example

html

<!DOCTYPE html>
<html>
<head>
    <title>CSS Pseudo Elements Example</title>
    <style>
        p::first-letter {
            font-size: 36px;
            color: red;
            font-weight: bold;
        }
        p::first-line {
            color: blue;
        }
        h2::before {
            content: "★ ";
            color: orange;
        }
        h2::after {
            content: " ★";
            color: orange;
        }
        ::selection {
            background-color: yellow;
            color: black;
        }
    </style>
</head>
<body>
    <h2>Featured Article</h2>
    <p>
        CSS pseudo elements allow developers to style specific parts of an element.
        They are commonly used for decorative effects, typography, and dynamic content.
    </p>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
::before1121.54
::highlight()10510514917.2

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8.

Notes

  • Pseudo elements use a double colon (::) in modern CSS.
  • ::before and ::after require the content property.
  • ::first-letter is often used in magazine-style layouts.
  • ::selection allows customization of highlighted text.
  • Pseudo elements do not modify the actual HTML content.

Common Pseudo Elements

Pseudo ElementPurpose
::beforeAdd content before an element
::afterAdd content after an element
::first-letterStyle the first letter
::first-lineStyle the first line
::selectionStyle selected text

Conclusion

CSS Pseudo Elements allow you to style specific portions of content and create decorative effects without modifying the HTML structure. They are powerful tools for enhancing typography, improving design, and adding visual details to web pages.

PreviousCSS Pseudo ClassesNextCSS Colors