Course

Web Development
  • 1.1 Introduction to the Web
  • 2.1 HTML Basics
  • 2.2 Text & Content
  • 2.3 Layout & Grouping
  • 2.4 Lists & Tables
  • 2.5 Media & Embedding
  • 2.6 Forms
  • 2.7 Semantic HTML
  • 2.8 Accessibility & SEO
  • 3.1 CSS Introduction
  • 3.2 CSS Syntax
  • 3.3 CSS Selectors
  • 3.4 Typography
  • 3.5 Colors & Backgrounds
  • 3.6 Text Styling
  • 3.7 Box Model
  • 3.8 Units
  • 3.9 Display & Positioning
  • 3.10 Layout Systems
  • 3.11 Responsive Design
  • 3.12 Animations
  • 3.13 Modern CSS
  • 3.14 Performance & Accessibility
  • 3.15 Tailwind CSS Basics
  • 4.1 Introduction to JavaScript
  • 4.2 Variables & Scope
  • 4.3 Data Types
  • 4.4 Type Conversion
  • 4.5 Operators
  • 4.6 Control Flow
  • 4.7 Loops
  • 4.8 Functions
  • 4.9 Advanced Functions
  • 4.10 Objects & Arrays
  • 4.11 The this Keyword

3.13 Modern CSS

Updated Jul 20, 2026

3.13 Modern CSS

Updated Jul 20, 2026

3.13 Modern CSS

Modern CSS includes advanced techniques and tools that make websites easier to build, maintain, scale, and reuse. These features improve developer productivity and help create cleaner, more efficient code.

In this lesson, we'll cover:

  1. CSS Variables
  2. CSS Functions
  3. CSS Modules
  4. BEM
  5. Sass
  6. PostCSS
  7. CSS-in-JS

CSS Variables

CSS Variables (also called Custom Properties) allow you to store reusable values that can be used throughout your stylesheet.

Instead of repeating the same value multiple times, define it once and reuse it everywhere.


Why Use CSS Variables?

Without Variables

.button {

    background: #2563EB;

}

.card {

    border-color: #2563EB;

}

.link {

    color: #2563EB;

}

If the primary color changes, every occurrence must be updated manually.


With Variables

:root {

    --primary-color: #2563EB;

}

.button {

    background: var(--primary-color);

}

.card {

    border-color: var(--primary-color);

}

.link {

    color: var(--primary-color);

}

Now the color only needs to be changed in one place.


Variable Syntax

Declaration

:root {

    --primary-color: blue;

}

Usage

h1 {

    color: var(--primary-color);

}

Multiple Variables

:root {

    --primary: #2563EB;
    --secondary: #9333EA;
    --text-color: #333333;
    --radius: 12px;

}

Fallback Values

If a variable doesn't exist, a fallback value can be used.

color: var(--primary, blue);

Dark Mode Example

:root {

    --bg: white;
    --text: black;

}

.dark {

    --bg: #111827;
    --text: white;

}

Changing variables automatically updates the entire theme.


CSS Functions

CSS Functions allow calculations and dynamic styling directly inside CSS.


var()

Accesses a CSS Variable.

background: var(--primary);

calc()

Performs mathematical calculations.

width: calc(100% - 50px);

Example

.main {

    height: calc(100vh - 80px);

}

Meaning:

Viewport Height
      -
Navbar Height

min()

Returns the smaller value.

width: min(100%, 1200px);

max()

Returns the larger value.

width: max(300px, 50%);

clamp()

The most useful modern CSS function for responsive values.

Syntax

clamp(
minimum,
preferred,
maximum
)

Example

font-size: clamp(
1rem,
5vw,
3rem
);

Result:

  • Responsive text size
  • Never smaller than 1rem
  • Never larger than 3rem

Common CSS Functions

Function Purpose
var() Access CSS Variables
calc() Perform calculations
min() Choose smaller value
max() Choose larger value
clamp() Responsive values
url() Load external assets

CSS Modules

CSS Modules solve the problem of global CSS conflicts.

They are commonly used with:

  • React
  • Next.js
  • Vue

Problem with Traditional CSS

File 1

.title {

    color: blue;

}

File 2

.title {

    color: red;

}

Both classes share the same name, causing conflicts.


CSS Modules Solution

Button.module.css

.title {

    color: blue;

}

React Component

import styles from "./Button.module.css";

<h1 className={styles.title}>
    Hello
</h1>

The browser generates a unique class name.

Example

.title_x7a92

This prevents naming conflicts.


Benefits

  • Scoped Styles
  • No Naming Conflicts
  • Easy Maintenance
  • Perfect for Components

BEM

BEM stands for:

  • Block
  • Element
  • Modifier

It is a CSS naming convention used in large projects.


Why Use BEM?

Poor naming becomes difficult to maintain.

Bad examples:

.title
.button
.card

These names are too generic.


BEM Structure

block

block__element

block--modifier

Example

<div class="card">

    <h2 class="card__title">

        Product

    </h2>

</div>

Modifier Example

<button class="button button--primary">

    Save

</button>
.button {

    padding: 10px;

}

.button--primary {

    background: blue;

}

Benefits

  • Consistent Naming
  • Easier Maintenance
  • Better for Large Projects

Sass

Sass (Syntactically Awesome Style Sheets) is a CSS preprocessor that adds powerful features to CSS.


Install

npm install sass

Variables

$primary: #2563EB;

.button {

    background: $primary;

}

Nesting

Regular CSS

.nav {}

.nav a {}

Sass

.nav {

    a {

        color: white;

    }

}

Mixins

Reusable groups of CSS.

@mixin center {

    display: flex;
    justify-content: center;
    align-items: center;

}

Use

.hero {

    @include center;

}

Benefits

  • Variables
  • Nesting
  • Mixins
  • Functions
  • Cleaner Code

PostCSS

PostCSS is a tool that processes CSS using plugins.

Workflow

CSS
   ↓
PostCSS Plugins
   ↓
Optimized CSS

Autoprefixer

Automatically adds browser prefixes.

Input

.card {

    display: flex;

}

Output

.card {

    display: -webkit-flex;
    display: flex;

}

CSS Nesting

PostCSS can also support nested CSS.

.card {

    h2 {

        color: blue;

    }

}

Tailwind CSS

Tailwind CSS uses PostCSS internally to generate optimized production CSS.


Benefits

  • Optimized CSS
  • Better Browser Support
  • Faster Development

CSS-in-JS

CSS-in-JS allows you to write CSS directly inside JavaScript.

It is popular in React applications.


Styled Components Example

const Button = styled.button`

    background: blue;
    color: white;

`;

Emotion Example

<div
css={{
    color: "blue"
}}
>

    Hello

</div>

Dynamic Styling

Styles can change based on JavaScript values.

const Button = styled.button`

    background:
    ${props =>
        props.primary
        ? "blue"
        : "gray"};

`;

Why CSS-in-JS?

  • Component-Based Styling
  • Dynamic Styles
  • Scoped CSS
  • JavaScript Integration

Popular Libraries

Library Common Usage
Styled Components React
Emotion React
Stitches React
Panda CSS Modern CSS-in-JS

CSS Modules vs CSS-in-JS

CSS Modules CSS-in-JS
Separate CSS File CSS Inside JavaScript
Faster Runtime More Dynamic
Simpler More Powerful
Great for Most Apps Great for Complex Apps

Modern CSS Workflow

Traditional

HTML
 +
CSS

Modern

React
   ↓
CSS Modules
   ↓
PostCSS
   ↓
Production CSS

Enterprise Applications

React
   ↓
TypeScript
   ↓
CSS Modules
      or
CSS-in-JS
   ↓
PostCSS

Real-World Example

CSS Variables

:root {

    --primary: #2563EB;

}

Card

.card {

    background: white;
    border-radius: 12px;
    padding: 20px;

}

Responsive Typography

h1 {

    font-size: clamp(
        2rem,
        5vw,
        4rem
    );

}

Modern Layout

.container {

    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 20px;

}

What Is Used Today?

Most Common Modern CSS Features

  • CSS Variables
  • Flexbox
  • CSS Grid
  • Media Queries
  • CSS Modules
  • PostCSS

React Ecosystem

  • CSS Modules
  • Tailwind CSS
  • Styled Components
  • Emotion

Next.js Ecosystem

  • CSS Modules
  • Tailwind CSS
  • Sass

Summary

Topic Purpose
CSS Variables Store reusable values
CSS Functions Dynamic styling and calculations
CSS Modules Scoped CSS
BEM Naming methodology
Sass CSS Preprocessor
PostCSS CSS Processing Tool
CSS-in-JS Write CSS inside JavaScript

Key Takeaway

Modern CSS development focuses on reusable, responsive, and maintainable code.

Example:

:root {

    --primary: #2563EB;

}

h1 {

    font-size: clamp(
        2rem,
        5vw,
        4rem
    );

}

Modern websites commonly combine:

CSS Variables
        +
Flexbox
        +
Grid
        +
CSS Modules
        +
PostCSS
        ↓
Scalable & Maintainable Modern CSS

While all of these tools are valuable, CSS Variables, Flexbox, Grid, and PostCSS are the most widely used in modern frontend development. Frameworks like React and Next.js often build upon these technologies to create large, maintainable applications.

3.12 Animations3.14 Performance & Accessibility