CSS <length> Data Type
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.
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
Syntax
property: <length>
Values
- 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.
- 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.
- 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.
- 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
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.
Desktop





Tablets & Mobile





