Danield's answer is good, but it can only be used when the div fills the whole viewport, or by using a bit of calc
, can be used if the width and height of the other content in the viewport is known.
However, by combining the margin-bottom
trick with the method in the aforementioned answer, the problem can be reduced to just having to know the height of the other content. This is useful if you have a fixed height header, but the width of the sidebar, for example, is not known.
body {_x000D_
margin: 0;_x000D_
margin-top: 100px; /* simulating a header */_x000D_
}_x000D_
_x000D_
main {_x000D_
margin: 0 auto;_x000D_
max-width: calc(200vh - 200px);_x000D_
}_x000D_
_x000D_
section {_x000D_
padding-bottom: 50%;_x000D_
position: relative;_x000D_
}_x000D_
_x000D_
div {_x000D_
position:absolute;_x000D_
background-color: red;_x000D_
top: 0;_x000D_
left: 0;_x000D_
bottom: 0;_x000D_
right: 0;_x000D_
}
_x000D_
<main>_x000D_
<section>_x000D_
<div></div>_x000D_
</section>_x000D_
</main>
_x000D_
Here it is in a jsfiddle using scss, which makes it more obvious where the values come from.