The sqrt()
CSS function is an exponential function that returns the square root of a number.
The function pow(x, 0.5)
is equivalent to sqrt(x)
.
Since December 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
The sqrt()
CSS function is an exponential function that returns the square root of a number.
The function pow(x, 0.5)
is equivalent to sqrt(x)
.
/* A <number> value */ width: calc(100px * sqrt(9)); /* 300px */ width: calc(100px * sqrt(25)); /* 500px */ width: calc(100px * sqrt(100)); /* 1000px */
Returns a <number>
which is the square root of x
.
x
is +∞
, the result is +∞
.x
is 0⁻
, the result is 0⁻
.x
is less than 0
, the result is NaN
.This example shows how you can use the sqrt()
function to calculate sizes.
<div class="boxes"> <div class="box">50px</div> <div class="box one">100px</div> <div class="box two">150px</div> <div class="box three">200px</div> </div>
Here we are using CSS custom properties to define the sizes to be used. First, we declare the first size (--size-0
), which is then used to calculate the other sizes.
--size-1
is calculated by multiplying the value of --size-0
(50px) by the square root of 4 (2), which results in 100px.--size-2
is calculated by multiplying the value of --size-0
(50px) by the square root of 9 (3), which results in 150px.--size-3
is calculated by multiplying the value of --size-0
(50px) by the square root of 16 (4), which results in 200px.:root { --size-0: 50px; --size-1: calc(var(--size-0) * sqrt(4)); /* 100px */ --size-2: calc(var(--size-0) * sqrt(9)); /* 150px */ --size-3: calc(var(--size-0) * sqrt(16)); /* 200px */ }
The sizes are then applied as the width
and height
values of the selectors.
.one { width: var(--size-1); height: var(--size-1); } .two { width: var(--size-2); height: var(--size-2); } .three { width: var(--size-3); height: var(--size-3); }
Specification |
---|
CSS Values and Units Module Level 4 # exponent-funcs |
Desktop | Mobile | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | |
sqrt |
120 | 120 | 118 | 106 | 15.4 | 120 | 118 | 80 | 15.4 | 25.0 | 120 |
© 2005–2024 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/sqrt