I have found this css code that allows me to keep a div's aspect ratio.
.container {
width: 100%;
height: 100%;
background-color: red;
position: absolute;
}
.wrapper {
width: 100%;
/* whatever width you want */
display: inline-block;
position: relative;
top: 50%;
transform: translate(0%, -50%);
}
.wrapper:after {
padding-top: 56.25%;
/* 16:9 ratio */
display: block;
content: '';
}
.main {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
/* fill parent */
background-color: deepskyblue;
/* let's see it! */
color: white;
}
See this JSFiddle example: https://jsfiddle.net/1uyo07tg/3/
I would like a css-only solution (without the use of VW or VH) to keep this aspect ratio even when the parent is wider than the aspect ratio in question (in this case, 16:9).
In other words, I would like the blue-colored div to stay at 16:9 ratio even when the parent (.container) is stretched wider than 16:9.
Clarifying - i would like some css-only solution, to have a child div stay always in a fixed ratio, centered vertically and horizontally, no matter the size or aspect ratio of the parent div, and without using vw,vh. I am pretty sure this needs JS code (which I have), but just wanted to see if someone has a css-only neat trick for it.
Bottom line - looking for this functionality in CSS only, no vh or vw.
Hope that makes sense. Any ideas?
Thanks in advance, Sa'ar