Working with relative units: %, em, rem, vh, vw
CSS units of measurement play a crucial role in web design, determining the size and layout of elements on a webpage. This video tutorial covers the most common CSS units, including pixels, percentages, viewport width/height, rems, and ems. Understanding these units is essential for creating responsive and accessible websites.
Lets Go!

Working with relative units: %, em, rem, vh, vw
Lesson 29
Understand the differences between relative units (%, em, rem, vh, vw) and when to use each to build responsive and scalable layouts.
Get Started 🍁Introduction to CSS Units of Measurement
Welcome to "Introduction to CSS Units of Measurement"! In this course, we will delve into the fundamental aspects of CSS units, exploring their usage and application in web design.
CSS offers a variety of units of measurement, ranging from absolute sizes to relative sizes. Understanding these units is essential for creating responsive and visually appealing websites.
Have you ever wondered how to precisely control the size of elements on your webpage? Or how to ensure your design remains consistent across different screen sizes? This course will equip you with the knowledge and skills to effectively utilize CSS units in your web development projects.
Throughout this course, we will cover commonly used CSS units such as pixels (px), percentages (%), viewport width (vw), viewport height (vh), root em (rem), and em (em). You will learn how each unit functions and when to apply them for optimal results.
By the end of this course, you will have a solid understanding of CSS units of measurement, enabling you to create more responsive and accessible web designs. Join us on this learning journey and enhance your web development skills!
Let's dive into the diverse world of CSS units and discover the endless possibilities they offer in creating dynamic and responsive web layouts. Are you ready to elevate your web design skills? Let's get started!
Main Concepts of CSS Units of Measurement
-
Absolute Sizes vs Relative Sizes: CSS units of measurement are broken into absolute sizes and relative sizes. Absolute sizes are specific values, while relative sizes are based on something else, such as its parent, font size, or other elements.
-
Pixels (px): Pixels are an absolute unit of measurement in CSS. When defined in pixels, the size will always remain the set value, whether for width or font size.
-
Percent (%): Percent is a relative unit of measurement in CSS. When set to a percentage value, it is relative to the size of its parent element. It can be used for both width and font size.
-
Viewport Width (vw) and Viewport Height (vh): VW and VH are similar to percent, but they are based on the viewport width and height of the screen respectively. They do not take padding and margin into account, making them useful for responsive web design.
-
Root Em (rem): Rem is based on the root font size (usually 16 pixels) of the HTML document. It is a multiplier of the default font size, making it easier to maintain consistency across different elements on a webpage.
-
Em (em): Em is also relative to font size, but it is based on the font size of the parent element. This can lead to compounding font sizes when nested within multiple elements, making it less predictable than rem. It is recommended to use rem over em for better accessibility and responsiveness.
By understanding these main concepts and when to use each unit of measurement, you can effectively style and structure your web pages with CSS.
Practical Applications of CSS Units of Measurement
Using Pixels for Absolute Values
- Open your preferred text editor or IDE.
- Create a basic HTML page with a
div
element having the classpx
. - Define a border for the
div
to make it visible. - Set the width of the
div
to200 pixels
. - Set the font size to
20 pixels
. - Save the changes.
- Observe how the size stays fixed in pixels.
Utilizing Relative Units
- Add another class called
percent
. - Set the width of this
div
to50%
. - Notice how the width is relative to its parent.
- Apply percentages to font sizes as well.
- Experiment with viewport width (
vw
) and viewport height (vh
) units. - Understand how
vw
andvh
units differ from percentages. - Create elements using
rem
andm
units. - Compare the subtle differences between
rem
andm
units.
Interactive Exercise:
- Try adjusting the values of
rem
andm
units to understand their impact on the elements.
Designing for Responsiveness with Relative Units
- Set percentage,
vw
, andvh
units to create responsive designs. - Explore how the width and height adjust based on viewport size.
- Experiment with various settings to see the impact on responsiveness.
Interactive Exercise:
- Create a full-page section using
100vh
to make it adapt to viewport height.
Enhancing Accessibility and User Experience
- Use
rem
for font sizes to ensure adaptability to user preferences. - Compare the behavior of
rem
andm
units when adjusting font sizes. - Apply
rem
for padding and margin for consistent spacing. - Test how elements respond to changes in user font size settings.
- Note the impact of choosing the right unit on accessibility and responsiveness.
Interactive Exercise:
- Experiment with changing font sizes in user settings and observe the effect on elements styled with
rem
andm
.
Conclusion
- Implement
rem
for font sizes, padding, and margin for better accessibility and responsiveness. - Share your experiences and any questions in the comments section.
- Like the video if you found it helpful and consider subscribing for more content.
- Keep practicing and experimenting with CSS units to enhance your web design skills.
Test your Knowledge
What does the em unit refer to?
What does the em unit refer to?
Advanced Insights into CSS Units of Measurement
In this section, we will delve deeper into CSS units of measurement, focusing on advanced aspects and insights that can enhance your understanding of this topic.
Relative vs. Absolute Sizes
- Absolute Sizes: Absolute values are fixed and do not change. They are defined explicitly, such as using pixels (
px
). - Relative Sizes: Relative values are dependent on another factor, like the parent element or viewport. They include units like percentages (
%
), viewport width (vw
), viewport height (vh
), root ems (rem
), and ems (em
).
Relative Units Explained
- Percentages (%): A percentage value is a fraction of another value, like the parent element. It is commonly used for both width and font sizes.
- Viewport Width (vw) and Viewport Height (vh): These units are similar to percentages but base their calculations on the entire screen's width or height.
- Root EMs (rem):
rem
units are based on the root<html>
font size, ensuring scalability and accessibility. - EMs (em):
em
units are relative to the font size of the element's parent, potentially leading to compounding effects in nested elements.
Recommendations for Usage
- Accessibility: Use
rem
for font sizes, padding, and margin to ensure compatibility with user preferences and accessibility settings. - Responsiveness: Utilize relative units for responsive design, such as using
vw
andvh
for dynamic layouts. - Avoiding Compounding Issues: Be cautious when using
em
for nested elements to prevent exponential increases in font sizes.
Curiosity Question
- How can the choice between
rem
andem
impact the scalability and accessibility of a website's design?
By incorporating these advanced insights into your CSS practices, you can create more flexible and user-friendly web designs. Explore further applications and experiment with different units to master the art of CSS measurement techniques.
Additional Resources for CSS Units of Measurement
- W3Schools CSS Units - A comprehensive guide to CSS units with examples.
- MDN Web Docs: Font Size - In-depth information about setting font sizes using different units.
- CSS-Tricks: Width and Height in Percentages - Explains the differences between percentage, vw, and vh units for width and height.
- CSS Tricks: Understanding CSS Units - A detailed comparison of different CSS units like em, rem, px, etc.
- A List Apart: Flexible Typesetting - An article on using relative units for better responsive design.
Explore these resources to enhance your understanding of CSS units and improve your web design skills. Happy learning!
Practice
Task: Create a layout where font sizes use em and rem, widths use %, and sections adjust height using vh units.