Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

rem — a unit relative to the root font size

The rem unit expresses a length relative to the document root element’s computed font-size. It lets text sizes and spacing share one reference size, independently of the parent element’s font size.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

<number>rem

rem is a relative length unit. In ordinary declarations, 1rem equals the computed font-size of the root element, which is html in HTML. With font-size: 20px on that element, 0.5rem is 10px and 2rem is 40px. Write the number directly before the unit, without whitespace.

Unlike em, which normally refers to the current element’s font size, rem uses a shared root reference. For font-size itself, em refers to the inherited font size. Nesting elements with font-size: 1.2rem therefore does not repeatedly multiply their sizes.

An exception prevents a circular dependency: in the root element’s font-size, rem refers to the initial font size. Outside an element context, such as in media queries, the reference also comes from initial values rather than the :root declaration. Do not assume that 1rem always equals 16px.

In the example, .parent has a 30px font size, but .box uses the 20px reference: its font size is 20px, its content width is 200px, and each padding side is 10px. Setting the reference in px makes this demonstration predictable; it is not a requirement for using rem.

Examples

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>rem example</title>
<style>
  :root { font-size: 20px; }
  .parent { font-size: 30px; }
  .box {
    font-size: 1rem;
    width: 10rem;
    padding: 0.5rem;
    background: lightblue;
  }
</style>
</head>
<body>
<div class="parent">
  <div class="box">rem example</div>
</div>
</body>
</html>

Result

.box: font-size = 20px; width = 200px; padding-top = 10px; padding-right = 10px; padding-bottom = 10px; padding-left = 10px

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources