Favorites

sign() CSS Function

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

The sign() function returns -1 if a value is negative, 1 if it is positive, and 0 if it is zero.

☕
Enjoying this site?

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

Buy us a coffee

Description

The sign() CSS function returns the sign of its argument: -1 if the value is negative, 1 if it is positive, and 0 if it is zero (strictly, 0 or -0). The argument can be any numeric type, such as a length, angle or number, but the result is always a plain <number>.

Because the result is just -1, 0 or 1, sign() is useful as a switch inside calculations. You can multiply another value by it to flip a direction, turn something on or off, or pick between two states based on whether a custom property is positive or negative, all without media queries or JavaScript. For instance, an arrow can point up for a positive change and down for a negative change from a single custom property.

/* Arrow points up for gains and down for losses */
.trend {
  rotate: calc(90deg - sign(var(--change)) * 90deg);
}

/* Move in the direction of an offset, by a fixed amount */
.nudge {
  translate: calc(sign(var(--offset)) * 8px) 0;
}

sign() pairs naturally with abs(), since calc(sign(x) * abs(x)) always 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: calc((sign(-5) + 2) * 60px);
}

Syntax

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

Values

  • <calc-sum>A calculation resolving to a <number>, <dimension> or <percentage>. The function returns -1 if it is negative, 1 if positive and 0 if zero.

Example

<div class="wrap">
<h1>CSS sign() examples</h1>
<div class="grid">
<div class="card">
<div class="stat up" style="--change: 12px"><span class="arrow"></span>+12</div>
<div class="label">sign(12px) = 1</div>
</div>
<div class="card">
<div class="stat down" style="--change: -8px"><span class="arrow"></span>-8</div>
<div class="label">sign(-8px) = -1</div>
</div>
<div class="card">
<div class="stat" style="--change: 0px"><span class="arrow"></span>0</div>
<div class="label">sign(0px) = 0</div>
</div>
</div>
<p class="note">Each arrow uses rotate: calc(90deg - sign(var(--change)) * 90deg).</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;
}

/* sign() examples */
.stat {
display: flex;
align-items: center;
gap: 12px;
font-size: 22px;
font-weight: 700;
}

.arrow {
width: 0;
height: 0;
border-left: 12px solid transparent;
border-right: 12px solid transparent;
border-bottom: 18px solid #475569;
/* 0deg when positive (up), 180deg when negative (down), 90deg when zero */
rotate: calc(90deg - sign(var(--change)) * 90deg);
}

.up .arrow { border-bottom-color: #16a34a; }
.down .arrow { border-bottom-color: #dc2626; }
Open in editor

Browser Support

The following information will show you the current browser support for the CSS sign() 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