docs.rodeo

MDN Web Docs mirror

{{CSSRef}} 

The <length> CSS data type represents a distance value. Lengths can be used in numerous CSS properties, such as {{Cssxref("width")}} , {{Cssxref("height")}} , {{Cssxref("margin")}} , {{Cssxref("padding")}} , {{Cssxref("border-width")}} , {{Cssxref("font-size")}} , and {{Cssxref("text-shadow")}} .

[!NOTE] Although {{cssxref("&lt;percentage&gt;")}}  values are usable in some of the same properties that accept <length> values, they are not themselves <length> values. See {{cssxref("&lt;length-percentage&gt;")}} .

Syntax

The <length> data type consists of a {{cssxref("&lt;number&gt;")}}  followed by one of the units listed below. As with all CSS dimensions, there is no space between the number and the unit literal. Specifying the length unit is optional if the number is 0.

[!NOTE] Some properties allow negative <length> values, while others do not.

The specified value of a length (specified length) is represented by its quantity and unit. The computed value of a length (computed length) is the specified length resolved to an absolute length, and its unit is not distinguished.

The <length> units can be relative or absolute. Relative lengths represent a measurement in terms of some other distance. Depending on the unit, this distance can be the size of a specific character, the line height, or the size of the {{Glossary("viewport")}} . Style sheets that use relative length units can more easily scale from one output environment to another.

[!NOTE] Child elements do not inherit the relative values as specified for their parent; they inherit the computed values.

Relative length units

CSS relative length units are based on font, container, or viewport sizes.

Relative length units based on font

Font lengths define the <length> value in terms of the size of a particular character or font attribute in the font currently in effect in an element or its parent.

[!NOTE] These units, especially em and the root relative rem, are often used to create scalable layouts, which maintain the vertical rhythm of the page even when the user changes the font size.

Relative length units based on root element’s font

Root element font relative length units define the <length> value in terms of the size of a particular character or font attribute of the root element:

Relative length units based on viewport

The viewport-percentage length units are based on four different viewport sizes: small, large, dynamic, and default. The allowance for the different viewport sizes is in response to browser interfaces expanding and retracting dynamically and hiding and showing the content underneath.

Viewport-percentage lengths define <length> values in percentage relative to the size of the initial containing block, which in turn is based on either the size of the {{Glossary("viewport")}}  or the page area, i.e., the visible portion of the document. When the height or width of the initial containing block is changed, the elements that are sized based on them are scaled accordingly. There is a viewport-percentage length unit variant corresponding to each of the viewport sizes, as described below.

[!NOTE] Viewport lengths are invalid in {{cssxref("@page")}}  declaration blocks.

Container query length units

When applying styles to a container using container queries, you can use container query length units. These units specify a length relative to the dimensions of a query container. Components that use units of length relative to their container are more flexible to use in different containers without having to recalculate concrete length values. For more information, see Container queries.

Absolute length units

Absolute length units represent a physical measurement when the physical properties of the output medium are known, such as for print layout. This is done by anchoring one of the units to a physical unit or the visual angle unit and then defining the others relative to it. Physical units include cm, in, mm, pc, pt, px, and Q.The anchoring is done differently for low-resolution devices, such as screens, versus high-resolution devices, such as printers.

For low-dpi devices, the unit px represents the physical reference pixel; other units are defined relative to it. Thus, 1in is defined as 96px, which equals 72pt. The consequence of this definition is that on such devices, dimensions described in inches (in), centimeters (cm), or millimeters (mm) don’t necessarily match the size of the physical unit with the same name.

For high-dpi devices, inches (in), centimeters (cm), and millimeters (mm) are the same as their physical counterparts. Therefore, the px unit is defined relative to them (1/96 of 1in).

[!NOTE] Many users increase their {{Glossary("user agent")}} 's default font size to make text more legible. Absolute lengths can cause accessibility problems because they are fixed and do not scale according to user settings. For this reason, prefer relative lengths (such as em or rem) when setting font-size.

Interpolation

When animated, values of the <length> data type are interpolated as real, floating-point numbers. The {{glossary("interpolation")}}  happens on the calculated value. The speed of the interpolation is determined by the easing function associated with the animation.

Examples

Comparing different length units

The following example provides you with an input field in which you can enter a <length> value (e.g. 300px, 50%, 30vw) to set the width of a result bar that will appear below it once you’ve pressed the Enter or the Return key.

This allows you to compare and contrast the effects of different length units.

HTML

<div class="outer">
  <div class="input-container">
    <label for="length">Enter width:</label>
    <input type="text" id="length" />
  </div>
  <div class="inner"></div>
</div>
<div class="results"></div>

CSS

html {
  font-family: sans-serif;
  font-weight: bold;
  box-sizing: border-box;
}

.outer {
  width: 100%;
  height: 50px;
  background-color: #eee;
  position: relative;
}

.inner {
  height: 50px;
  background-color: #999;
  box-shadow:
    inset 3px 3px 5px rgb(255 255 255 / 50%),
    inset -3px -3px 5px rgb(0 0 0 / 50%);
}

.result {
  height: 20px;
  box-shadow:
    inset 3px 3px 5px rgb(255 255 255 / 50%),
    inset -3px -3px 5px rgb(0 0 0 / 50%);
  background-color: orange;
  display: flex;
  align-items: center;
  margin-top: 10px;
}

.result code {
  position: absolute;
  margin-left: 20px;
}

.results {
  margin-top: 10px;
}

.input-container {
  position: absolute;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  height: 50px;
}

label {
  margin: 0 10px 0 20px;
}

JavaScript

const inputDiv = document.querySelector(".inner");
const inputElem = document.querySelector("input");
const resultsDiv = document.querySelector(".results");

inputElem.addEventListener("change", () => {
  inputDiv.style.width = inputElem.value;

  const result = document.createElement("div");
  result.className = "result";
  result.style.width = inputElem.value;
  const code = document.createElement("code");
  code.textContent = `width: ${inputElem.value}`;
  result.appendChild(code);
  resultsDiv.appendChild(result);

  inputElem.value = "";
  inputElem.focus();
});

Result

{{EmbedLiveSample('Comparing different length units', '100%', 700)}} 

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN