header-logo

CODE WITH FAHIM

CSS Pseudo-elements

CSS pseudo-elements are special selectors that let you style specific parts of an element’s content — parts that you can’t normally target directly with HTML or regular selectors.

What Pseudo-Elements Actually Do:

Pseudo-elements create virtual elements that don’t exist in your HTML, but behave like real ones in the CSS rendering layer.

They:




Syntax:

CSS3 introduced the **double-colon (::)** notation to distinguish pseudo-elements from pseudo-classes (like :hover), though most browsers still support the single-colon form for backward compatibility:
p::first-line {
color: blue;
}


🔹 Difference Between Pseudo-Classes and Pseudo-Elements:

Type Example Targets
Pseudo-Class a:hover State of an element
Pseudo-Element p::after Part of an element



Limitations:




Complete Pseudo-element Reference (20 Total):


Pseudo-Element Description
::before Inserts generated content **before** an element’s content.
::after Inserts generated content **after** an element’s content.
::first-line Styles only the **first line** of an element’s formatted text.
::first-letter Styles the **first letter** of an element’s text.
::selection Styles the portion of text the user has **selected/highlighted**.
::marker Styles the **bullet or numbering** of a list item (<li>).
::placeholder Styles the placeholder text in **form fields** (<input>, <textarea>).
::backdrop Styles the **background** layer behind an element in full-screen mode (e.g., <dialog>).
::cue Styles the **captions/subtitles** displayed in media elements (<video>).
::file-selector-button Styles the button part of an <input type="file"> element.
::target-text Styles the specific **text fragment** highlighted when navigating with a text fragment URL.
::part(name) Targets named parts inside a **Shadow DOM** component (Web Components).
::slotted(selector) Targets content inserted into a **Shadow DOM <slot>** element.
**::-webkit-autofill** **(Vendor Prefix)** Targets input fields when they have been **auto-filled** by the browser.
**::-webkit-slider-thumb** **(Vendor Prefix)** Styles the **handle (thumb)** of an <input type="range">.
**::-webkit-slider-runnable-track** **(Vendor Prefix)** Styles the **track** of an <input type="range">.
**::-webkit-validation-next-paged-page** **(Vendor Prefix)** Styles the native **error message balloon** for form validation.
**::-webkit-search-decoration** **(Vendor Prefix)** Targets the **search icon/clear button** in <input type="search">.
**::next-paged-page** **(Print Layout)** Targets the generated **empty boxes** for print and paged media layouts.
**::grammar-error** **(Vendor Prefix)** Styles text marked by the browser as a **grammar error**.



Pseudo-element Use Cases:


1.::before & ::after - (Content Generation)

These are used with the `content` property to insert non-semantic decorative content.

CSS code:

p::before {
content: "👉 ";
color: var(--neon-blue);
}
p::after {
content: " 💡";
color: var(--neon-pink);
}

HTML code:

<p class="content-ex">This paragraph has generated content on both sides.</p>

Output (Combined):

👉This paragraph has generated content on both sides. 💡




2.::first-line - (Text Formatting)

Styles only the first line of text. The line break adjusts with the viewport size.

CSS code:

.text-ex::first-line {
font-variant: small-caps;
color: var(--neon-pink);
}

HTML code:

<p class="text-ex">This is a long paragraph where only the very first line will be styled differently, regardless of how wide the browser window is. The styling property is restricted to only font-based styles, color, background, etc.</p>
Output:

This is a long paragraph where only the very first line will be styled differently, regardless of how wide the browser window is. The styling property is restricted to only font-based styles, color, background, etc.




3.::first-letter - (Text Formatting)

Used to create drop-cap effects on the first letter of a block-level element.

CSS code:

.text-ex::first-letter {
font-size: 3em;
color: var(--neon-blue);
float: left;
margin-right: 10px;
}

HTML code:

<p class="first-letter-ex">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
Output:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.




4.::selection - (User Interaction)

Styles the foreground (text color) and background of text selected by the user.

CSS code:

::selection {
background: var(--neon-pink);
color: var(--dark-bg);
}

HTML code:

<p class="selection-ex">Try selecting this text to see the custom neon highlight.</p>
Output:

Try selecting this text to see the custom neon highlight.




5.::marker - (List Styling)

Allows styling of the bullet point or numbered counter of a list item.

CSS code:

li::marker {
content: "★ ";
color: var(--neon-blue);
font-size: 1.2em;
}

HTML code:

<ul class="marker-ex">
<li>List Item One</li>
<li>List Item Two</li>
</ul>
Output:




6.::placeholder - (Form Styling)

Styles the instructional placeholder text in inputs and textareas.

CSS code:

input::placeholder {
color: var(--neon-pink);
opacity: 0.8;
font-style: italic;
}

HTML code:

<input type="text" placeholder="Enter your neon code..." class="placeholder-ex">
Output:




7.::backdrop - (Modal/Full-Screen Styling)

Targets the layer that covers the rest of the page when an element is in a top layer (like an HTML <dialog> or full-screen video).

CSS code:

dialog::backdrop {
background-color: rgba(0, 0, 0, 0.9);
backdrop-filter: blur(4px);
}

Explanation:

When a <dialog> element is opened, this rule styles the shaded area behind it, giving a dark, focused look.


8.::cue - (Media/Accessibility)

Styles the text of WebVTT captions/subtitles used in HTML5 <video> elements.

CSS code:

video::cue {
background-color: var(--neon-blue);
color: var(--dark-bg);
text-shadow: 0 0 5px #fff;
}

Explanation:

Ensures subtitles are styled with high contrast and a neon effect to stand out over the video content.


9.::target-text - (Navigation/Highlighting)

Styles the specific text highlighted by the browser when a URL includes a text fragment identifier (e.g., `#::target=...`).

CSS code:

::target-text {
background-color: var(--neon-pink-glow);
color: var(--dark-bg);
}

Explanation:

If a user lands on your page directly to a specific sentence, that sentence is highlighted with the pink neon glow.


10.::file-selector-button - (Form Styling)

Styles the button portion of an `` element.

CSS code:

input[type="file"]::file-selector-button {
background-color: var(--neon-blue);
color: var(--dark-bg);
border: none;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
}

HTML code:

<input type="file" class="file-selector-ex">
Output:




11.::target-text - (Navigation/Highlighting)

Styles the specific text highlighted by the browser when a URL includes a text fragment identifier (e.g., `#::target=...`).

CSS code:

::target-text {
background-color: var(--neon-pink-glow);
color: var(--dark-bg);
}

Explanation:

If a user lands on your page directly to a specific sentence, that sentence is highlighted with the pink neon glow.


12.::part(name) - (Web Components / Shadow DOM)

Used to style named internal parts of a custom web component from the outside (Light DOM).

CSS code (In global stylesheet):

my-component::part(button-style) {
border: 2px solid var(--neon-pink);
background: var(--dark-bg);
}

Explanation:

This allows external CSS to penetrate the Shadow DOM and style internal elements that have the `part="button-style"` attribute.


13.::slotted(selector) - (Web Components / Shadow DOM)

Used within the Shadow DOM component's stylesheet to style content that has been "slotted" in from the Light DOM.

CSS code (Inside Shadow DOM stylesheet):

::slotted(h3) {
color: var(--neon-blue);
text-decoration: underline;
}

Explanation:

If the user of the custom component passes an `

` tag into the component, this rule applies styling to that specific `

`.


14.::-webkit-autofill - (Form Styling - Vendor Prefixed)

A crucial pseudo-element for modern form styling. It targets the input element when the browser has auto-filled its value (e.g., saved passwords/addresses).

CSS code:

/* Note: Requires vendor prefix and "!important" to override browser styles */
input:-webkit-autofill {
-webkit-box-shadow: 0 0 0 1000px #2b2b1a inset !important; /* Dark green background */
-webkit-text-fill-color: var(--valid-color) !important;
}

HTML code:

<input type="email" autocomplete="email" placeholder="Auto-fillable field">
Output:




15.::-webkit-slider-thumb / ::-webkit-slider-runnable-track - (Form Styling - Vendor Prefixed)

Used to style the complex components of a range slider input.

CSS code:

input[type="range"]::-webkit-slider-thumb {
background: var(--neon-pink);
border: 1px solid white;
}
input[type="range"]::-webkit-slider-runnable-track {
background: #333;
}

HTML code:

<input type="range" min="0" max="100" value="50" class="slider-ex">
Output:




16.::-webkit-validation-next-paged-page - (Form Validation - Vendor Prefixed)

Targets the native error message balloon that appears when a form field fails validation.

CSS code:

/* Vendor prefix is required and styling capabilities are limited. */
::-webkit-validation-next-paged-page {
background: var(--neon-pink);
color: var(--dark-bg);
border: 1px solid var(--neon-pink);
}

Explanation:

If you try to submit a required, empty field, the resulting error bubble will be neon pink.


17.::-webkit-search-decoration - (Form Styling - Vendor Prefixed)

Used to style the internal decorations (magnifying glass, clear 'x' button) inside an `input[type="search"]`.

CSS code:

::-webkit-search-decoration {
display: none; /* Hide the native search elements */
}

HTML code:

<input type="search" placeholder="Search" class="search-ex">
Output:




18.::next-paged-page - (Print Layout - Paged Media)

Targets the blank boxes generated by block elements in paged media. Useful for controlling printed output.

CSS code:

@media print {
p::next-paged-page {
content: "--- NEW PAGE ---";
color: #999;
margin-top: 1em;
}
}

Explanation:

Adds generated content in the space left by a forced page break, helpful for debugging print layouts.


19.::grammar-error - (Error Highlighting - Vendor Prefixed)

Used to style text marked by the browser's native grammar check feature.

CSS code:

/* Note: Requires vendor prefix (e.g., -webkit) in actual usage */
::grammar-error {
border-bottom: 2px dotted var(--neon-blue);
background-color: rgba(45, 217, 254, 0.1);
}

Explanation:

This non-standard rule allows you to customize the default wavy green line seen under grammatically incorrect text.


20.::spelling-error - (Error Highlighting - Vendor Prefixed)

Used to style text marked by the browser's native spell check feature.

CSS code:

/* Note: Requires vendor prefix (e.g., -webkit) in actual usage */
::spelling-error {
background-color: rgba(255, 0, 141, 0.2);
border-bottom: 2px dashed var(--neon-pink);
}

Explanation:

This non-standard rule allows you to customize the default wavy red line seen under misspelled text.