30. CSS Architecture (BEM), @supports & Dark Mode Themes

🔴 Advanced

1. Definition

BEM (Block Element Modifier) methodology (.block__element--modifier) maintains modular component naming. Feature queries (@supports) test browser CSS property support. Dark Mode toggles color themes using CSS variables, prefers-color-scheme, print stylesheets (@media print), and performance optimization (content-visibility: auto).

2. Why It Is Used

BEM architecture eliminates selector specificity wars. content-visibility: auto skips rendering off-screen elements, improving long page rendering speed by up to 7x.

3. How It Works

BEM uses strict double-underscore __ for descendant elements and double-hyphen -- for state modifiers. content-visibility: auto isolates off-screen DOM subtrees from layout calculation.

4. Syntax

/* BEM component structure */
.card {}                  /* Block */
.card__title {}           /* Element */
.card__title--accent {}   /* Modifier */

/* Performance containment */
.section-heavy {
  content-visibility: auto;
  contain-intrinsic-size: 0 500px;
}

5. BEM & Architecture Reference Table

BEM PartNaming SyntaxComponent Example
Block.block.btn, .card, .navbar
Element.block__element.btn__icon, .card__title
Modifier.block--modifier.btn--primary, .card--featured

6. Beginner Example

.btn { padding: 10px 20px; }
.btn--danger { background-color: #ef4444; color: white; }

7. Intermediate Example

/* Feature query check */
@supports (backdrop-filter: blur(10px)) {
  .header {
    backdrop-filter: blur(10px);
  }
}

8. Advanced Dark Mode & Print Optimization Example

/* Dark mode theme variables */
:root {
  color-scheme: light dark;
  --bg-main: #ffffff;
  --text-main: #0f172a;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg-main: #0f172a;
    --text-main: #f8fafc;
  }
}

@media print {
  body { background: white; color: black; }
  .no-print { display: none !important; }
}

9. Line-by-Line Code Explanation

  • content-visibility: auto;: Skips rendering off-screen element layout until scrolled into view.

10. Expected Terminal / Browser Output

A modular web component architecture supporting dark theme switching and print optimization.

11. Common Mistakes & Pitfalls

  • Nesting BEM element names: Writing .card__header__title is invalid. Keep BEM names flat (e.g. .card__title).

12. Important Rules & Limitations

  • BEM class selectors always maintain flat specificity (0,0,1,0).

13. Multilingual Explanation: English

BEM structures clean component class names. Combine with prefers-color-scheme for automatic dark mode support.

14. Multilingual Explanation: Hindi (Roman Script)

BEM methodology se CSS class names simple aur organized rehte hain. Dark mode ke liye CSS variables ka istemaal karein.

15. Multilingual Explanation: Marathi (Roman Script)

BEM mule CSS naming organized rahte. Dark mode sathi prefers-color-scheme vapara.

16. Practice Questions

  1. Why does BEM use flat single-class selectors instead of deep nested selectors?

17. Practical Coding Challenge

Write BEM classes for a .product-card block containing __image, __title, and --highlighted modifier.

18. Related Topics