0

In CSS, if I want to set a div's height to its width, I can use padding-bottom:100%. However, I want to set a certain div's height to the viewport height, and have the div's width resize so that the div is square. Is there a method of doing so in CSS?

knosmos
  • 636
  • 6
  • 17

1 Answers1

4

If you aim to set its height to the viewport height, you can as well set its width to the viewport height:

.square {
  height: 100vh;
  width: 100vh;
}

In case you're not familiar with this unit: 1vh represents one percent of the view height.

Edit: depending on how responsive you want your design to be, you can also make use of the 1vmin unit, which represents 1 percent of the smallest side of your screen.

.square {
  height: 100vmin;
  width: 100vmin;
}

Regardless of whether your website is viewed in portrait or landscape mode, the square will always max out its size without causing the page to scroll.

soimon
  • 2,400
  • 1
  • 15
  • 16