Favorites

CSS <length> Data Type

<length>
CSS Data TypeThis data type is supported by all modern browsers.
Updated 21st November 2026

The <length> data type is a distance, written as a number with a unit such as px, em, rem, %, vw, ch or cm. It sets sizes, spacing and offsets.

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.

Description

The <length> CSS data type represents a measurement of distance or size in CSS. It can be used to define the dimensions of elements, spacing between elements, font sizes, and more. A <length> value can be expressed in absolute units such as pixels (px), centimeters (cm), millimeters (mm), points (pt), and inches (in), or in relative units like ems (em), root ems (rem), viewport width (vw) and viewport height (vh). Percentages are a separate type, <percentage>, although many properties accept both as a <length-percentage>. Absolute units are fixed and do not scale with the user’s settings or viewport, whereas relative units adjust according to context such as the parent element’s font size or the viewport size.

The <length> type is frequently used with CSS properties that expect a measurement, such as width, margin, padding, font-size, and border-width. Because it can accept both absolute and relative values, <length> is highly versatile for responsive design, allowing developers to scale elements consistently across different screen sizes.

When using <length> in CSS, it’s important to understand context-dependent behavior. For example, percentage values often relate to the dimensions of a containing element, whereas em-based values are relative to the font size of the current element. Some CSS functions, like calc(), also work seamlessly with <length> values, enabling dynamic calculations of dimensions.

Examples:

/* Absolute lengths */
div.box {
  width: 300px;
  height: 150px;
  border-width: 2pt;
}

/* Relative lengths */
p.text {
  font-size: 1.2em;
  margin-top: 5%;
  padding: 2rem;
}

/* Using calc() with length */
section.container {
  width: calc(100vw - 40px);
  height: 50vh;
}

The <length> type is essential for precise control over layout, typography, and spacing, giving developers the ability to create both fixed and flexible designs. It is one of the most widely used CSS data types, alongside others like <number> or <percentage>.

Interactive Demo

Bar (font-size 12px)
Demo Code
HTML Code
<div class="demo-track">
	<div class="demo-element">Bar (font-size 12px)</div>
</div>
CSS Code
.demo-track {
	width: 300px;
	padding: 8px 0;
	background: #f4f4f4;
	outline: 1px dashed #b0b0b0;
}
.demo-element {
	height: 36px;
	display: flex;
	align-items: center;
	padding: 0 8px;
	box-sizing: border-box;
	background: #4361ee;
	color: #fff;
	font-size: 12px;
	white-space: nowrap;
	width: 200px;
}

Syntax

property: <length>

Values

Absolute lengths:
  • cm and mmSpecify centimetres and millimetres.
  • QSpecifies quarter-millimetres (40 per centimetre).
  • inSpecify inches.
  • pcSpecify picas, 6 per inch.
  • ptSpecify points, 72 per inch.
  • pxSpecify pixels, 96 per inch. These are often referred to as CSS pixels, and do not necessarily correspond to the device's resolution.
Font-relative lengths:
  • emEqual to the computed font-size of the element (or of the parent, when used in font-size).
  • remEqual to the font-size of the root element (usually html).
  • exThe x-height of the current font, the general height of lowercase letters without ascenders.
  • chThe advance width of the "0" (zero) character in the current font.
  • capThe cap height of the current font, the height of capital letters.
  • icThe advance width of the CJK water ideograph ("水") in the current font.
  • lhThe computed line-height of the element.
  • rex, rch, rcap, ric, rlhThe same as ex, ch, cap, ic and lh, but measured on the root element.
Viewport-relative lengths:
  • vw and vh1% of the width or height of the viewport.
  • vmin and vmax1% of the smaller or larger of the viewport's width and height.
  • vi and vb1% of the viewport size in the inline or block direction.
  • svh, lvh and dvh (and the matching w, i, b, min and max forms)1% of the small, large or dynamic viewport. On mobile, the small viewport assumes the browser toolbars are shown, the large viewport assumes they are hidden, and the dynamic viewport changes as they appear and disappear.
Container-relative lengths:
  • cqw, cqh, cqi, cqb, cqmin and cqmax1% of the width, height, inline size, block size, or smaller or larger dimension of the nearest query container.

Example

<div class='container'>
<h1>CSS &lt;length&gt; examples</h1>
<div class='row'>
<div class='box px'><span>150px</span></div>
<div class='box em'><span>12em</span></div>
<div class='box rem'><span>8rem</span></div>
<div class='box vw'><span>20vw</span></div>
<div class='box vh'><span>20vh</span></div>
<div class='box cm'><span>3cm</span></div>
<div class='box mm'><span>25mm</span></div>
<div class='box in'><span>1in</span></div>
<div class='box pt'><span>36pt</span></div>
</div>
<p class='note'>Resize the window to observe vw, vh and percentage changes.</p>
</div>
:root {
  font-size: 16px;
}

.container {
  width: 800px;
  margin: 24px auto;
  padding: 20px;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
  background: #ffffff;
  box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}

h1 {
  font-size: 1.25rem; /* rem example for typography */
  margin: 0 0 16px 0;
}

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-start;
}

.box {
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f8fafc;
  color: #0f172a;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  padding: 8px;
  box-sizing: border-box;
  font-weight: 600;
}

/* Absolute pixel length */
.px {
  width: 150px; /* px */
  height: 80px;
}

/* Relative to the element's font-size */
.em {
  font-size: 16px; /* 1em = 16px here */
  width: 12em; /* em */
  height: 5em;
}

/* Relative to the root font-size */
.rem {
  width: 8rem; /* rem */
  height: 4rem;
}

/* Viewport width and height */
.vw {
  width: 20vw; /* vw */
  height: 8vw;
}

.vh {
  width: 20vh; /* vh */
  height: 20vh;
}

/* Absolute physical units */
.cm {
  width: 3cm; /* cm */
  height: 1.5cm;
}

.mm {
  width: 25mm; /* mm */
  height: 12mm;
}

.in {
  width: 1in; /* in */
  height: 0.5in;
}

.pt {
  width: 36pt; /* pt (1pt = 1/72in) */
  height: 18pt;
}

.note {
  margin-top: 12px;
  color: #475569;
  font-size: 0.9rem;
}
Open in editor

Browser Support

The following information will show you the current browser support for the CSS length data type. Hover over a browser icon to see the version that first introduced support for this CSS data type.

This data type is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.