Square DIV where height is equal to viewport

前端 未结 8 1301
我在风中等你
我在风中等你 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:52

    CSS3 has a way of doing this using vw, viewport width, and vh, viewport height. Using these measures, 100vw is the entire width of the viewport, and 100vh is the entire height. More information about relative css3 values and units here.

    As of writing this, the only support however is for Internet Explorer 9, so this is probably not what you're looking for, but is something good to keep in mind when future support follows.

    0 讨论(0)
  • 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)

    0 讨论(0)
提交回复
热议问题