Favorites

abs() CSS Function

abs()
CSS FunctionThis function is supported by all modern browsers.
Updated 21st November 2026

The abs() function returns the absolute value of its argument, the same value without its sign, so -20px becomes 20px.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

Description

The abs() CSS function returns the absolute value of its argument: the same value without its sign. Positive values are unchanged and negative values become positive, so abs(-20px) is 20px and abs(20px) is also 20px. The argument can be any numeric type, such as a number, length, angle or percentage, and the result keeps that type.

abs() is handy when a value is calculated from custom properties that might be negative, but the property it is used in only accepts positive values or should always move in one direction. For example, a shadow size, a border width or a distance between two points must never be negative. Note that when a percentage is involved, abs() works on the resolved value, so abs(20% - 100px) depends on what the percentage resolves to.

/* Distance between two offsets is always positive */
.gap {
  width: abs(var(--end) - var(--start));
}

/* Shadow size based on a tilt that may be negative */
.card {
  box-shadow: 0 0 abs(var(--tilt) * 2px) rgb(0 0 0 / 0.3);
}

abs() is a sign-related function together with sign(). Combining them reconstructs the original value: calc(sign(x) * abs(x)) equals x.

Interactive Demo

050px100px150px200px250px
Demo Code
HTML Code
<div class="demo-track">
	<div class="demo-element"></div>
</div>
<div class="demo-scale"><span>0</span><span>50px</span><span>100px</span><span>150px</span><span>200px</span><span>250px</span></div>
CSS Code
.demo-track {
	width: 300px;
	height: 36px;
	background: #f4f4f4 repeating-linear-gradient(90deg, #b0b0b0 0 1px, transparent 1px 50px);
	outline: 1px solid #c5c5c5;
}
.demo-scale {
	display: grid;
	grid-template-columns: repeat(6, 50px);
	width: 300px;
	font-size: 11px;
	color: #666;
}
.demo-element {
	height: 100%;
	background: linear-gradient(90deg, #f72585, #b5179e);
	width: abs(-150px);
}

Syntax

abs() = abs( <calc-sum> )

Values

  • <calc-sum>A calculation resolving to a <number>, <dimension> or <percentage>. The function returns the same value with a positive sign.

Example

<div class="wrap">
<h1>CSS abs() examples</h1>
<div class="card list">
<div class="bar" style="--w: 120px"></div><div class="label">abs(120px) = 120px</div>
<div class="bar" style="--w: -200px"></div><div class="label">abs(-200px) = 200px</div>
<div class="bar" style="--w: calc(100px - 260px)"></div><div class="label">abs(100px - 260px) = 160px</div>
</div>
<p class="note">Without abs(), the negative widths would be invalid and the bars would disappear.</p>
</div>
/* Base demo layout */
* { box-sizing: border-box; }

body {
margin: 24px;
font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
background: #f5f7fb;
color: #102a43;
}

.wrap {
max-width: 860px;
margin: 0 auto;
}

h1 {
font-size: 20px;
margin: 0 0 16px;
}

.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 16px;
}

.card {
background: #fff;
border-radius: 12px;
padding: 16px;
box-shadow: 0 6px 18px rgba(16, 42, 67, 0.08);
}

.label {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
font-size: 12px;
color: #486581;
margin-top: 10px;
word-break: break-word;
}

.note {
font-size: 13px;
color: #486581;
margin-top: 14px;
}

/* abs() examples */
.bar {
height: 28px;
border-radius: 6px;
background: linear-gradient(90deg, #2563eb, #06b6d4);
max-width: 100%;
}

.track {
background: #e2e8f0;
border-radius: 6px;
overflow: hidden;
}

.list { display: grid; gap: 10px; }
.bar { width: abs(var(--w)); }
Open in editor

Browser Support

The following information will show you the current browser support for the CSS abs() function. Hover over a browser icon to see the version that first introduced support for this CSS function.

This function 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
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee