The Wayback Machine - https://web.archive.org/web/20241227220301/https://www.geeksforgeeks.org/css-units/
Open In App

CSS Units

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

CSS (Cascading Style Sheets) provides a variety of units for expressing length and measurements, essential for defining styles such as padding, margins, and font sizes. These units are categorized into two main types: absolute length units and relative length units. Understanding these units and their appropriate usage is crucial for creating responsive and adaptive web designs that look consistent across different devices and screen sizes. This comprehensive guide covers both absolute and relative length units in CSS, complete with syntax, examples, and practical applications.

Absolute Length Units

Absolute length units are fixed and are not recommended for screen use due to varying device sizes. They are best used for print layouts and scenarios where the output medium is known.

Absolute Length Units and Syntax

1. cm: Centimeters

Syntax:

font-size: 0.5cm;
line-height: 0.1cm;

Example: This example illustrates the CSS units by specifying the length unit in cm.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>CSS units</title>
    <style>
        .gfg {
            font-size: 1.2cm;
            font-weight: bold;
        }
        
        .geeks {
            font-size: 0.5cm;
            line-height: 0.1cm;
        }
    </style>
</head>

<body>
    <div class="gfg">GeeksforGeeks</div>
    <div class="geeks">
        A computer science portal for geeks
    </div>
</body>

</html>

Output:

css units

2. mm: Millimeters

Syntax:

font-size: 5mm;
line-height: 1mm;

Example: This example illustrates the CSS units by specifying the length unit in mm.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>CSS units</title>
    <style>
        .gfg {
            font-size: 12mm;
            font-weight: bold;
        }
        
        .geeks {
            font-size: 5mm;
            line-height: 1mm;
        }
    </style>
</head>

<body>
    <div class="gfg">GeeksforGeeks</div>
    <div class="geeks">
        A computer science portal for geeks
    </div>
</body>

</html>

Output:

css units

3. in: Inches

Note: inches (1in = 96px = 2.54cm) 

Syntax:

font-size: 0.2in;
1line-height: 0.1in;

Example: This example illustrates the CSS units by specifying the length unit in inches.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>CSS units</title>
    <style>
        .gfg {
            font-size: .5in;
            font-weight: bold;
        }
        
        .geeks {
            font-size: .2in;
            line-height: .1in;
        }
    </style>
</head>

<body>
    <div class="gfg">GeeksforGeeks</div>
    <div class="geeks">
        A computer science portal for geeks
    </div>
</body>

</html>

Output:

css units

4. px: Pixels

Note: pixels (1px = 1/96th of 1in) 

Syntax:

font-size: 20px;
line-height: 10px;

Example: This example illustrates the CSS units by specifying the length unit in pixels.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>CSS units</title>
    <style>
        .gfg {
            font-size: 40px;
            font-weight: bold;
        }
        
        .geeks {
            font-size: 17px;
            line-height: 5px;
        }
    </style>
</head>

<body>
    <div class="gfg">GeeksforGeeks</div>
    <div class="geeks">
        A computer science portal for geeks
    </div>
</body>

</html>

Output:

css units

5. pt: Points

Note: points (1pt = 1/72 of 1in) 

Syntax:

font-size: 16pt;
line-height: 8pt;

Example: This example illustrates the CSS units by specifying the length unit in points.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>CSS units</title>
    <style>
        .gfg {
            font-size: 35pt;
            font-weight: bold;
        }
        
        .geeks {
            font-size: 15pt;
            line-height: 5pt;
        }
    </style>
</head>

<body>
    <div class="gfg">GeeksforGeeks</div>
    <div class="geeks">
        A computer science portal for geeks
    </div>
</body>

</html>

Output:

css units

6. pc: Picas

Note: picas (1pc = 12 pt) 

Syntax:

font-size: 1pc;
line-height: 0.5pc;

Example: This example illustrates the CSS units by specifying the length unit in pc.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>CSS units</title>
    <style>
        .gfg {
            font-size: 3pc;
            font-weight: bold;
        }
        
        .geeks {
            font-size: 1.3pc;
            line-height: .2pc;
        }
    </style>
</head>

<body>
    <div class="gfg">GeeksforGeeks</div>
    <div class="geeks">
        A computer science portal for geeks
    </div>
</body>

</html>

Output:

css units

Relative Length Units

Relative length units adjust based on the surrounding context, making them ideal for responsive designs that adapt to different screen sizes and devices.

Relative Length Units and Syntax

1. em: Relative to the font size of that element.

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

Syntax:

font-size: 10px;
line-height: 2em;

Example: This example illustrates the CSS units by specifying the length unit in em.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>relative unit</title>
    <style>
        p {
            font-size: 15px;
            line-height: 2em;
        }
    </style>
</head>

<body>
    <p>GeeksforGeeks Line height: 2x10px = 20px</p>

    <p>GeeksforGeeks Line height: 2x10px = 20px</p>

    <p>GeeksforGeeks Line height: 2x10px = 20px</p>
</body>

</html>

Output:

em unit

2. ex: Relative to the X(axis)-height of the current font.

Syntax:

font-size: 1ex;

Example: This example illustrates the CSS units by specifying the length unit in ex.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>relative unit</title>
    <style>
        p {
            font-size: 40px;
        }
        
        span {
            font-size: 1ex;
        }
    </style>
</head>

<body>
    <p>
        GeeksforGeeks:<span>A computer science
        portal for geeks</span>
    </p>

</body>

</html>

Output:

ex unit image

3. ch: Relative to the width of the 0.

Syntax:

font-size: 2ch;

Example: This example illustrates the CSS units by specifying the length unit in ch.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>ch unit in CSS</title>
    <style>
        body {
            font-size: 50px;
        }
        
        div {
            font-size: 1ch;
        }
    </style>
</head>

<body>
    <p>GeeksforGeeks</p>
    <div>
        A computer science portal for geeks
    </div>
</body>

</html>

Output:

ch unit

4. rem: Relative to the browser base font-size.

Syntax:

font-size: 3rem;

Example: This example illustrates the CSS units by specifying the length unit in rem.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>ch unit in CSS</title>
    <style>
        body {
            font-size: 4rem;
        }
        
        div {
            font-size: 1.6rem;
        }
    </style>
</head>

<body>
    <p>
        GeeksforGeeks
    </p>
    <div>
        A computer science portal for geeks
    </div>
</body>

</html>

Output:

ch unit

5. vw: Relative to 1% of the width of the viewport.

Syntax:

font-size: 10vw;

Example: This example illustrates the CSS units by specifying the length unit in vw.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>vw unit</title>
    <style>
        h1 {
            font-size: 4vw;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <p>
        A computer science portal for geeks
    </p>
</body>

</html>

Output:

unit image

6. vh: Relative to 1% of the height of the viewport.

Syntax:

font-size: 10vh;

Example: This example illustrates the CSS units by specifying the length unit in vh.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>vw unit</title>
    <style>
        h1 {
            font-size: 4vw;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <p>
        A computer science portal for geeks
    </p>
</body>

</html>

Output:

unit image

7. vmin: Relative to 1% of the viewport’s smaller dimension.

Syntax:

font-size: 10vmin;

Example: This example illustrates the CSS units by specifying the length unit in vmin.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>vmin unit</title>
    <style>
        h1 {
            font-size: 8vmin;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <p>A computer science portal for geeks</p>
    <p>1vmin = 1vw or 1vh whichever is smaller.</p>
</body>

</html>

Output:

vmin unit

8. vmax: Relative to 1% of the viewport’s larger dimension.

Syntax:

font-size: 20vmax;

Example: This example illustrates the CSS units by specifying the length unit in vmax.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>vmax unit</title>
    <style>
        h1 {
            font-size: 5vmax;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <p>A computer science portal for geeks</p>
    <p>1vmax = 1vw or 1vh whichever larger.</p>

</body>

</html>

Output:

vmax

9. %: % unit sets the font-size relative to the current font-size.

Syntax:

font-size: 250%;

Example: This example illustrates the CSS units by specifying the length unit in percentage.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>CSS unit</title>
    <style>
        body {
            font-size: 250%;
        }
        
        div {
            font-size: 17px;
        }
    </style>
</head>

<body>
    <p>GeeksforGeeks</p>
    <div>A computer science portal for geeks</div>
</body>

</html>

Output:

percentage unit

In conclusion, CSS offers a wide range of units for specifying lengths and measurements, each with its unique advantages and use cases. Absolute length units like cm, mm, in, px, pt, and pc are fixed and best used for print layouts or known output mediums. In contrast, relative length units like em, ex, ch, rem, vw, vh, vmin, vmax, and % are flexible and ideal for responsive web design. Understanding when and how to use these units allows developers to create versatile and adaptive layouts that provide a consistent user experience across different devices and screen sizes. Experiment with the provided examples to enhance your web design skills and improve your projects’ responsiveness.

Supported Browser:

  • Google Chrome 1
  • Microsoft Edge 3
  • Firefox 1
  • Opera 3.5
  • Safari 1

CSS Units – FAQs

What are CSS units?

CSS units are measurements used in CSS to define sizes, distances, or other properties in a webpage layout. They help determine how content is displayed on different devices with varying screen sizes.

What are the types of CSS units?

CSS units can be broadly categorized into two types:

  • Absolute units: These are fixed-size units that do not change based on other elements, such as pixels (px), points (pt), inches (in), centimeters (cm), and millimeters (mm).
  • Relative units: These units are relative to another length property, such as percentages (%), ems (em), rems (rem), viewport width (vw), and viewport height (vh).

When should you use percentage (%) units?

Percentage units are relative to the parent container’s size and are useful for creating fluid layouts where you want elements to scale based on the size of their container, such as in responsive designs.

How do CSS units affect responsiveness?

Using relative units like %, em, rem, vw, and vh can significantly enhance the responsiveness of a web design. These units adapt to different screen sizes and resolutions, allowing more flexibility than absolute units like pixels.

Are there any newer CSS units to consider?

Yes, newer units include the ch unit, which is based on the width of the “0” character of the font being used, and ex, based on the x-height of the font. These units can be useful for text-related measurements.



Next Article

Similar Reads

three90RightbarBannerImg