Trigonometric and exponential functions
Definition and purpose
CSS trigonometric functions turn angles into numeric ratios that can drive geometry. cos() and sin() are convenient for horizontal and vertical offsets from a center. In CSS screen coordinates the positive vertical axis points downward.
Angle offsets
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { position: relative; left: calc(40px * cos(60deg)); top: calc(40px * sin(90deg)); } </style> </head> <body> <div id="a">Point</div> <div id="b">Other</div> </body></html>The ratios 0.5 and 1 scale the lengths.
Syntax and mental model
sin(), cos() and tan() accept angles or numbers interpreted as radians. asin(), acos(), atan() and atan2() return angles; atan2(y,x) distinguishes quadrants. pow(), sqrt(), log() and exp() operate on numbers; hypot() accepts compatible numeric types and returns their type. For an offset multiply a ratio by a length, e.g. calc(40px * cos(60deg)). Avoid undefined domains and near-singular tangents.
Length and exponent
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { width: hypot(30px, 40px); } #b { width: calc(10px * pow(2, 3)); } </style> </head> <body> <div id="a">Point</div> <div id="b">Other</div> </body></html>The hypotenuse is 50px and 2 cubed is 8.
Common mistakes
A raw sin() result is a number, not a distance. Do not treat a unitless argument of 90 as 90 degrees.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Typed arithmetic and compatibility. Next lesson: Rounding, remainder and comparison.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Trigonometric and exponential functions — knowledge check
Five questions on trigonometric and exponential functions.
What does cos(60deg) return?
What does sin(90deg) return?
What does a unitless trig argument represent?
What is hypot(3px, 4px)?
What is pow(2, 3)?