Sass math.atan2() Function

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
sass:math · Dart Sass 1.25+

What You’ll Learn

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

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.atan2(1, 1)45deg · math.atan2(-1, 1)-45deg
  • 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).

📝 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

ParameterTypeRequiredDescription
$yNumberYesVertical component of the point. Must be unitless or share compatible units with $x.
$xNumberYesHorizontal component of the point. Must be unitless or share compatible units with $y.

Return value

TypeSituationResult
AngleCompatible / unitless inputsTwo-argument arctangent in deg (quadrant preserved)
ErrorIncompatible unitsCompile fails (e.g. px with em)

📦 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+.

📏 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.

CallResultNotes
math.atan2(1, -1)135degOfficial fun-fact example (Q2)
math.atan2(1, 1)45degQ1
math.atan2(-1, 1)-45degQ4
math.atan2(-1, -1)-135degQ3
math.atan2(40px, 40px)45degCompatible length units
math.atan2(10px, 2em)Error: incompatible units

🛠 Compatibility

This is about Sass compilers, not browsers. Compiled CSS receives a plain angle.

Implementationmath.atan2()Notes
Dart Sass 1.25+YesPreferred; needs @use "sass:math"
Dart Sass < 1.25NoUpgrade Dart Sass
LibSass / Ruby SassNo module APIUse Dart Sass for trigonometric helpers

⚡ Quick Reference

GoalCode
Import math@use "sass:math";
Quadrant-safe anglemath.atan2($y, $x)
Q2 examplemath.atan2(1, -1)135deg
From offsetsmath.atan2($dy, $dx)
Avoid losing quadrantPrefer atan2 over math.atan(math.div($y, $x))
Debug while learning@debug math.atan2(1, -1);

📋 atan2 vs atan(y/x)

ApproachWhat happensBest for
math.atan2($y, $x)Keeps both signs; correct quadrantPointers, orbits, directed offsets
math.atan(math.div($y, $x))Divides first; many points collapse to the same half-plane answerOnly when you already know the quadrant
math.atan($slope)One combined ratioSimple 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.

📋 Sass math.atan2() vs CSS / JS atan2

Sass math.atan2()CSS atan2() / JS Math.atan2
When it runsCompile time (Dart Sass)In the browser / runtime
Result unitAlways degJS returns radians; CSS follows CSS angle rules
Argument order($y, $x)Same idea: (y, x)
Best forFixed offsets known in SCSSDynamic / measured coordinates

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);
}

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.

styles.scss
@use "sass:math";

.q {
  --q1: #{math.atan2(1, 1)};   // 45deg
  --q2: #{math.atan2(1, -1)};  // 135deg
  --q3: #{math.atan2(-1, -1)}; // -135deg
  --q4: #{math.atan2(-1, 1)};  // -45deg
}

How It Works

Dividing y/x first would collapse several of these into the same atan(-1) or atan(1) answer. Atan2 keeps them apart.

📈 Practical Patterns

Contrast with atan, length units, and pointer rotation.

Example 3 — Why Not Just atan(y/x)?

Same division patterns, wrong shared answer without atan2.

styles.scss
@use "sass:math";

.compare {
  --atan2: #{math.atan2(1, -1)};                 // 135deg
  --atan-a: #{math.atan(math.div(1, -1))};       // -45deg
  --atan-b: #{math.atan(math.div(-1, 1))};       // -45deg
}

How It Works

Both divisions become -1 before atan, so the quadrant is lost. That is the exact warning from the official Sass docs.

Example 4 — Compatible Length Units

Offsets can keep CSS units as long as they are compatible.

styles.scss
@use "sass:math";

$dy: 30px;
$dx: 40px;

.arrow {
  rotate: math.atan2($dy, $dx); // 36.8698976458deg
  --angle: #{math.atan2($dy, $dx)};
}

How It Works

Both values use px, so Sass accepts them and returns the directed angle of the 30–40 offset (a 3–4–5 triangle).

Example 5 — Rotate a Pointer Toward a Point

Bake a fixed target offset into a rotate token.

styles.scss
@use "sass:math";

// from origin toward (-80px, 40px)
$dx: -80px;
$dy: 40px;
$angle: math.atan2($dy, $dx); // 153.4349488229deg

.pointer {
  rotate: $angle;
  width: math.hypot($dx, $dy);
}

How It Works

Atan2 aims the pointer; math.hypot() sizes it to the same offset vector.

🚀 Real-World Use Cases

  • Pointers / chevrons — rotate toward a known offset.
  • Orbit badges — place items from cartesian offsets with correct quadrants.
  • Connector lines — angle a stroke from (dx, dy).
  • Icon geometry — keep Q2/Q3 directions that plain atan would flip.
  • Teaching quadrants — contrast atan2 with atan(y/x).
  • Fixed vs measured — use Sass when offsets are known; use JS/CSS when they are live.

🧠 How Compilation Works

1

Write SCSS

Call math.atan2($y, $x) after @use.

Source
2

Check units

Require unitless args or compatible length units.

Validate
3

Compute atan2

Return a quadrant-aware angle in deg.

Atan2
4

Plain CSS ships

The browser only receives the final angle (e.g. 135deg).

⚠️ Common Pitfalls

  • Swapping x and y — order is ($y, $x), not ($x, $y).
  • Using atan(y/x) — loses quadrant; prefer atan2 for directed geometry.
  • Incompatible unitspx with em fails.
  • Expecting radians — Sass atan2 always returns deg.
  • Old Dart Sass — need 1.25+; LibSass lacks the module API.

💡 Best Practices

✅ Do

  • Use @use "sass:math" and math.atan2() in new SCSS
  • Pass $y first, then $x
  • Keep units compatible (or both unitless)
  • Prefer atan2 over atan(div(y, x)) for directed angles
  • Prefer Dart Sass 1.25+

❌ Don’t

  • Assume argument order is (x, y)
  • Divide first when quadrants matter
  • Mix incompatible units
  • Expect the browser to re-evaluate math.atan2
  • Rely on LibSass for sass:math

Key Takeaways

Knowledge Unlocked

Five things to remember about math.atan2()

Compile-time two-arg arctangent—quadrants stay intact.

5
Core concepts
📦 02

Module

sass:math

@use
🎲 03

Order

y then x

Args
🌡️ 04

Output

deg + quadrant

Angle
05

Since

Dart Sass 1.25+

Tooling

❓ Frequently Asked Questions

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).
Add @use "sass:math"; then call math.atan2($y, $x). Requires Dart Sass 1.25+.
$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.

Conclusion

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.

Continue with math.atan(), math.hypot(), or math.ceil().

More Sass Math

Continue with rounding helpers in the math module.

math.ceil() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful