The Wayback Machine - https://web.archive.org/web/20250126154504/https://www.geeksforgeeks.org/css-units-em-rem-px-vh-vw/
Open In App

CSS units – %, em, rem, px, vh, vw

Last Updated : 09 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

CSS units are used to define the sizes of elements in web design. Different units offer varying levels of flexibility and control over the layout and appearance of web pages. Commonly used units such as percentages (%), em, rem, pixels (px), viewport height (vh), and viewport width (vw).

CSS units fall into two main categories:

  • Absolute Units: These units have a fixed size regardless of the context, such as px (pixels).
  • Relative Units: These units are relative to another value, such as the parent element’s size or the viewport’s dimensions, making them more flexible for responsive design. Examples include %, em, rem, vw, and vh.

Detailed Explanation of CSS Units

1. Percentage (%)

The percentage unit is used to set values relative to the current font-size or the size of the parent element. This unit is highly flexible and allows for responsive design adjustments.

Example: Setting the font-size relative to the current font-size.

font-size: 150%;

2. em:

The em unit is relative to the font-size of the element it is used on. For example, 2em means two times the size of the current font.

Example: Setting the font-size relative to the current font-size.

font-size: 2em;

Note: Here, 2em means 2 times the size of the current font.

3. rem:

The rem unit is relative to the root element (usually the <html> element) font-size, making it consistent across the entire document.

Example: Setting the font-size relative to the browser base font-size.

font-size: 1.5rem

4. Pixles(px):

The pixel unit is an absolute unit used to define sizes in terms of pixels. It ensures consistent size across different screens.

Example: Setting the font-size in pixels.

font-size: 16px;

5. Viewport Height (vh)

The vh unit is relative to 1% of the viewport’s height, making it useful for setting sizes based on the height of the browser window.

Example: Setting the height relative to the viewport height.

height: 50vh;

6. Viewport Width (vw)

The vw unit is relative to 1% of the viewport’s width, making it useful for setting sizes based on the width of the browser window.

Example: Setting the width relative to the viewport width.

width: 50vw;

Examples of CSS Units

Example 1: Using Pixels (px) vs. Percentages (%)

The pixel unit is an absolute unit to set the width i.e. it is always the same. A percentage unit is based on a relative unit i.e. it is based on its parent size. 

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        .box {
            background: red;
            border: 1px solid black;
            margin: 10px;
        }

        .parent {
            background: white;
            border: 1px solid black;
        }

        .fifty-percent {
            width: 50%;
        }

        .one-hundred {
            width: 100px;
        }

        .parent {
            width: 250px;
            ;
        }
    </style>
</head>

<body>
    <h3>Output-1 </h3>
    <div class="box fifty-percent">50%</div>
    <div class="box one-hundred">100px</div>
    <hr>
    <h3>Output-2 </h3>
    <div class="parent">
        <div class="box fifty-percent">50%</div>
        <div class="box one-hundred">100px</div>
    </div>
</body>

</html>

Output :

Example 2:  Viewport Width (vw) and Viewport Height (vh)

Another type of relative width is called view width (vw) and view height (vh).  1vw equals one percent of the entire screen size so 100 vw would take up the entire width and 50vw obviously would take up half the width but the important thing about vw versus percentages is that viewport units are based on the entire screen size while percentages are relative to their parent. 

HTML
<!DOCTYPE html>
<html>

<head>
    <title>Page Title</title>
    <style>
        .box {
            background: red;
            border: 1px solid black;
            margin: 10px;
        }

        .parent {
            background: white;
            border: 1px solid black;
        }

        .fifty-percent {
            width: 50%;
        }

        .fifty-vw {
            width: 50vw;
        }

        .twenty-five-vh {
            height: 25vh;
        }

        .parent {
            width: 100px;
            ;
        }
    </style>
</head>

<body>
    <h3>Output-1 </h3>
    <div class="box fifty-percent">50%</div>
    <div class="box fifty-vw">50vw</div>
    <div class="box twenty-five-vh">25vh</div>
    <hr>
    <h3>Output-2 </h3>
    <div class="parent">
        <div class="box fifty-percent">50%</div>
        <div class="box fifty-vw">50vw</div>
        <div class="box twenty-five-vh">25vh</div>
    </div>
</body>

</html>

Output:

Example 3: em vs. rem

Both REM units and EM units are relative but instead of being relative to things around them such as the width of their parents or the height of the parents they’re actually relative to the font size.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>Page Title</title>
    <style>
        .parent {
            background: white;
            border: 1px solid black;
        }

        .one-rem {
            font-size: 1rem;
        }

        .one-em {
            font-size: 1em;
        }

        .two-rem {
            font-size: 2rem;
        }

        .two-em {
            font-size: 2em;
        }

        .parent {
            font-size: 30px;
        }
    </style>
</head>

<body>
    <h3>Output-1</h3>
    <div class="one-rem">1rem</div>
    <div class="one-em">1em</div>
    <div class="two-rem">2rem</div>
    <div class="two-em">2em</div>
    <hr>
    <h3>Output-2</h3>
    <div class="parent">
        <div class="one-rem">1rem</div>
        <div class="one-em">1em</div>
        <div class="two-rem">2rem</div>
        <div class="two-em">2em</div>
    </div>
</body>

</html>

Output:

When to Use Each Unit

1. Percentage (%)

  • Ideal for fluid layouts where element sizes should adapt to their parent container.
  • Commonly used for width and padding.

2. em

  • Use when you need to scale elements relative to the current font size.
  • Useful for nested elements where you want styles to scale consistently.

3. rem

  • Suitable for consistent scaling across the document.
  • Recommended for global font sizes and spacing.

4. Pixels (px)

  • Use for fixed sizes that need to remain constant.
  • Ideal for borders, small icons, or elements with specific size requirements.

5. Viewport Height (vh) and Viewport Width (vw)

  • Perfect for full-screen layouts or elements that should scale with the viewport size.
  • Useful for setting background images, sections, or banners that span the entire screen.


Next Article

Similar Reads

three90RightbarBannerImg