CSS Reference ยท Functions
Trigonometric functions: sin(), cos(), tan() and friends
Reference for CSS trigonometric functions sin, cos, tan, asin, acos, atan and atan2: arguments, return types, circular layouts and live examples.
CSS has the usual trigonometric functions: sin(), cos() and tan() turn an angle into a number, and asin(), acos(), atan() and atan2() turn numbers back into an angle. They make geometric layouts possible in pure CSS: items placed on a circle, waves, and angles computed from coordinates.
.dot { --angle: 60deg; translate: calc(cos(var(--angle)) * 80px) calc(sin(var(--angle)) * 80px); }
Try it
Angle 0 points right and angles grow clockwise, because the CSS y axis points down.
Syntax
sin( <angle> | <number> ) /* returns a number from -1 to 1 */ cos( <angle> | <number> ) /* returns a number from -1 to 1 */ tan( <angle> | <number> ) /* returns a number */ asin( <number> ) /* returns an angle, -90deg to 90deg */ acos( <number> ) /* returns an angle, 0deg to 180deg */ atan( <number> ) /* returns an angle, -90deg to 90deg */ atan2( <A>, <B> ) /* returns an angle, -180deg to 180deg */
A plain number given to sin(), cos() or tan() is read as radians.
Syntax and arguments
| Function | Description |
|---|---|
sin(x) |
Sine of an angle. Multiply by a length to get a vertical offset on a circle. |
cos(x) |
Cosine of an angle. Multiply by a length to get a horizontal offset. |
tan(x) |
Tangent of an angle. Grows without bound near 90deg. |
asin(n), acos(n) |
Inverse sine and cosine. The argument must be between -1 and 1. |
atan(n) |
Inverse tangent of a number. |
atan2(y, x) |
The angle of the point (x, y) measured from the positive x axis. Both arguments must have the same type: numbers, or lengths with compatible units. |
Usage notes
- The result is a unitless number, so multiply it by a unit:
calc(sin(30deg) * 100px)is50px. - Use custom properties for angles, then compute the position per item. Calculating
--afromsibling-index()is possible in browsers that support it; otherwise set it per element. - Angle units are mixed freely:
90deg,0.25turn,100gradand1.5708radare the same angle. - Rotating a label back. When items sit on a rotated ring, counter-rotate their content so text stays upright.
- Overflow and layout. The offset happens in
transformortranslate, so layout is not affected; reserve the space with your own sizing. - Exactness. Results such as
cos(90deg)are very close to, but may not be exactly,0. Avoid equality checks in math that depends on them. - Use sparingly. Complex trigonometry in CSS is hard to maintain; for data-driven charts, calculate positions in JavaScript or SVG.
Browser support
Trigonometric functions are supported in the current versions of all major browsers; check a compatibility table if you need older ones. Unsupported browsers drop the declaration, so provide a static fallback layout.