Square DIV where height is equal to viewport

前端 未结 8 1315
我在风中等你
我在风中等你 2020-12-01 06:13

I need to create a DIV where width=height, and height=100% of the viewport (which, obviously, is variable).

In other words, a perfectly sq

8条回答
  •  时光说笑
    2020-12-01 06:54

    One additional trick I came up with to help partially solve this problem, for anybody who stumbles across this page... Run the following code in your page's onload event:

    $('body').css('font-size',$(window).height()/100)
    

    This means the css "em" unit is equal to 100th of your page height- You can now arbitrarily use this in your css file to size any item relative to your viewport height. This is more generic than the other solutions listed here- And most likely you want to size all of the elements of your page to take into account your viewport height. For instance, if you now want to create your square you can just write:

    #square{width:100em;height:100em}
    

    Of course, you'll also have to take this into account for any text on your page (since this trick affects the font size)

提交回复
热议问题