Course contentsShow
← Web Development
Lesson 489 of 2,9828. Advanced CSS Selectors and FeaturesFree lesson

Math Functions: sin(), cos(), tan(), and Trigonometry

Use trigonometric functions for complex animations, circular layouts, and mathematical calculations.

Math Functions: sin(), cos(), tan(), and Trigonometry

You'll learn how CSS's trigonometric functions unlock circular motion, wave patterns, and precise mathematical calculations in your designs.

CSS now includes mathematical functions from trigonometry—sin(), cos(), and tan()—that accept angle values and return numeric ratios. Think of these as tools for describing circular and wave-like motion: just as a clock's hands move in predictable arcs, these functions help you position elements along curves or create rhythmic animations.

Understanding the Basics

These functions work with angles (in deg, rad, turn, or grad):

  • sin(angle) - Returns a value between -1 and 1, creating wave patterns
  • cos(angle) - Similar to sine but shifted 90 degrees, perfect for circular positioning
  • tan(angle) - Returns the tangent ratio, useful for slopes and angles

Practical Applications

Circular layouts: Use cos() and sin() to position items around a circle's circumference. Since cosine gives horizontal position and sine gives vertical position on a unit circle, you can calculate precise coordinates.

Wave animations: Combine sin() with custom properties to create smooth, oscillating motion—think of floating elements or breathing effects.

Complex calculations: Calculate angles, slopes, or geometric patterns mathematically rather than guessing values.

.item {
  /* Position element on a circle */
  --angle: 45deg;
  --radius: 200px;
  transform: translate(
    calc(cos(var(--angle)) * var(--radius)),
    calc(sin(var(--angle)) * var(--radius))
  );
}

These functions integrate seamlessly with calc(), custom properties, and animations to create mathematically precise designs that were previously only possible with JavaScript.

Key Takeaway: CSS trigonometric functions (sin(), cos(), tan()) enable circular layouts, wave patterns, and precise mathematical calculations directly in stylesheets, eliminating the need for JavaScript in many geometric design scenarios.