I have a div (#outer) that's placed in the middle left section of the web browser (Chromium). It's width can change as the window resizes. The outer div has another div inside that I want to resize with a fixed aspect ratio (16:9) while always staying in the bounds of the outer div.
For example, if the outer div resizes to tall and thin, the inner div should fit the full width of the outer div while maintaining the proportional height. If the outer div resizes to short and wide, the inner div should fit the full height of the outer div with proportional width. The inner div shouldn't stretch outside of the the outer div's bounds and there shouldn't be scrollbars.
Essentially, I want to accomplish what was asked here, but contained within another dynamically changing div, and not just the viewport. I tried using the viewport vw and vh, but haven't gotten it to work within a div. I'm currently using a before element with padding-top: 56.25%, which works only with respect to the width of the outer div and overflows the outer div if the width is much larger than the height.
Ideally, I'd like to use pure CSS, but I'm willing to write javascript if there is no alternative. Any ideas? Thanks!
Relevant HTML:
<div id="outer">
<div class="box"><div id="camera_view"></div></div>
</div>
Relevant CSS:
Using a before element:
.box {
position:relative;
width: 100%;
height: 100%;
}
.box:before {
content: "";
display: block;
padding-top: 56.25%;
background: #A2E8A7;
}
.box-content {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
#outer {
display: flex;
position: relative;
width: 100%;
align-items: center;
}
Using viewport units: (edit: this as a separate test file)
.box {
width: 100vw;
height: 56.25vw;
background: #FF8AB1;
max-height: 100vh;
max-width: 177.78vh;
margin: auto;
position: absolute;
left:0;right:0;
}
#outer {
width: 50%;
height: 50%;
/* edit: 50% only used to mimic the approximate size of #outer */
}
.box-content {
background: #FF8AB1;
}