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
Concept
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.
Foundation
📝 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
Parameter
Type
Required
Description
$number
Number
Yes
An angle whose units are compatible with deg (e.g. deg, rad, turn, grad), or a unitless number treated as radians.
Return value
Type
Situation
Result
Number
Valid angle or unitless radian
Unitless cosine (typically −1…1)
Error
Incompatible units (e.g. px)
Compile fails—pass an angle, not a length
Modules
📦 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+.
Numbers
📏 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.
Call
Result (approx.)
Notes
math.cos(100deg)
-0.1736481777
Official example
math.cos(1rad)
0.5403023059
Official example
math.cos(1)
0.5403023059
Unitless = radians
math.cos(0deg)
1
Full horizontal factor
math.cos(180deg)
-1
Opposite direction
80px * math.cos(30deg)
~69.2820323px
Reattach a length after cos
math.cos(10px)
—
Error: not an angle
Support
🛠 Compatibility
This is about Sass compilers, not browsers. Compiled CSS receives a plain number.
Implementation
math.cos()
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";
Cosine in degrees
math.cos(100deg)
Cosine in radians
math.cos(1rad) or math.cos(1)
X offset on a circle
$r * math.cos($angle)
Use π from the module
math.cos(math.$pi) ≈ -1
Debug while learning
@debug math.cos(60deg);
Compare
📋 cos vs sin vs tan
Function
What it returns
Common use
math.cos($angle)
Horizontal / adjacent factor
X on a circle, soft falloff from an angle
math.sin($angle)
Vertical / opposite factor
Y on a circle (same angle rules as cos)
math.tan($angle)
Slope ratio sin/cos
Angles 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).
Compare
📋 Sass math.cos() vs CSS / JS cos
Sass math.cos()
CSS cos() / JS Math.cos
When it runs
Compile time (Dart Sass)
In the browser / runtime
Appears in CSS output?
No — only the numeric result
CSS cos() can stay in the stylesheet
Unitless input
Treated as radians
JS: radians; CSS: follows CSS angle rules
Best for
Fixed angles known in SCSS
Dynamic angles, animated custom properties
Hands-On
Examples Gallery
Each example uses @use "sass:math". Open View Compiled CSS to see what Dart Sass emits.
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)};
}
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)};
}
Expecting an angle back — cosine is a ratio, not deg.
Old Dart Sass — need 1.25+; LibSass lacks the module API.
Pro Tips
💡 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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about math.cos()
Compile-time cosine—angles in, unitless ratio out.
5
Core concepts
📝01
Call
math.cos($angle)
API
📦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.
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.