Favorites

atan2() CSS Function

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

The atan2() function returns the angle between the positive x-axis and a point (x, y), taking two values. It is useful for pointing elements at a target.

☕
Enjoying this site?

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

Buy us a coffee

Description

The atan2() CSS function calculates the angle between the positive x-axis and the point (x, y). It takes two arguments, the y value first and the x value second, and returns an <angle> between -180deg and 180deg. Unlike atan(), which only sees a single ratio, atan2() knows the sign of both values, so it can tell apart all four directions and never divides by zero.

Both arguments must be of the same type, such as two lengths, two numbers or two percentages, which makes atan2() ideal when you have horizontal and vertical distances stored as lengths. A common use is pointing an arrow or icon toward a target position, or rotating a line so it connects two points.

The atan2() function is one of the CSS trigonometric functions, together with sin(), cos(), tan(), asin(), acos(), atan() and atan2(). Like calc(), it can be used anywhere a value of the matching type is accepted, and its arguments can contain full calculations, custom properties and the constants pi and e.

/* Point an arrow at a target 100px right and 50px down */
.arrow {
  rotate: atan2(50px, 100px);   /* about 26.6deg */
}

/* Angle from custom properties holding a direction vector */
.compass {
  rotate: atan2(var(--dy), var(--dx));
}

Interactive Demo

Demo Code
HTML Code
<div class="demo-dial">
	<div class="demo-element"></div>
</div>
CSS Code
.demo-dial {
	position: relative;
	width: 200px;
	height: 200px;
	border-radius: 50%;
	background: repeating-conic-gradient(from 90deg, #b0b0b0 0 1deg, transparent 1deg 15deg), #f4f4f4;
	outline: 1px solid #c5c5c5;
}
.demo-dial::after {
	content: "";
	position: absolute;
	left: 100px;
	top: 100px;
	width: 100px;
	border-top: 1px dashed #4361ee;
}
.demo-element {
	position: absolute;
	left: 100px;
	top: 98px;
	width: 95px;
	height: 4px;
	border-radius: 2px;
	background: #f72585;
	transform-origin: 0 50%;
	rotate: atan2(0, 1);
}

Syntax

atan2() = atan2( <calc-sum>, <calc-sum> )

Values

  • yA calculation resolving to a <number>, <dimension> or <percentage>: the vertical coordinate.
  • xA calculation of the same type: the horizontal coordinate. The function returns an <angle> between -180deg and 180deg.

Example

<div class="wrap">
<h1>CSS atan2() examples</h1>
<div class="grid">
<div class="card">
<div class="dial"><span class="needle" style="--dx: 40px; --dy: 40px"></span></div>
<div class="label">atan2(40px, 40px) = 45deg</div>
</div>
<div class="card">
<div class="dial"><span class="needle" style="--dx: -40px; --dy: 40px"></span></div>
<div class="label">atan2(40px, -40px) = 135deg</div>
</div>
<div class="card">
<div class="dial"><span class="needle" style="--dx: -40px; --dy: -20px"></span></div>
<div class="label">atan2(-20px, -40px) &asymp; -153deg</div>
</div>
<div class="card">
<div class="dial"><span class="needle" style="--dx: 30px; --dy: -60px"></span></div>
<div class="label">atan2(-60px, 30px) &asymp; -63deg</div>
</div>
</div>
<p class="note">Positive y points down the screen, so positive angles turn clockwise.</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;
}

/* atan2() examples */
.dial {
position: relative;
width: 120px;
height: 120px;
margin: 0 auto;
border-radius: 50%;
background: #e2e8f0;
}

.needle {
position: absolute;
left: 50%;
top: 50%;
width: 52px;
height: 4px;
margin-top: -2px;
border-radius: 2px;
background: #dc2626;
transform-origin: left center;
}

.dial::after {
content: '';
position: absolute;
left: 50%;
top: 50%;
width: 10px;
height: 10px;
margin: -5px 0 0 -5px;
border-radius: 50%;
background: #0f172a;
}

.needle { rotate: atan2(var(--dy), var(--dx)); }
Open in editor

Browser Support

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