hypot()
Example
.line {
width: sqrt(pow(var(--dx), 2) + pow(var(--dy), 2));
}.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(), notsqrt()combined withpow()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.