Getting the iPhone X safe area using JavaScript
Asked Answered
G

1

22

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?

Goad answered 4/11, 2017 at 15:36 Comment(1)
@Scott How is this a duplicated? I have same question and the duplicated link gives no solution to this question.Refluent
T
17

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")
Transarctic answered 16/2, 2021 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.