Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

em — a font-size-relative length unit

The em unit expresses a length relative to computed font-size. In font-size it uses the parent's size; in properties such as padding it uses the element's own size.

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

Syntax

<number>em

em is a font-relative length unit. Write a number directly before the unit, such as 0.5em. Normally, 1em equals the element's computed font-size, not the width of the letter “m”.

In font-size and other font-affecting properties, the reference is the parent's font metrics. If there is no parent, metrics corresponding to initial values are used. In contrast, padding: 0.5em uses the element's own font size.

In the example, .parent has 20px, so font-size: 1.5em gives .child a size of 30px. Its padding: 0.5em is therefore 15px, not 10px.

Repeating font-size: 1.5em on successive nested elements multiplies the size at each level. By contrast, rem refers to the document root element's font size.

Examples

<!doctype html>
<meta charset="utf-8">
<style>
  .parent { font-size: 20px; }
  .child { font-size: 1.5em; padding: 0.5em; }
</style>
<div class="parent">
  <div class="child">em</div>
</div>
<script>
  const child = document.querySelector('.child');
  const style = getComputedStyle(child);
  console.log(style.fontSize);
  console.log(style.paddingTop);
</script>

Result

30px
15px

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