math.atan2() is an inverse trigonometric helper in the sass:math module. Given separate $y and $x components, it returns the angle in deg while keeping the correct quadrant. You will learn argument order, unit rules, why it beats math.atan(math.div($y, $x)), Dart Sass 1.25+ notes, and five compiled examples.
01
Concept
2-arg arctangent
02
Module
@use "sass:math"
03
Args
$y, then $x
04
Output
Angle in deg
05
Since
Dart Sass 1.25+
06
Practice
5 examples
Concept
What Is math.atan2()?
Two-argument arctangent answers: what angle is the point (x, y) from the origin? Unlike a single slope, both signs stay visible, so each quadrant stays distinct:
math.atan2(1, -1) → 135deg (point (-1, 1), as in the official docs fun fact)
math.atan(math.div(1, -1)) and math.atan(math.div(-1, 1)) both become atan(-1) → -45deg
Official docs place it under Trigonometric Functions next to math.atan(). Prefer atan2 whenever you still have separate horizontal and vertical offsets.
💡
Beginner tip
Argument order is $y first, then $x—same idea as JavaScript Math.atan2(y, x). The geometric point is (x, y) = ($x, $y).
Foundation
📝 Syntax
Load the math module, then pass vertical and horizontal components:
styles.scss
@use "sass:math";
math.atan2($y, $x)
// returns an angle in deg
Parameters
Parameter
Type
Required
Description
$y
Number
Yes
Vertical component of the point. Must be unitless or share compatible units with $x.
$x
Number
Yes
Horizontal component of the point. Must be unitless or share compatible units with $y.
Return value
Type
Situation
Result
Angle
Compatible / unitless inputs
Two-argument arctangent in deg (quadrant preserved)
Error
Incompatible units
Compile fails (e.g. px with em)
Modules
📦 Loading sass:math
styles.scss
// Recommended — namespaced
@use "sass:math";
$a: math.atan2(1, -1);
// Optional — bring members into the current namespace
@use "sass:math" as *;
$a: atan2(1, -1);
// Optional — custom namespace
@use "sass:math" as m;
$a: m.atan2(1, -1);
⚠️
Put @use first
Keep @use "sass:math"; near the top of the file. math.atan2() needs Dart Sass 1.25+.
Numbers
📏 How Units Work
Both arguments must be unitless, or share compatible units. Absolute lengths like px and cm can mix; relative units like em with px cannot.
Call
Result
Notes
math.atan2(1, -1)
135deg
Official fun-fact example (Q2)
math.atan2(1, 1)
45deg
Q1
math.atan2(-1, 1)
-45deg
Q4
math.atan2(-1, -1)
-135deg
Q3
math.atan2(40px, 40px)
45deg
Compatible length units
math.atan2(10px, 2em)
—
Error: incompatible units
Support
🛠 Compatibility
This is about Sass compilers, not browsers. Compiled CSS receives a plain angle.
Implementation
math.atan2()
Notes
Dart Sass 1.25+
Yes
Preferred; needs @use "sass:math"
Dart Sass < 1.25
No
Upgrade Dart Sass
LibSass / Ruby Sass
No module API
Use Dart Sass for trigonometric helpers
Cheat Sheet
⚡ Quick Reference
Goal
Code
Import math
@use "sass:math";
Quadrant-safe angle
math.atan2($y, $x)
Q2 example
math.atan2(1, -1) → 135deg
From offsets
math.atan2($dy, $dx)
Avoid losing quadrant
Prefer atan2 over math.atan(math.div($y, $x))
Debug while learning
@debug math.atan2(1, -1);
Compare
📋 atan2 vs atan(y/x)
Approach
What happens
Best for
math.atan2($y, $x)
Keeps both signs; correct quadrant
Pointers, orbits, directed offsets
math.atan(math.div($y, $x))
Divides first; many points collapse to the same half-plane answer
Only when you already know the quadrant
math.atan($slope)
One combined ratio
Simple rise/run with no quadrant needs
Official docs stress this exact difference: math.atan2(1, -1) is 135deg, while both atan(1/-1) and atan(-1/1) become -45deg.
Compare
📋 Sass math.atan2() vs CSS / JS atan2
Sass math.atan2()
CSS atan2() / JS Math.atan2
When it runs
Compile time (Dart Sass)
In the browser / runtime
Result unit
Always deg
JS returns radians; CSS follows CSS angle rules
Argument order
($y, $x)
Same idea: (y, x)
Best for
Fixed offsets known in SCSS
Dynamic / measured coordinates
Hands-On
Examples Gallery
Each example uses @use "sass:math". Open View Compiled CSS to see what Dart Sass emits.
📚 Getting Started
Official-style quadrant example and all four quadrants.
Example 1 — Basic math.atan2()
Official docs fun fact: point (-1, 1) is 135 degrees.
styles.scss
@use "sass:math";
// y = 1, x = -1 → point (-1, 1)
@debug math.atan2(1, -1); // 135deg
.demo {
--angle: #{math.atan2(1, -1)};
rotate: math.atan2(1, -1);
}
📤 Compiled CSS:
.demo {
--angle: 135deg;
rotate: 135deg;
}
How It Works
Vertical first ($y), horizontal second ($x). The signs place the point in quadrant II, so the angle is 135deg.
Example 2 — All Four Quadrants
Same magnitude, different signs—four different angles.
math.atan2($y, $x) returns the two-argument arctangent of y and x as an angle in degrees. It keeps the correct quadrant. Example: math.atan2(1, -1) is 135deg for the point (-1, 1).
$y and $x must both be unitless, or have compatible units (for example both px, or px with cm). Mixing incompatible units like px and em fails.
math.atan($n) takes one slope ratio and cannot tell quadrants apart after dividing. math.atan2($y, $x) keeps the signs of both components, so points in different quadrants stay distinct.
Like JavaScript Math.atan2, Sass uses math.atan2($y, $x)—vertical first, then horizontal. The point is (x, y) = ($x, $y).
Official docs expose this as math.atan2() on sass:math. Prefer the module form with @use "sass:math".
Did you know?
Official Sass docs call out that math.atan2($y, $x) is distinct from atan(math.div($y, $x)) because it preserves the quadrant. That is why math.atan2(1, -1) is 135deg for the point (-1, 1), while dividing first collapses related sign patterns toward -45deg.
math.atan2() turns separate $y and $x components into a compile-time angle in deg without losing the quadrant. Remember (y, x) order, keep units compatible, and prefer it over atan(y/x) for directed geometry.