Tuesday, 3 March 2026

@scope in CSS Explained with Examples (Complete Guide)

scope in CSS Explained with Examples

@scope in CSS

The @scope rule lets you apply CSS only inside a specific section of the page — without adding extra classes everywhere.

It helps prevent style conflicts and makes CSS more component-based.

1. Simple Example

Only the h2 inside .card becomes blue

Outside h2 is not affected

------------------------------------------------------------------

2. Scoped Button Styling

Only button in .section-a turns blue

.section-b button stays default

------------------------------------------------------------------

3. Using :scope with @scope

  1. :scope refers to .box
  2. Adds border only to .box
  3. Styles p inside .box
  4. Outside elements are NOT affected

------------------------------------------------------------------

No comments:

Post a Comment

Master CSS Grid: Build Responsive Layouts Easily (With Examples)

Learn 10 powerful CSS3 tricks with real examples to build modern, responsive, and interactive Introduction CSS G...