CSS – Measurement Units

By Published 9 min read

Certainly! In CSS, measurement units play a crucial role in defining the size and dimensions of various elements on a web page. Understanding these units is essential for precise control over your web page’s layout and design. Here are some of the common measurement units used in CSS:

1. Pixels (px):

Pixels are the most basic unit of measurement in CSS. A pixel is a single dot on a screen and is commonly used for defining fixed sizes, such as font sizes and element dimensions. For example, font-size: 16px; sets the text size to 16 pixels.

Let’s explore pixels with an example:

Example: Setting Font Size with Pixels

Suppose you want to define the font size for the text in a paragraph on your website. You decide that you want the text to be relatively large and easy to read. Here’s how you would use pixels to set the font size:

<!DOCTYPE html>
<html>
<head>
    <title>Document</title>
    <style>
        p {
            font-size: 18px;
        }
    </style>
</head>
<body>
    <p>Hello Developers!</p>
</body>
</html>Code language: HTML, XML (xml)

Output :

In this example:

  • p is the selector, which targets all the <p> elements on your web page.
  • font-size is the CSS property that determines the size of the font.
  • 18px is the value assigned to the font-size property. This means that the text inside all <p> elements will be displayed at a size of 18 pixels.

By using pixels as the unit of measurement for font size, you have precise control over the text’s appearance. The text in your paragraphs will consistently display at 18 pixels in size, regardless of the user’s screen size or device. This can help ensure readability and maintain a consistent design across various parts of your website.

Pixels are also commonly used for defining the width, height, margins, and padding of elements. For example, you can set the width of an image to 300px, add 10px of margin to a button, or define the padding of a container as 20px. Pixels provide a fixed, absolute measurement that is especially useful when you need elements to maintain a specific size or spacing regardless of the user’s device or screen resolution.

2. Em (em):

The “em” unit is relative to the font size of the parent element. If the parent element’s font size is 16px, `1em` is equivalent to 16 pixels. Using “em” allows for scalable designs based on the parent element’s font size.

Example: Setting Text Size with “em”

Suppose you’re designing a blog website, and you want the text within your articles to have a consistent and easily adjustable font size. You decide to use the “em” unit to make the text size relative to the base font size of the entire page. Here’s how you would do it:

<!DOCTYPE html>
<html>
<head>
    <title>Document</title>
    <style>
        body {
            font-size: 16px;
            /* Set the base font size for the entire page to 16 pixels. */
        }
        .article {
            font-size: 1.2em;
            /* Set the font size for article content to 1.2 times the parent's font size. */
        }
    </style>
</head>
<body>
    <p>Hello Developers!</p>
    <div class="article">
        This is a article.
    </div>
</body>
</html>Code language: HTML, XML (xml)

Output :

In this example:

  1. body is the selector, and it sets the base font size for the entire webpage to 16px. This becomes the reference point for all font sizes on the page.
  2. .article is a class selector that targets the content within your articles. By setting font-size to 1.2em, you are instructing the browser to make the text within the .article element 1.2 times larger than the font size inherited from its parent, which is the body.

With this setup, if you decide to change the base font size of the entire webpage to 18px later, all the text within the .article elements will automatically adjust to 1.2em of the new base font size. This relative approach makes it easier to maintain consistent typography throughout your website, especially when dealing with various sections or elements that require different font sizes.

Using “em” units allows for scalability and adaptability, making your web design more responsive to different devices and user preferences. It’s particularly useful when you want to maintain a harmonious typography hierarchy across your web pages.

3. Rem (rem):

Similar to “em,” the “rem” unit is relative to the root element (usually the <html> tag), making it more predictable than “em.” This is especially useful for maintaining consistency in designs across various parts of a web page.

Example: Setting Margins with “rem”

Imagine you’re designing a website’s layout, and you want to define consistent margins for various sections and elements, ensuring they adapt smoothly when you decide to change the overall layout. Here’s how you would use the “rem” unit:

<!DOCTYPE html>
<html>
<head>
    <title>Document</title>
    <style>
        html {
            font-size: 16px;
            /* Set the base font size for the entire page to 16 pixels. */
        }

        .section {
            margin: 1.5rem;
            /* Set a margin of 1.5 times the root font size for all elements with the class 'section'. */
        }
    </style>
</head>
<body>
    <p>Hello Developers!</p>
    <div class="section">
        This is a Section.
    </div>
</body>
</html>
Code language: HTML, XML (xml)

Output :

In this example:

  1. html is the selector, and it sets the base font size for the entire webpage to 16px. This becomes the reference point for all font sizes and “rem” units on the page.
  2. .section is a class selector that targets various sections or elements within your web page. By setting the margin to 1.5rem, you’re instructing the browser to create a margin around these elements that is 1.5 times the base font size defined at the root element (html).

The beauty of using “rem” units is that if you later decide to change the base font size for your entire webpage to 18px, all the margins within the .section elements will automatically adjust to 1.5rem of the new base font size. This consistent scaling ensures that your layout remains well-proportioned and responsive to changes in font size, making it easier to maintain a harmonious design.

“Rem” units are especially beneficial for maintaining a consistent and adaptable layout in responsive web design, as they are not influenced by the font sizes of parent elements. They provide a reliable way to control spacing and dimensions throughout your web page.

4. Percentage (%):

Percentages are often used for specifying dimensions relative to the parent element. For example, setting a width of 50% on an element makes it half the width of its parent container.

Example: Creating a Fluid Layout with Percentage

Imagine you’re designing a simple two-column website layout. You want the left column to occupy 30% of the page’s width, while the right column should take up the remaining 70%. Here’s how you would use percentages to achieve this:

<!DOCTYPE html>
<html>
<head>
    <title>Document</title>
    <style>
        .container {
            width: 100%;
            /* Set the container's width to 100% of its parent's width. */
        }

        .left-column {
            width: 30%;
            /* Set the left column's width to 30% of the container's width. */
            float: left;
            /* Use float to position the columns side by side. */
            height: 50px;
            background-color: aqua;
        }

        .right-column {
            width: 30%;
            /* Set the right column's width to 70% of the container's width. */
            float: left;
            /* Float the right column for side-by-side positioning. */
            height: 50px;
            background-color: blueviolet;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="left-column"></div>
        <div class="right-column"></div>
    </div>
</body>
</html>Code language: HTML, XML (xml)

Output :

In this example:

  1. .container is a class selector that represents a container element containing the two columns. By setting its width to 100%, you ensure that it spans the entire width of its parent element, which could be the web page itself.
  2. .left-column and .right-column are class selectors for the two columns. By setting their widths to 30% and 70%, respectively, you specify that the left column should occupy 30% of the container’s width, while the right column should occupy 70%. Using percentages here ensures that the columns adapt proportionally to the container’s size.
  3. The float: left; property is used to position the columns side by side within the container.

This flexible layout adjusts gracefully to different screen sizes or window widths. If the parent container’s width changes, the columns’ widths will automatically adjust according to the specified percentages. This makes it an excellent approach for creating responsive designs that work well on various devices.

Percentages are also frequently used for defining heights, padding, margins, and other CSS properties. They provide a valuable tool for creating fluid and adaptable web layouts that respond to the dynamic nature of the web.

5.Viewport Units (vw, vh, vmin, vmax):

Viewport units are relative to the size of the viewport (the browser window).

  • vw: Represents 1% of the viewport’s width.
  • vh: Represents 1% of the viewport’s height.
  • vmin: Represents 1% of the smaller of the viewport’s width and height.
  • vmax: Represents 1% of the larger of the viewport’s width and height. These units are useful for creating responsive designs that adapt to different screen sizes.

Example: Creating a Responsive Full-Screen Header with Viewport Units

Imagine you want to create a header section for your website that spans the entire width and height of the user’s viewport, ensuring it looks great on various screen sizes. Here’s how you can use viewport units to achieve this:

<!DOCTYPE html>
<html>
<head>
    <title>Document</title>
    <style>
        .header {
            width: 100vw;
            /* Set the header's width to 100% of the viewport width. */
            height: 100vh;
            /* Set the header's height to 100% of the viewport height. */
            background-color: #3498db;
            /* Add a background color for styling. */
            color: #fff;
            /* Set the text color to white for better visibility. */
            text-align: center;
            /* Center-align text content. */
            padding: 20px;
            /* Add some padding for better spacing. */
            font-size: 3vw;
            /* Set the font size to 3% of the viewport width for responsiveness. */
        }
    </style>
</head>
<body>
    <div class="header">
        This is a Header.
    </div>
</body>
</html>Code language: HTML, XML (xml)

Output :

In this example:

  1. .header is a class selector representing the header section of your website.
  2. width: 100vw; sets the width of the header to 100vw, which means it spans 100% of the viewport’s width.
  3. height: 100vh; sets the height of the header to 100vh, which means it spans 100% of the viewport’s height.
  4. background-color, color, and other properties are used for styling to make the header visually appealing.
  5. font-size: 3vw; sets the font size to 3vw, which means the text inside the header will be 3% of the viewport width. This ensures that the text remains readable and proportional to the viewport size, making it responsive.

By using viewport units, your header will adapt automatically to different screen sizes and orientations. If the user resizes their browser window or views the page on various devices, the header will always cover the entire viewport, maintaining a visually consistent and responsive design.

Viewport units are especially valuable for creating components that need to adjust to the available screen real estate, making them an essential part of modern web design for responsive layouts.

6.Flex (fr):

The “fr” unit is used in CSS Grid and Flexbox layouts to distribute available space among elements proportionally. For example, if you have two columns, and one is set to 1fr while the other is set to 2fr, the second column will get twice as much space as the first.

Example: Creating a CSS Grid Layout with “fr” Units

Suppose you’re designing a web page that features a grid layout with three columns, where the first and third columns should take up one fraction of the available space each, and the second column should take up two fractions. Here’s how you would use the “fr” unit to achieve this:

<!DOCTYPE html>
<html>
<head>
    <title>Document</title>
    <style>
        .grid-container {
            display: grid;
            grid-template-columns: 1fr 2fr 1fr;
            /* Create three columns with 1fr, 2fr, and 1fr width respectively. */
            grid-gap: 10px;
            /* Add a 10px gap between grid items for spacing. */
        }

        .grid-item {
            background-color: #3498db;
            /* Add background color for styling. */
            color: #fff;
            /* Set text color to white for better visibility. */
            padding: 20px;
            /* Add padding for spacing. */
            text-align: center;
            /* Center-align text content. */
        }
    </style>
</head>
<body>
    <div class="grid-container">
        <div class="grid-item">
            This is a grid 1.
        </div>
        <div class="grid-item">
            This is a grid 2.
        </div>
        <div class="grid-item">
            This is a grid 3.
        </div>
    </div>
</body>
</html>Code language: HTML, XML (xml)

Output :

In this example:

  1. .grid-container is a class selector representing a container that will use CSS Grid for layout.
  2. display: grid; is used to define the container as a CSS Grid container.
  3. grid-template-columns: 1fr 2fr 1fr; defines the three columns within the grid. The first and third columns each occupy one fraction of the available space (1fr), while the second column takes up two fractions (2fr). This creates a flexible grid layout where the middle column is twice as wide as the others.
  4. grid-gap: 10px; adds a 10px gap between grid items for spacing and aesthetics.
  5. .grid-item is a class selector that represents the individual grid items within the container. Styling properties like background color, text color, padding, and text alignment are applied for visual appeal.

With this setup, the columns in your grid layout will automatically adjust their widths based on the available space in the container. The “fr” unit ensures that the second column takes up twice the space of the other columns, maintaining a balanced and responsive grid layout.

The “fr” unit is valuable for creating grid-based designs that adapt to varying content sizes or screen widths. It provides a dynamic and proportional way to distribute space within your grid layout, making it an essential tool for modern web design.

Understanding and effectively using these measurement units in CSS will enable you to create responsive and visually appealing web designs that adapt to various devices and screen sizes while maintaining a consistent layout and design.

Khan Waseem is a tech lead and full-stack engineer in Bengaluru, building hospitality and SaaS software with Node.js, React and React Native. See the resume