1

I'm looking for a solution how to draw a responsive square, where will be height given in %.

<div class="square"></div>

//CSS
.square{
  background: red;
  height: 49%;
  //and what else make this div square I found many solutions how to make it but it is dependent on width :(
}

viewport: <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">

and wrapper of this square is another div with this css:

  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  width: 100%;
  height: 100%;
balicekt
  • 615
  • 5
  • 14

2 Answers2

1

Aspect ratio can be controlled by using the padding-bottom trick.

Note however that this technique means that you cannot have additional inline content inside the child div.

div.parent {
  width: 80%;
  background: #c0ffee;
  margin: 25px auto;
}
.parent >div {
  width: 49%;
  padding-bottom: 49%;
  background: #000;
}
<div class="parent">
  <div></div>
</div>

If size in relation to the viewport is required the viewport units exist for this.

div {
  width: 49vh;
  height: 49vh;
  background: #c0ffee;
  margin: 25px auto;
}
<div>
</div>
Paulie_D
  • 107,962
  • 13
  • 142
  • 161
0

Height given in percentage is calculated on the basis of its total width. As width changes, height also changes.