atan2() CSS Function
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.
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
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
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.
Desktop





Tablets & Mobile





