Understanding Different Units in CSS: A Beginner’s Guide with Real-World Analogies

Wasiu Akindoyin
8 min readApr 13, 2024

--

· Introduction
· Absolute Units: The Inch Ruler of the Web
1. Pixels (px): The Building Blocks
2. Inches (in), Centimeters (cm), Millimeters (mm):
· Relative Units: The Flexible Measuring Tape
1. Em (em):
2. Rem (rem):
3. Percentages (%): The Responsive Recipe
· Viewport Units: The Magic Measuring Wand
1. Viewport Width (vw):
2. Viewport height (vh):
· Choosing the Right Unit: The Toolbox Analogy
· Conclusion

Introduction

Picture yourself as a web designer building a beautiful website. But how do you ensure everything fits perfectly, elements are properly aligned, and the layout looks good on every device? This is where units in Cascading Style Sheets (CSS) play a crucial role, and at first glance, they may seem like a confusing mix of letters (px, rem, em, %).

When it comes to designing and styling elements on a webpage, understanding units in CSS is important. These units are used to define measurements for properties like width, height, margin, padding, font size, and others. However, fear not! This article will break them down using simple analogies, making you a unit master in no time.

Absolute Units

Consider these units like a reliable ruler. They define sizes as fixed measurements, much like inches on a ruler. Absolute units include the following:

1. Pixels (px)

Pixels (px) are the most basic unit of measurement in CSS. They represent the individual dots on your screen, acting like tiny building blocks for defining the size and position of elements. Imagine your website as a giant Lego set; each pixel is a single Lego brick. While pixels offer precise control, they may exhibit inconsistencies across devices with different screen resolutions.

Code Example:

.box {
width: 200px; /* Set the width of the box to 200 pixels */
height: 150px; /* Set the height of the box to 150 pixels */
}

In the code example above, the .box class always takes up 200 Lego bricks (200px) horizontally and 150 bricks vertically (150px), regardless of the screen size. For example, if you set an element’s width to 200px and its height to 150px, it will be 200 pixels wide and 150 pixels tall on any screen, but the physical size of the element may appear different on screens with different resolutions. It’s like using a rubber band ruler, where the size may stretch or shrink depending on the screen’s resolution.

2. Inches (in), Centimeters (cm), Millimeters (mm):

Inches (in), Centimeters (cm), and Millimeters (mm) are length units in CSS that represent physical measurements. These units are like real-world measurements. They are useful for creating designs that need to be accurately sized for print or when dimensions need to be specified in real-world units. For instance, setting a border to be 1cm thick is like drawing a line that is 1 centimeter long on a piece of paper.

Code Example:

.box {
width: 2in; /* Set the width of the box to 2 inches */
height: 5cm; /* Set the height of the box to 5 centimeters */
border: 3mm solid blue; /* Set a 3 millimeter solid blue border around the box */
}

In the code example above, the .box class sets the width of the element to 2in (2 inches) and the height to 5cm (5 centimeters). This means the box will be 2 inches wide and 5 centimeters tall.

While Inches (in), Centimeters (cm), and Millimeters (mm) might seem familiar for physical measurements, they are not ideal for defining element sizes in CSS due to limitations in screen rendering and responsiveness. Consider using pixels, percentages, or ems/rems for better web development practices.

Relative Units:

Unlike the ruler, these units adjust based on their surroundings, making them more adaptable for responsive design. Imagine a flexible measuring tape that adjusts depending on what you’re measuring.

1. Em (em):

EMs (short for em) are CSS units that define the element’s sizes relative to the parent element’s font size. They allow you to create proportional relationships between elements within your website’s hierarchy. Imagine nesting dolls of different sizes. The parent element’s font size acts like the size of the largest doll.

EMs allow you to define the size of child elements relative to the parent. For instance, a child element with font-size: 2em; would be twice the size of its parent. Another child element with font-size: 0.5em; would be half the size of its parent.

Code Example:

/* Set base font size for the body */
body {
font-size: 16px;
}

/* Define a parent element with a larger font size */
.parent {
font-size: 20px;
}

/* Define child elements using EMs relative to the parent */
.child-1 {
font-size: 1.5em; /* 1.5 times the size of the parent (30px) */
}

.child-2 {
font-size: 0.75em; /* 0.75 times the size of the parent (15px) */
}

In the code example above, the base font size for the document body is set to 16px (16 pixels). The .parent element has a larger font size of 20px (20 pixels). The .child-1 element uses 1.5em for its font size, which translates to 1.5 * 20px = 30px (1.5 times the size of its parent). The .child-2 element uses 0.75em, resulting in a font size of 0.75em * 20px = 15px (0.75 times the size of its parent).

2. Rem (rem):

Rems (short for root rem) is a CSS unit that represents the root element’s font size. Similar to em, but it takes the font size of the root element (usually the HTML tag) as its base, making it consistent throughout your entire website. REMs allow you to define other element sizes (like heading sizes) relative to this base size.

Code Example:

/* Set base font size */
html {
font-size: 16px;
}

/* Define heading size using REM */
h1 {
font-size: 2rem; /* Twice the base font size */
}

/* Define paragraph size using REM */
p {
font-size: 1rem; /* Same as base font size */
}

In the code example above, we set the base font size for the entire website to 16px using html {font-size: 16px; }. The h1 heading is defined to be twice the base font size by settingfont-size: 2rem;. This means if the base font size is 16px, the h1 heading will be 2 * 16px = 32px. The p paragraph text maintains the base font size by setting font-size: 1rem;. This keeps the body text consistent with the overall font size.

3. Percentages (%):

Percentages (%) are a powerful unit in CSS that defines relative sizes, providing flexibility and responsiveness in website design. Imagine cutting a cake. A 20% slice is always 20% of the whole cake, no matter how big or small the cake is.

Percentages are great for defining margin, padding, and other elements that should scale proportionally with the overall layout. It’s like using a measuring tape to mark percentages on a cake, ensuring everyone gets a fair share.

Code Example:

.container {
width: 80%; /* Set the width of the container to 80% of the viewport width */
margin: 0 auto; /* Center the container horizontally */
}

.box {
width: 20%; /* Set the width of the box to 20% of the container's width */
float: left; /* Positions the box to the left */
}

In the code example above, the.container class defines a content area that takes up 80% of the viewport width using the percentage unit within its container. This means the container will take up 80% of the available width of the viewport. The .box class takes up 20% of the available space and floats to the left.

Using % for sizing elements allows them to scale proportionally based on the size of their parent container or the viewport, making it easier to create designs that work well on different devices and screen sizes.

Viewport Units:

These units act like magic wands, adjusting to the size of the user’s screen (viewport). Imagine a measuring tape that automatically adapts its scale based on the size of the window it occupies.

1. Viewport Width (vw):

Viewport width (vw) is a unit in CSS that defines the size of an element relative to the width of the user’s viewport. Imagine a website layout like a movie screen. The viewport width acts like the actual width of the movie screen itself. By using viewport units, elements on your website can adapt their size based on the user’s screen size, just like a movie adjusts to fit different screen dimensions while maintaining its aspect ratio.

Code Example:

.box {
width: 50vw; /* Set the width of the box to 50% of the viewport's width */
background-image: url("background.jpg");
background-size: cover;
}

In the code example above, the .box class width is set to 50vw, which is 50% of the viewport’s width. This means the box will be half as wide as the viewport, regardless of the user’s screen size. The background image will also be sized using the cover property, ensuring it fills the entire box area proportionally.

However, using viewport units excessively can lead to layout issues on very small screens. It’s recommended to use viewport units strategically in combination with other units, like px or % for optimal website responsiveness.

2. Viewport height (vh):

Viewport height (vh) is another unit in CSS that defines the size of an element relative to the height of the user’s viewport. Just like viewport width (vw), this unit allows for responsive websites that adapt to various screen sizes. Imagine a website layout like a portrait-oriented phone screen.

The viewport height acts like the height of the visible area on the phone. Using viewport height ensures elements adjust their size based on the user’s screen height, adapting the layout for both portrait and landscape orientations.

Code Example:

.box {
height: 50vh; /* Set the height of the box to 50% of the viewport's height */
margin: 0 auto; /* Centers the content horizontally */
}

In the code example above, the .box class is set to 50vh, which is 50% of the viewport’s height. This means the box will be half as tall as the available viewport height, regardless of the user’s screen size and orientation. The margin: 0 auto; property centers the content horizontally within the .box class.

However, similar to viewport width, using viewport height excessively can lead to layout issues on very small screens. It’s recommended to use viewport units strategically and in combination with other units, like px or %, for optimal website responsiveness.

Choosing the Right Unit: The Toolbox Analogy

Just like you wouldn’t use a hammer to screw in a nail, the choice of the appropriate unit depends on the specific task. Use px for precise layouts and icons, use em/rem for responsive design and consistent font sizes, use vw, vh, or % for elements that should scale with the screen size.

Keep in mind that practice makes perfect! Experiment with different units to observe their impact on your website’s layout. With a little understanding and these simple analogies, you’ll become a CSS unit expert in no time!

Conclusion

It is important to understand and use the appropriate units in CSS to design well-structured and adaptable web pages. Just like in the real world, where we use various units of measurement for different purposes, by drawing comparisons to real-world analogies, you can understand the purpose and functionality of each unit, making it easier to apply them effectively in your CSS stylesheets.

If you find this article helpful, please clap, share, and follow me to learn more about front-end development, blockchain networks, and a whole lot more. Thank you for sticking around till the end, and see you on the next one.

--

--

Wasiu Akindoyin

Front-end Developer | Technical Writer | Simplifying complex software concepts through code and real life analogies.