đź’»
HTML/CSS
  • Overview
  • How the Web Works
    • DNS, TCP/IP, HTTP, HTML
    • Anatomy of a Web Page
    • How a Web Page Gets Loaded
  • HTML/CSS intro
    • HTML
      • Boilerplate HTML
      • HTML Hierarchy
      • Meta Section
      • Text-formatting Elements
      • Image
      • Video
      • Anchor
      • Tables
      • Semantic Elements
    • CSS
      • Properties
      • Selectors
      • Box-Model
      • Block vs Inline
      • The Cascade
    • Layout
      • Container Elements
      • Grid vs Flexbox
      • Flexbox
      • Grid
  • Miscellaneous Topics
    • Background Image
    • Border-radius
    • Box-shadow
    • Centering Elements
    • Colors
    • Custom Properties
    • Forms
    • Typography
    • Units
  • Appendix
    • Google Fonts
    • Lorem Ipsum
    • HTML Cheatsheet
    • CSS Cheatsheet
    • Browser Support for HTML/CSS
    • unsplash.com (free images)
    • pexels.com (free images)
    • Chrome Default Stylesheet
  • Further Learning Resources
    • FE Mentor Challenges
    • Free Scrimba Courses
    • Kevin Powell Youtube
Powered by GitBook
On this page
  • Absolute Units (px)
  • Font-relative Units (em and rem)
  • Resources

Was this helpful?

  1. Miscellaneous Topics

Units

PreviousTypographyNextGoogle Fonts

Last updated 3 years ago

Was this helpful?

There are two types of units: absolute and relative units.

  • Absolute units are always the same size, even when the user zooms in on the browser.

  • Relative units are based on the size of something else, such as the font size or viewport size. When the user zooms in the browser, elements using relative units will increase in size too.

The following table contains some of the most commonly used CSS units.

Unit

Relative or absolute

Description

rem

Relative

1rem is equal to the font size of the root element. If the root font size is 16px, then 1rem = 16px and 0.5rem = 8px

em

Relative

1em is equal to the font size of the parent element. If the parent font size is 16px, then 1em = 16px and 0.5em = 8px

%

Relative

100% is equal to the full width or height of the parent element

px

Absolute

1px is 1/96th of 1 inch

There are many CSS properties whose value represents a length. For example, width, margin, padding, font-size all expect a length value. But lengths can be expressed in different units and it is important to know the differences between them.

If you want to view all CSS units, visit the .

Some users who find it difficult to read small text will use the browser zoom to increase the content size. Since absolute units do not increase in size with browser zoom, we should use relative units whenever possible. Therefore, we should prefer using rem and %, and avoid using px.

Absolute Units (px)

Absolute units are not relative to anything else and generally stay the same size. They are not affected by any screen size or fonts.

Pixels - px (1px = 1/96th of 1 inch) are the absolute unit that is used in web design. Pixels are very easy to use, because they specify the exact length. But they are not a good choice for accessibility. For example, if you increase your font to large in your browser settings, your hard-coded absolute font-sizes will not change.

Font-relative Units (em and rem)

em - Em inherits size from its immediate parent’s font size. Say, if a parent element has font-size:18px, then 1em will be measured as 18px for all its child’s.

The advantage of using em is, if you decide to change the font-size, padding, and margin of each element proportionately, then you just have to change the parent element font size and all other elements will adjust accordingly.

That won’t be the case with absolute units like px, where you have to adjust each element individually.

rem - rem units always refer to the root element font size, not the parent element.

Resources

MDN CSS values and units page
How to Choose the Best CSS Unit to Create Better Site LayoutsWP Engine
Logo