hypot()

Term side of the Card.
Topic: Mathematics
Unit title: hypot()
Categories: Function
Explanation side of the Card

Explanation: The math function that calculates the length of the hypotenuse of a right-angled triangle. It takes two or more numbers as arguments, representing the sides of the triangle and returns the square root of the sum of their squares, useful in advanced layout calculations.

Browser compatibility:
  • The chrome browser supports this feature starting from version 120.
  • The edge browser supports this feature starting from version 120.
  • The firefox browser supports this feature starting from version 118.
  • The safari browser supports this feature starting from version 15.4.
Availability: 

Example

Wrongcss
.line {
  width: sqrt(pow(var(--dx), 2) + pow(var(--dy), 2));
}
Rightcss
.line {
  width: hypot(var(--dx), var(--dy));
}

pow() and sqrt() only accept numbers, while hypot() accepts lengths and returns a length.

Common mistakes

  • Mixing numbers and lengths like hypot(3, 4px), which is invalid.

Key points

  • All arguments have the same type, like all lengths or all numbers.
  • Distance is computed with hypot(), not sqrt() combined with pow() on lengths.
  • A fallback value comes first where hypot() is unsupported.

Interview questions

What does hypot() return in CSS?

It returns a value of the same type as the arguments, so hypot(30px, 40px) is '50px'.

Can hypot() take more than two arguments?

Yes. It accepts any number of arguments, so it also works for three-dimensional distances.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy