I want to create a responsive <div>
that can change its width/height as the window’s width and height changes, while maintaining its aspect ratio and filling the maximum space available in a parent div with no overflow. The parent div is responsive, with height:100vh
and width:100vw
.
Are there any CSS rules that would allow me to maintain aspect ratio, and expand to fill this <div>
?
When the window width > window height:
I want the responsive div to have horizontal aspect-ratio [16/9] (horizontal video) and to take up maximum space in the 100vh parent div without overflow.
When the window width < window height: I want the responsive div to have vertical aspect-ratio [9/16] (vertical video) and to take up maximum space in the 100vh div without overflowing the div.
If there is a pure CSS solution to this, I would be so grateful.
Here is what I’ve tried so far:
.parent {
background: pink;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.child {
aspect-ratio: 16/9;
background: blue;
}
@media (max-aspect-ratio: 9/16) {
.child {
aspect-ratio: 9/16;
height: 100%;
}
}
@media (min-aspect-ratio: 16/9) {
.child {
aspect-ratio: 16/9;
background: green;
width: 100%;
}
}
<div class="parent">
<video class="child"></video>
</div>
I can’t make the video take up maximum space in the parent without overflow.
Thanks for reading.
2
Answers
If I have understood it properly, you want to rotate the video when it’s in portrait mode to fit a bigger height, which could be solved by a media query:
However, if this isn’t what you’re looking for, try to give more information on what you want to do (f.e. I want to keep the video horizontal but just adapt it to a portrait screen).
Note this answer has been edited in response to the OP’s comment
This snippet demonstrates the problem. The inner
<div>
stays within its container when the container is very wide or very tall, but not when the container is squarer.If the inside element is a replaced element instead of a
<div>
, we can useobject-fit: contain;
and the problem is trivial.The thing about replaced elements is that they have an intrinsic aspect ratio. It would be nice if the
object-fit
behaviour could also be used for a non-replaced element with anaspect-ratio
specified in CSS, but I don’t believe it can, at least not yet.aspect-ratio
is fairly new to CSS, so perhaps this will happen in time.But do you really need to make it work with a
<div>
? Perhaps the solution is to put your video directly inside your parent, rather than having another<div>
in-between?