11

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?

Alice Chan
  • 2,591
  • 2
  • 15
  • 14
  • 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 Answers1

4

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,015
  • 6
  • 32
  • 57