CSS units define the size of elements in your webpage. They are classified into two main categories: absolute and relative units. Let’s break them down!


1. Types of CSS Units 🛠️

A. Absolute Units 📏

These units have a fixed size that does not change, regardless of the viewport or parent elements.

Examples of Absolute Units:

When to Use Absolute Units?

Absolute units are best for print layouts or when you need precise, unchanging sizes. However, they are not ideal for responsive designs.


B. Relative Units 📐

These units are dynamic and scale based on their environment, such as the parent element or the viewport.

Examples of Relative Units:

  1. % (Percentage):

    Relative to the size of the parent container.

    width: 50%; /* Takes 50% of the parent's width */
    
    
  2. em:

    Relative to the font size of the parent element.

    padding: 1.5em; /* 1.5 times the parent font size */
    
    
  3. rem (Root EM):

    Relative to the font size of the root element (<html>), typically 16px by default.

    font-size: 2rem; /* 2 times the root font size (e.g., 16px * 2 = 32px) */