Skip to main content

CSS Absolute Units

A length given in an absolute unit is fixed: it always displays at exactly that size.

Screen size has no effect on these units, which is why they are not recommended for websites. They can still be useful when the output medium is known, print layout being one case.

Of all of them, px (pixels) is the one used most.

The full set of absolute CSS units:

  • px — Pixels, the top choice for screens (1px = 1/96th of 1in).
  • cm — Centimeters, mainly for print stylesheets.
  • mm — Millimeters, mainly for print stylesheets.
  • in — Inches, mainly for print stylesheets (1in = 96px = 2.54cm).
  • pt — Points, a unit from typography (1pt = 1/72 of 1in).
  • pc — Picas, a unit from print (1pc = 12pt).

Set Font Size With Px

Sizing text in px (pixels) gives you complete control over how large it appears.

The trade-off is that the page may not scale well across screen sizes, and users cannot change the text size through their browser settings. The zoom tool still works, though, and it resizes the whole page.

Note: The number and the unit must not be separated by a whitespace. With a value of 0, you can leave the unit out.

Example

Font sizes given in px:

Prompt
Create a page that sets the font size of h1 to 40px, h2 to 30px, and p to 17px. Add sample heading and paragraph text to demonstrate the sizes.
Output: complete HTML page

When to Use Absolute Units

Absolute units suit situations such as these:

  • The dimensions have to be precise and fixed.
  • The design is meant for print media.
  • An element must not scale with the viewport.

Tip: Relative units like em or rem give better responsiveness and are worth considering for most web design work.