height: calc(100%) not working correctly in CSS

后端 未结 7 968
面向向阳花
面向向阳花 2020-12-02 15:09

I have a div that I want to fill the whole height of the body less a set number in pixels. But I can\'t get height: calc(100% - 50px) to work.

The reas

7条回答
  •  半阙折子戏
    2020-12-02 15:52

    I was searching why % doesn't seem to work. So, I tested out using 100vh instead of just setting it at 100% it seems that 100vh works really well across almost all browsers/devices.

    example: you want to only display the top div to the user before it scrolls, like a hero banner module. But, at the top of the page is a navbar which is 68px in height. The following doesn't work for me at all doing just %

    height: calc(100% - 68px); 
    

    There's was no change. The page just stayed the same. However, when swapping this to "vh" instead it works great! The div block you assign it too will stay on the viewer's device hight only. Until they decide to scroll down the page.

    height: calc(100vh - 68px); 
    

    Change the +/- to include how big your header is on the top. If your navbar is say 120px in height then change 68px to 120px.

    Hope this helps anyone who cannot get this working with using normal height: calc();

提交回复
热议问题