Sass math.atan2()

Beginner
10 min read
Updated: Nov 2026
5 examples
Dart Sass 1.25+

Definition and Usage

math.atan2($y, $x) returns the 2-argument arctangent of $y and $x in deg. $y and $x must be unitless or share compatible units. Load it with @use "sass:math" on Dart Sass 1.25+.

Official fun-fact sample.

math.atan2(1, -1) is the point (-1, 1) → 135deg.

Keeps the quadrant.

Unlike math.atan(math.div($y, $x)), both signs survive. So atan2(1, -1) stays 135deg, while dividing first collapses to atan(-1) → -45deg.

Argument order is $y, then $x.

Same as JavaScript Math.atan2: vertical first, horizontal second. The Cartesian point is ($x, $y).

Syntax

SCSS
@use "sass:math";

math.atan2($y, $x); //=> angle in deg

math.atan2(1, -1); // 135deg  — point (-1, 1)
math.atan2(1, 1);  // 45deg
math.atan2(0, 1);  // 0deg

// Compatible length units are fine
math.atan2(10px, -10px); // 135deg

// Module API — prefer math.atan2 after @use "sass:math"

Pattern: pass separate vertical and horizontal components → get a CSS angle that respects the quadrant → use math.atan only when you already have a single unitless slope and do not need quadrant.

Quadrants and Axes

Verified Dart Sass angles for common points:

CallPoint (x, y)Result
math.atan2(1, 1)(1, 1) Q145deg
math.atan2(1, -1)(-1, 1) Q2135deg
math.atan2(-1, -1)(-1, -1) Q3-135deg
math.atan2(-1, 1)(1, -1) Q4-45deg
math.atan2(0, 1)positive x-axis0deg
math.atan2(1, 0)positive y-axis90deg
math.atan2(0, -1)negative x-axis180deg
math.atan2(-1, 0)negative y-axis-90deg
vs math.atan.

Official docs: math.atan(math.div(1, -1)) and math.atan(math.div(-1, 1)) both become atan(-1) → -45deg. Only atan2 keeps them apart.

SCSS Examples

Examples target Dart Sass 1.25+ with @use "sass:math". Open View Compiled CSS for verified output (aligned with official Sass docs).

Example 1 — Official Fun-Fact Sample

Point (-1, 1) needs quadrant II, not a collapsed slope.

SCSS
@use "sass:math";

@debug math.atan2(1, -1); // 135deg

.basic {
  --q2: #{math.atan2(1, -1)};
}

How It Works

  1. Args are $y, $x — here 1 then -1.
  2. Point is (-1, 1) in quadrant II.
  3. Result is 135deg, not -45deg.
  4. Matches the official docs fun-fact sample.

Example 2 — Why Not Divide First?

Division erases which quadrant the point was in.

SCSS
@use "sass:math";

.compare {
  --atan: #{math.atan(math.div(1, -1))};
  --atan2: #{math.atan2(1, -1)};
}

How It Works

  1. 1 / -1 becomes -1 before atan.
  2. atan(-1) is always -45deg.
  3. atan2 still sees negative x and positive y.
  4. Use atan2 whenever both components matter.

Example 3 — All Four Quadrants

Same magnitude, different signs — four different angles.

SCSS
@use "sass:math";

.quads {
  --q1: #{math.atan2(1, 1)};
  --q2: #{math.atan2(1, -1)};
  --q3: #{math.atan2(-1, -1)};
  --q4: #{math.atan2(-1, 1)};
}

How It Works

  1. Q1 positive x & y → 45deg.
  2. Q2 negative x, positive y → 135deg.
  3. Q3 both negative → -135deg.
  4. Q4 positive x, negative y → -45deg.

Example 4 — Axis Alignments

Zero on one axis lands on the cardinal directions.

SCSS
@use "sass:math";

.axes {
  --pos-x: #{math.atan2(0, 1)};
  --pos-y: #{math.atan2(1, 0)};
  --neg-x: #{math.atan2(0, -1)};
  --neg-y: #{math.atan2(-1, 0)};
  --origin: #{math.atan2(0, 0)};
}

How It Works

  1. Positive x → 0deg.
  2. Positive y → 90deg.
  3. Negative x → 180deg.
  4. atan2(0, 0) compiles to 0deg in current Dart Sass — avoid relying on origin without a defined direction.

Example 5 — Emit a CSS Rotation from y/x

Drive rotate() from separate offset tokens.

SCSS
@use "sass:math";

$y: 1;
$x: -1;

.angle {
  --deg: #{math.atan2($y, $x)};
  transform: rotate(math.atan2($y, $x));
}

How It Works

  1. Keep $y / $x as separate design tokens.
  2. Sass compiles the angle before CSS sees it.
  3. transform receives plain 135deg.
  4. Useful for pointer/offset geometry that crosses quadrants.

Edge Cases & Pitfalls

Most atan2 bugs are argument order, units, or dividing before you call it.

order

Remember $y, then $x

Swapping args flips the angle. The Cartesian point is always ($x, $y).

units

Compatible units or both unitless

px with cm is fine; px with em fails. Check with math.compatible.

div

Do not divide first

atan(y/x) loses quadrant. Pass both components into atan2.

module

Module-only helper

Use math.atan2 after @use "sass:math".

Sass Support

Prefer the module form with Dart Sass 1.25+.

APIStatus
math.atan2()Dart Sass 1.25+ via @use "sass:math"
InputUnitless or compatible units for $y and $x
Return typeAngle in deg
LibSass / Ruby SassNo modern sass:math atan2

Compile with the Dart Sass CLI.

Key Takeaways

  • Returns a deg angle from separate $y and $x.
  • Preserves quadrant — unlike dividing then calling atan.
  • Order is $y, $x (point is (x, y)).
  • Dart Sass 1.25+ via @use "sass:math".

One line: when vertical and horizontal components must stay quadrant-safe, call math.atan2($y, $x).

Compile Notes

Practical checklist:

checklist.txt
1. @use "sass:math"
2. Pass $y first, then $x (compatible units or unitless)
3. Expect a deg angle that respects the quadrant
4. Do not divide before calling atan2
5. Use math.atan only for a single known slope

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

Next: Sass math.ceil()

Round a number up to the next whole value.

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.

8 people found this page helpful