CSS get height of screen resolution

后端 未结 9 1248
后悔当初
后悔当初 2020-12-02 12:10

I\'m having a hard time getting the height of lower screen resolution because my screen resolution is 1920x1080.

Does anyone know how to get height and width of the

9条回答
  •  自闭症患者
    2020-12-02 12:55

    You can bind the current height and width of the screen to css variables: var(--screen-x) and var(--screen-y) with this javascript:

    var root = document.documentElement;
    
    document.addEventListener('resize', () => {
      root.style.setProperty('--screen-x', window.screenX)
      root.style.setProperty('--screen-y', window.screenY)
    })
    

    This was directly adapted from lea verou's example in her talk on css variables here: https://leaverou.github.io/css-variables/#slide31

提交回复
热议问题