We have safe-area-inset-left
, safe-area-inset-right
, safe-area-inset-top
, and safe-area-inset-bottom
in CSS, but how can we get those values using JavaScript?
Asked
Active
Viewed 8,000 times
19

Alice Chan
- 2,814
- 2
- 16
- 16
-
2@Scott How is this a duplicated? I have same question and the duplicated link gives no solution to this question. – Bryan Chen Feb 01 '18 at 03:29
1 Answers
13
From https://benfrain.com/how-to-get-the-value-of-phone-notches-environment-variables-env-in-javascript-from-css/. You can find more context in the link.
Within CSS
:root {
--sat: env(safe-area-inset-top);
--sar: env(safe-area-inset-right);
--sab: env(safe-area-inset-bottom);
--sal: env(safe-area-inset-left);
}
Within JS
getComputedStyle(document.documentElement).getPropertyValue("--sat")

Kaya Toast
- 5,267
- 8
- 35
- 59