Sass math.cos() Function

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

What You’ll Learn

math.cos() is a trigonometric helper in the sass:math module. It returns the cosine of an angle—useful for circular layouts, soft fades, and fixed transform factors. You will learn angle units, the unitless-as-radians rule, Dart Sass 1.25+ notes, and five compiled examples.

01

Concept

Cosine

02

Module

@use "sass:math"

03

Angles

deg / rad / unitless

04

Result

Unitless ratio

05

Since

Dart Sass 1.25+

06

Practice

5 examples

What Is math.cos()?

Cosine relates an angle to a horizontal factor on the unit circle. In Sass it returns that factor as a unitless number (usually between -1 and 1):

  • math.cos(100deg) → about -0.1736481777
  • math.cos(1rad) → about 0.5403023059
  • math.cos(1) → same as 1rad (unitless = radians)
  • math.cos(0deg)1 · math.cos(180deg)-1

Official docs place it under Trigonometric Functions with math.sin, math.tan, and the inverse helpers. Pair cosine with a radius to place points: $x: $r * math.cos($angle).

💡
Beginner tip

Browsers never see math.cos. Dart Sass evaluates it and writes a plain number into your .css file. Prefer writing deg when you think in degrees—unitless 1 means 1 radian, not 1 degree.

📝 Syntax

Load the math module, then pass an angle or a unitless radian value:

styles.scss
@use "sass:math";

math.cos($number)
// angle (deg-compatible) or unitless → radians

Parameters

ParameterTypeRequiredDescription
$numberNumberYesAn angle whose units are compatible with deg (e.g. deg, rad, turn, grad), or a unitless number treated as radians.

Return value

TypeSituationResult
NumberValid angle or unitless radianUnitless cosine (typically −1…1)
ErrorIncompatible units (e.g. px)Compile fails—pass an angle, not a length

📦 Loading sass:math

styles.scss
// Recommended — namespaced
@use "sass:math";
$c: math.cos(60deg);

// Optional — bring members into the current namespace
@use "sass:math" as *;
$c: cos(60deg);

// Optional — custom namespace
@use "sass:math" as m;
$c: m.cos(60deg);
⚠️
Put @use first

Keep @use "sass:math"; near the top of the file. math.cos() needs Dart Sass 1.25+.

📏 How Units Work

Pass an angle, or omit units to mean radians. The result is always unitless—multiply by a length when you need CSS lengths.

CallResult (approx.)Notes
math.cos(100deg)-0.1736481777Official example
math.cos(1rad)0.5403023059Official example
math.cos(1)0.5403023059Unitless = radians
math.cos(0deg)1Full horizontal factor
math.cos(180deg)-1Opposite direction
80px * math.cos(30deg)~69.2820323pxReattach a length after cos
math.cos(10px)Error: not an angle

🛠 Compatibility

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

Implementationmath.cos()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";
Cosine in degreesmath.cos(100deg)
Cosine in radiansmath.cos(1rad) or math.cos(1)
X offset on a circle$r * math.cos($angle)
Use π from the modulemath.cos(math.$pi)-1
Debug while learning@debug math.cos(60deg);

📋 cos vs sin vs tan

FunctionWhat it returnsCommon use
math.cos($angle)Horizontal / adjacent factorX on a circle, soft falloff from an angle
math.sin($angle)Vertical / opposite factorY on a circle (same angle rules as cos)
math.tan($angle)Slope ratio sin/cosAngles of incline, skewed offsets

All three share the same input rules: angle units, or unitless radians. For a point on a circle: $x: $r * math.cos($a) and $y: $r * math.sin($a).

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

Sass math.cos()CSS cos() / JS Math.cos
When it runsCompile time (Dart Sass)In the browser / runtime
Appears in CSS output?No — only the numeric resultCSS cos() can stay in the stylesheet
Unitless inputTreated as radiansJS: radians; CSS: follows CSS angle rules
Best forFixed angles known in SCSSDynamic angles, animated custom properties

Examples Gallery

Each example uses @use "sass:math". Open View Compiled CSS to see what Dart Sass emits.

📚 Getting Started

Official-style deg/rad calls and familiar angles.

Example 1 — Basic math.cos()

Official docs examples with degrees and radians.

styles.scss
@use "sass:math";

@debug math.cos(100deg); // -0.1736481777
@debug math.cos(1rad);   // 0.5403023059
@debug math.cos(1);      // 0.5403023059

.demo {
  --cos-100deg: #{math.cos(100deg)};
  --cos-1rad: #{math.cos(1rad)};
}

How It Works

Degrees and radians are both valid angle inputs. Unitless 1 matches 1rad, not 1deg.

Example 2 — Common Angles

Memorize a few landmarks: 0°, 60°, 90°, 180°.

styles.scss
@use "sass:math";

.landmarks {
  --c0: #{math.cos(0deg)};     // 1
  --c60: #{math.cos(60deg)};   // 0.5
  --c90: #{math.cos(90deg)};   // ~0
  --c180: #{math.cos(180deg)}; // -1
  --cpi: #{math.cos(math.$pi)}; // ~-1 (π radians)
}

How It Works

Perfect values like 0.5 and -1 compile cleanly. 90deg is effectively 0 within Sass precision. math.$pi is available from the same module (Dart Sass 1.25+).

Example 3 — Unitless Means Radians

A common beginner trap: forgetting that bare numbers are radians.

styles.scss
@use "sass:math";

.compare {
  // These match (1 radian)
  --as-rad: #{math.cos(1rad)};
  --unitless: #{math.cos(1)};

  // This is different (1 degree)
  --as-deg: #{math.cos(1deg)};
}

How It Works

Write deg when you mean degrees. Leaving units off is the same as writing rad.

📈 Practical Patterns

Circular offsets and angle-based opacity tokens.

Example 4 — X Offset on a Circle

Place an element along a fixed radius using cosine for the horizontal component.

styles.scss
@use "sass:math";

$r: 80px;
$angle: 30deg;

.orbit-item {
  // x = r · cos(θ)
  left: 50% + $r * math.cos($angle);
  // pair with sin for y when you add math.sin
  --cos: #{math.cos($angle)};
}

How It Works

math.cos(30deg) is √3/2 ≈ 0.866. Multiplying by 80px gives the horizontal offset; Sass may emit calc() when mixing % and px.

Example 5 — Soft Factor from an Angle

Map cosine into a 0–1 range for a gentle opacity or scale token.

styles.scss
@use "sass:math";

// cos goes -1…1 → shift to 0…1
$angle: 120deg;
$factor: (math.cos($angle) + 1) / 2;

.soft {
  opacity: $factor;
  transform: scale($factor);
}

How It Works

math.cos(120deg) is -0.5. Adding 1 and dividing by 2 yields 0.25—a simple angle-to-fade mapping at compile time.

🚀 Real-World Use Cases

  • Circular menus — compute fixed x positions with $r * math.cos($angle).
  • Icon / badge rings — place known steps around a circle in a design system.
  • Soft fades — map cosine into 0–1 for opacity or scale tokens.
  • Transform factors — bake horizontal projection into translate or scale.
  • Teaching trig — show deg vs rad vs unitless with @debug.
  • Fixed vs animated — use Sass when the angle is known; use CSS cos() when it must animate.

🧠 How Compilation Works

1

Write SCSS

Call math.cos($angle) after @use.

Source
2

Check angle units

Accept deg-compatible angles; treat unitless as radians.

Validate
3

Compute cosine

Return a unitless number (typically between −1 and 1).

Cos
4

Plain CSS ships

The browser only receives the final number (or a length you rebuild).

⚠️ Common Pitfalls

  • Unitless ≠ degrees — bare numbers are radians; write deg when you mean degrees.
  • Passing lengthsmath.cos(10px) fails; pass an angle, then multiply by a length.
  • Forgetting @usemath.cos needs sass:math loaded.
  • Expecting an angle back — cosine is a ratio, not deg.
  • Old Dart Sass — need 1.25+; LibSass lacks the module API.

💡 Best Practices

✅ Do

  • Use @use "sass:math" and math.cos() in new SCSS
  • Write deg (or rad) explicitly for clarity
  • Multiply by a length when you need CSS offsets
  • Pair with math.sin for full circular coordinates
  • Prefer Dart Sass 1.25+

❌ Don’t

  • Assume unitless means degrees
  • Pass px / rem into math.cos
  • Expect the browser to re-evaluate math.cos
  • Use Sass cos for animated angles (prefer CSS cos())
  • Rely on LibSass for sass:math

Key Takeaways

Knowledge Unlocked

Five things to remember about math.cos()

Compile-time cosine—angles in, unitless ratio out.

5
Core concepts
📦 02

Module

sass:math

@use
🌡️ 03

Unitless

= radians

Rule
🔢 04

Result

unitless ratio

Output
05

Since

Dart Sass 1.25+

Tooling

❓ Frequently Asked Questions

math.cos($number) returns the cosine of an angle. Example: math.cos(100deg) is about -0.1736481777; math.cos(1rad) and math.cos(1) are about 0.5403023059.
Add @use "sass:math"; then call math.cos($number). Requires Dart Sass 1.25+.
An angle compatible with deg (such as deg, rad, turn, grad), or a unitless number. Unitless values are treated as radians.
No. Cosine is a unitless ratio (typically between -1 and 1). Multiply by a length if you need a CSS length, for example $r * math.cos(30deg).
Sass math.cos() runs at compile time and writes a fixed number. CSS cos() stays in the stylesheet and can use runtime values like custom properties in supporting browsers.
Official docs expose this as math.cos() on sass:math. Prefer the module form with @use "sass:math".
Did you know?

Official Sass docs note that if $number has no units, it is assumed to be in rad. That is why math.cos(1) and math.cos(1rad) print the same value in the documentation examples.

Conclusion

math.cos() gives you compile-time cosine for known angles. Prefer explicit deg or rad, remember that unitless means radians, and multiply by a length when you need CSS offsets. Use CSS cos() when the angle must change at runtime.

Continue with math.div(), math.floor(), or math.sin().

More Sass Math

Continue with modern division in the math module.

math.div() →

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