Trigonometric functions

Trigonometric functions allow you to use trigonometric calculations directly in CSS to define property values. If we want to create complex animations, transformations, or positioning based on trigonometric rules then we can use these functions. It contains sin(), cos(), tan(), asin(), acos(), atan(), and atan2() functions.

sin() Function: The CSS sin() function is used to calculate the sine of an angle. The sine is a trigonometric function whose value ranges between -1 & 1.

Syntax:

/* Angle values */
width: calc(100px * sin(30deg));
width: calc(100px * sin(0.15turn));
width: calc(100px * sin(1.0471967rad));

/* Number values */
width: calc(100px * sin(90));
width: calc(100px * sin(2 * 5));

/* Other values */
width: calc(100px * sin(pi / 3));
width: calc(100px * sin(e / 5));

Example: This example illustrates the Dynamic Width Based on Sine of an Angle.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <style>
        .box {
        height: 100px;
        background-color: green;
        position: absolute;
        top: 50px;
        left: 30%;
        transform: translateX(-50%);
        width: calc(100px * sin(30deg));
        }
    </style>
</head>
  
<body>
    <h1 style="color: green;">
        w3wiki
    </h1>
    <h2>
          sin() function
      </h2>
    <div class="box"></div>
</body>
  
</html>


Output:

cos() Function: The CSS cos() function is a mathematical function that calculates the cosine of a given angle.

Syntax:

width: calc(angle);

Example: In this example,we will use cos() function.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <style>
        .box {
            width: 100px;
            height: 100px;
            background-color: green;
            position: absolute;
            top: 50px;
            left: calc(30% + 50px * cos(30deg));
        }
    </style>
</head>
  
<body>
    <h1 style="color: green;">
        w3wiki
    </h1>
    <h2>
        cos() function
    </h2>
    <div class="box">
        w3wiki
    </div>
</body>
  
</html>


Output:

tan() Function: The CSS tan() function returns the tangent of a specified angle in radians. The value of the tangent will range between −infinity and infinity.

Syntax:

/* Angle values */
width: calc(100px * tan(30deg));
width: calc(100px * tan(0.25turn));
width: calc(100px * tan(0.398163rad));

/* Number values */
width: calc(100px * tan(0.73502));
width: calc(100px * tan(2.732 – 1));

/* Other values */
width: calc(100px * tan(pi / 4));
width: calc(100px * tan(e / 2));

Example: In this example, we will use the tan() function:

HTML




<!DOCTYPE html>
<html>
  
<head>
    <style>
        .container {
            height: 200px;
            width: 200px;
            background-color: green;
            transform: rotate(45deg) 
                       skew(20deg) 
                       scale(1, tan(20deg));
        }
    </style>
</head>
  
<body>
    <h1 style="color: green;">
        w3wiki
    </h1>
    <h2>tan() function</h2>
    <div class="container">
    </div>
</body>
  
</html>


Output:

asin() Function: The inverse sine of an integer between -1 and 1 is returned by the trigonometric asin() CSS function.

Syntax:

/* Numeric values */
transform: rotate(asin(-0.9));
transform: rotate(asin(1));

/* Other values */
transform: rotate(asin(pi / 2));
transform: rotate(asin(e / 4));

Example:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <style>
        div.heading {
            width: 100px;
            height: 30px;
            color: green;
            padding: 30px;
        }
  
        div.heading-1 {
            transform: rotate(asin(pi/8));
        }
  
        div.heading-2 {
            transform: rotate(asin(e/5));
        }
    </style>
</head>
  
<body>
    <h1>
          w3wiki
      </h1>
    <h2>
          asin() function
      </h2>
    <div class="heading heading-1">
        w3wiki
    </div>
    <div class="heading heading-2">
        w3wiki
    </div>
</body>
  
</html>


Output:

  • acos() Function: The trigonometric acos() function in the CSS gives the inverse cosine of a value between -1 and 1.

Syntax:

/* Numeric values */
transform: rotate(acos(-0.5));
transform: rotate(acos(4 * 0.256));

/* Other values */
transform: rotate(acos(pi / 2));
transform: rotate(acos(e / 6));

Example: In this example, we will use acos() functions.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <style>
        div.heading {
            width: 100px;
            height: 30px;
            color: green;
            padding: 30px;
        }
  
        div.heading-1 {
            transform: rotate(acos(pi/8));
        }
  
        div.heading-2 {
            transform: rotate(acos(e/5));
        }
    </style>
</head>
  
<body>
    <h1 style="color: green;">
        w3wiki
    </h1>
    <h2>acos() function</h2>
    <div class="heading heading-1">
        w3wiki
    </div>
    <div class="heading heading-2">
        w3wiki
    </div>
</body>
  
</html>


Output:

atan() Function: The trigonometric atan() CSS function returns the inverse tangent of a value between -∞ and +∞.

Syntax:

/* Numeric values */
transform: rotate(atan(-0.9));
transform: rotate(atan(1));

/* Other values */
transform: rotate(atan(pi / 2));
transform: rotate(atan(e / 4));

Example:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <style>
        div.heading {
            width: 100px;
            height: 30px;
            color: green;
            padding: 30px;
        }
  
        div.heading-1 {
            transform: rotate(atan(pi/8));
        }
  
        div.heading-2 {
            transform: rotate(atan(e/5));
        }
    </style>
</head>
  
<body>
    <h1 style="color: green;">
          w3wiki
      </h1>
    <h2>
          atan() function
      </h2>
    <div class="heading heading-1">
          w3wiki
      </div>
    <div class="heading heading-2">
          w3wiki
      </div>
</body>
  
</html>


Output:

atan2() Function: The inverse tangent of two values between -infinity and infinity is returned by the trigonometric atan2() CSS function.

Syntax:

/* Numeric values */
transform: rotate(atan2(5, 1));

/* Dimensional values */
transform: rotate(atan2(12rem, -1.5rem));

/* Percentage values */
transform: rotate(atan2(40%, -50%));

/* Other values */
transform: rotate(atan2(pi, 30));
transform: rotate(atan2(e, 20));

Example:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <style>
        div.heading {
            width: 100px;
            height: 30px;
            color: green;
            padding: 30px;
        }
  
        div.heading-1 {
            transform: rotate(atan2(pi, 45));
        }
  
        div.heading-2 {
            transform: rotate(atan2(e, 30));
        }
    </style>
</head>
  
<body>
    <h1 style="color: green;">
        w3wiki
    </h1>
    <h2>
        atan2() function
    </h2>
    <div class="heading heading-1">
        w3wiki
    </div>
    <div class="heading heading-2">
        w3wiki
    </div>
</body>
  
</html>


Output:

CSS Math Functions

In this article, we will learn about the CSS Math Functions. CSS Math Functions enable you to express CSS numeric values using mathematical expressions directly in your stylesheets. By using CSS Math Functions, we achieve greater flexibility and ease in creating responsive layouts and styling elements. We can use calc(), max(), and min() math functions to make our work easy.

CSS Math Functions are classified into the following types:

  • Basic arithmetic
  • Comparison functions
  • Trigonometric functions
  • Exponential functions
  • Sign-related functions
  • Stepped value functions

Similar Reads

Basic Arithmetic

The basic arithmetic CSS Math function is calc()....

Comparison Functions

...

Trigonometric functions

This function contains all the comparison function which is used in the CSS Function. It contains max(), min(), and clamp() functions....

Exponential functions:

...

Sign-related functions:

...

Stepped value functions:

...

Contact Us