The hypot() operate takes an inventory of values and returns the sq. root of the sum of their squares.
.hypotenuse {
width: hypot(30vmin, 40vmin); /* 50vmin */
}
More often than not, we’ll cross it two arguments: hypot(A, B). Consider it like a strategy to full the Pythagorean theorem the place we give the operate the other and adjoining sides of a triangle, and the longest aspect is what it returns.
The hypot() operate is outlined within the CSS Values and Items Module Degree 4 specification
Syntax
hypot(#)
The hypot() features takes a comma-separated listing of calculations () that should resolve to a , , or .
Arguments
/* Takes dimensions */
width: hypot(30px, 40px); /* 50px */
width: hypot(12rem, 160px)
/* Takes percentages */
width: hypot(60%, 80%);
width: hypot(30px, 10%)
/* Takes numbers */
hypot(9, 12) /* 15 */
hypot(15, 20) /* 25 */
/* Takes unfavourable values */
width: hypot(-50px, 120px); /* 130px */
width: hypot(-90%, -120%); /* 150% */
width: hypot(9, 40); /* 41 */
is a comma-separated listing of calculations that resolve to both a , , or .
Mixing and values is okay so long as they’ve a constant kind — like 25% and 5rem within the width property, or else the operate is invalid.
Lastly, the results of hypot() will likely be of the identical kind as its arguments, so hypot( returns a , and hypot( returns a .
3 ways to see it
There are three most important methods we will consider hypot():
1. Whereas boring, we will consider hypot() as simply its underlying system, which squares every argument, sums it, and takes the sq. root of the outcome. You realize, similar to you discovered in class: “A squared, plus B squared equals C squared.
This explains why it might take unfavourable values, since they get squared right into a optimistic worth.
2. Nevertheless, hypot() has an entire lot extra interpretation past a system. If given two values, hypot(A, B) returns the size of the hypotenuse of a proper triangle with sides A and B.
As an alternative of the perimeters of a triangle, we will additionally consider each arguments because the coordinates of a degree on an XY aircraft. During which case, hypot(x, y) returns the size from the origin to that time in area.
3. That mentioned, hypot() can take greater than two arguments, so we’ll lengthen the final definition past two dimensions the place one thing like hypot(a1, a2, ..., an) is the size of a degree to its origin in an n-dimensional area. For instance, seen in three dimensions, hypot(x, y, z) would appear to be this:

We’ll take a look at primary utilization with two values to assist illustrate how we’d put this into observe. Plus, CSS isn’t too eager on larger dimensions past 2D.
Fundamental Utilization
Let’s begin with a not-so-practical instance for hypot(), simply to showcase the way it works. Think about we need to solid a line from one level on the display to our mouse place within the viewport.
Step one is to let CSS know the mouse’s x and y coordinates by means of the following JavaScript snippet, which saves every coordinate within the --m-x and --m-y variables:
window.addEventListener("pointermove", (occasion) => {
let x = occasion.clientX;
let y = occasion.clientY;
doc.documentElement.type.setProperty("--m-x", `${Math.spherical(x)}px`);
doc.documentElement.type.setProperty("--m-y", `${Math.spherical(y)}px`);
});
From right here, we have to reply two questions: (1) How lengthy ought to that line be? (2) what angle ought to we rotate it?
The primary query is the place hypot() is available in, for the reason that size of that line is identical as hypot(x, y).
.line {
/* Locations line on the prime left nook */
place: fastened;
prime: 0;
left: 0;
width: hypot(var(--m-x), var(--m-y));
peak: 5px; /* Thickness of the road */
}
With this, we’ve got a line that will get longer because the mouse strikes away from the top-left nook.
Now, we’ve got to rotate it by a sure angle, so we’ll use the atan2() operate. With out going an excessive amount of into particulars, atan2(y, x) provides us the angle between a degree within the aircraft and the horizontal axis.
.line {
/* ... */
transform-origin: left heart;
remodel: rotate(atan2(var(--m-y), var(--m-x)));
}
Observe that we cross first y after which x into atan2() slightly than the opposite manner round.
And now the road ought to completely connect with our mouse.
If we need to as an alternative join the road from the middle to the mouse, we must first place our line on the heart:
.line {
place: fastened;
prime: 50%;
left: 50%;
/* ... */
}
Then we tweak our JavaScript a bit in order that our coordinates additionally begin from the middle as an alternative of the top-left nook. This may be finished by subtracting half the window’s innerWidth and innerHeight from the respective coordinate earlier than passing them onto CSS.
x = x - window.innerWidth / 2;
y = y - window.innerHeight / 2;
Detecting When the Cursor is Close to
So, now that we all know the fundamentals of how hypot() works, we will use it for a extra sensible state of affairs. Not so way back, Daniel Schwarz wrote about :close to(), a possible new pseudo-selector that might match a component when the cursor is near it by a given distance:
button:close to(3rem) {
/* Pointer is inside 3rem of the button */
}
This could possibly be used to use some impact to a button every time the person is close to it, like highlighting or scaling it a bit. Sadly, on the time of writing, we don’t have :close to() on any browser, however we will already simulate it utilizing each hypot() and type queries.
Once more, our first step is to cross the mouse coordinates to CSS through JavaScript. Nevertheless, this time we’ll measure the mouse place from the middle of the button. To do that, we’ll:
- Subtract from every coordinate the button’s
offsetLeftandoffsetTop, which strikes the origin to the button’s top-left nook. - Subtract half the button’s
clientWidthandclientHeight, which strikes the origin proper to the button’s heart.
const button = doc.querySelector("button");
window.addEventListener("pointermove", (occasion) => {
let x = occasion.clientX;
let y = occasion.clientY;
x = x - button.offsetLeft - button.clientWidth / 2;
y = y - button.offsetTop - button.clientHeight / 2;
doc.documentElement.type.setProperty("--m-x", `${Math.spherical(x)}px`);
doc.documentElement.type.setProperty("--m-y", `${Math.spherical(y)}px`);
});
Again to CSS! Let’s outline two variables: (1) --near that holds the space from the mouse to the button, and (2) --limit that defines the restrict the place we take into account the mouse close to the button.
:root {
--near: hypot(var(--m-x), var(--m-y));
--limit: 200px;
}
What’s left is to create a mode question that matches every time --near is smaller than --limit, that means our cursor is inside the restrict.
@container type(--near < var(--limit)) {
button {
scale: 1.025;
box-shadow: #e07a5f 0px 6px 25px 0px;
}
}
Earlier than we take a look at the outcome, it’s price checking ion your browser helps container type queries.
It’s Extra Than Syntactical Sugar
Since hypot() returns the sq. root of a sum of squares, the largest math nerds of us would count on that the next two expressions can be equal:
hypot(A, B)
sqrt(pow(A, 2) + pow(B, 2))
Nevertheless, if we attempt to swap the previous for the latter, we’ll discover that it received’t work most instances. The 2 formulation aren’t interchangeable every time hypot() takes both a or , since all different exponential features solely settle for values. In different phrases, we’d like hypot() if we’re working with particular kinds of values, notably and
The primary purpose behind that is clashing expectations. As the spec says, if in our stylesheet 1rem equals the default 16px, pow(1rem, 2) would end in 1rem (16px once more), regardless that they’re the identical worth. In the meantime, pow(16px, 2) leads to 256px. hypot() inputs and outputs are at all times constant, so these sorts of sudden outcomes don’t happen!
Observe: The spec goes into an extended rationalization on why hypot() takes each dimensions and numbers.
Edge Instances
- If any of the arguments is
infinityor-infinity, then the result’sinfinity. - If, for any purpose, you need to enter just one worth, it’ll return that very same worth as a optimistic.
Specification
The hypot() operate is outlined within the CSS Values and Items Module Degree 4 specification, which is presently in Editor’s Draft.
