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.8 Units

Updated Jul 12, 2026

3.8 Units

Updated Jul 12, 2026

CSS units define the size, spacing, position, and dimensions of elements.

Units are commonly used for:

  • Width
  • Height
  • Font Size
  • Margin
  • Padding
  • Borders
  • Positioning

CSS units are divided into:

  • Absolute Units
  • Relative Units
  • CSS Functions

What is a CSS Unit?

A CSS value consists of a number and a unit.

Example:

width: 300px;
300 → Value
px  → Unit

Absolute Units

Absolute units always have a fixed size.

They do not depend on the parent element or screen size.

Pixels (px)

The most commonly used CSS unit.

h1 {
    font-size: 32px;
}

Example:

.card {
    width: 300px;
}

Advantages:

  • Predictable
  • Easy to use

Disadvantages:

  • Less responsive

Centimeters (cm)

width: 10cm;

Represents physical centimeters.

Rarely used on websites.


Millimeters (mm)

width: 50mm;

Rarely used in web design.


Inches (in)

width: 2in;

Mostly used for printing.


Points (pt)

Common in printed documents.

font-size: 12pt;

Picas (pc)

font-size: 1pc;
1pc = 12pt

Rarely used.


Absolute Units

Unit Meaning
px Pixels
cm Centimeters
mm Millimeters
in Inches
pt Points
pc Picas

Relative Units

Relative units depend on another value.

They are widely used in responsive web design.


Percentage (%)

Relative to the parent element.

.container {
    width: 100%;
}

Example:

.card {
    width: 50%;
}

If the parent is 1000px wide:

50% = 500px

em

Relative to the font size of the parent element.

Example:

.parent {
    font-size: 20px;
}

.child {
    font-size: 2em;
}

Result:

40px

Spacing example:

padding: 2em;

rem

Relative to the root (html) font size.

Example:

html {
    font-size: 16px;
}

h1 {
    font-size: 2rem;
}

Result:

32px

Unlike em, rem always uses the root font size.

Common values:

1rem   = 16px
1.5rem = 24px
2rem   = 32px
3rem   = 48px

vw (Viewport Width)

Relative to the browser width.

width: 50vw;

Example:

.hero {
    width: 100vw;
}

Creates a full-width section.


vh (Viewport Height)

Relative to the browser height.

height: 100vh;

Example:

.hero {
    height: 100vh;
}

Commonly used for hero sections.


vmin

Uses the smaller viewport dimension.

width: 20vmin;

vmax

Uses the larger viewport dimension.

width: 20vmax;

ch

Relative to the width of the character 0.

Useful for readable text containers.

width: 60ch;

Example:

.article {
    max-width: 70ch;
}

Relative Units

Unit Relative To
% Parent Element
em Parent Font Size
rem Root Font Size
vw Viewport Width
vh Viewport Height
vmin Smaller Viewport Dimension
vmax Larger Viewport Dimension
ch Character Width

Absolute vs Relative Units

Absolute:

width: 300px;

Always:

300px

Relative:

width: 50%;

Depends on the parent element.

Absolute Relative
Fixed Flexible
Less Responsive Responsive
Easier Initially Better Long-Term
px rem, %, vw, vh

CSS Functions

CSS functions perform calculations and create dynamic values.


calc()

Performs mathematical calculations.

width: calc(100% - 50px);

Example:

height: calc(100vh - 80px);

Common use:

Viewport Height
− Navbar Height

min()

Returns the smaller value.

width: min(100%, 1200px);

Meaning:

Use whichever is smaller.

max()

Returns the larger value.

width: max(50%, 500px);

Meaning:

Always at least 500px.

clamp()

Defines:

  • Minimum value
  • Preferred value
  • Maximum value

Syntax:

clamp(min, preferred, max)

Example:

font-size:
clamp(
    16px,
    4vw,
    40px
);

Meaning:

Minimum → 16px
Responsive Scaling
Maximum → 40px

Responsive typography:

h1 {

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

}

var()

Accesses CSS variables.

Define:

:root {

    --primary:
    #2563EB;

}

Use:

button {

    background:
    var(--primary);

}

url()

Loads external resources.

background-image:
url("hero.jpg");

Common CSS Functions

Function Purpose
calc() Perform calculations
min() Smallest value
max() Largest value
clamp() Responsive sizing
var() CSS variables
url() Load resources

Real-World Examples

Responsive typography:

h1 {

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

}

Responsive container:

.container {

    width:
    min(
        100%,
        1200px
    );

}

Layout calculation:

.main {

    height:
    calc(
        100vh - 80px
    );

}

Modern CSS Recommendations

Typography:

font-size: 1rem;

Layout width:

width: 100%;
max-width: 1200px;

Full-screen sections:

height: 100vh;

Responsive text:

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

Key Takeaway

Modern CSS primarily uses responsive units and functions.

font-size: 1rem;

width: 100%;

height: 100vh;

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

width:
calc(
    100% - 2rem
);

For modern responsive websites:

  • Use rem for typography.
  • Use % for widths.
  • Use vh and vw for viewport sizing.
  • Use clamp() for responsive text.
  • Use calc() for dynamic layouts.
3.7 Box Model3.9 Display & Positioning