Maximize While Maintaining Aspect Ratio
Maximize element size within container bounds while maintaining fixed aspect ratio.
Structure
Nested containers with absolute positioning for aspect ratio constraint.
<div class="container">
<div class="inner">
<div class="aspect-ratio-lock">
<!-- Your contents -->
</div>
</div>
</div>
Aspect Ratio Scaling
Combine aspect-ratio
with object-fit: contain
on absolutely positioned element to scale within parent bounds.
.container {
position: relative;
border: 1px solid #aaa;
width: 100%;
height: 100%;
}
.inner {
position: absolute;
inset: 0;
max-width: 100%;
max-height: 100%;
aspect-ratio: 16 / 9;
object-fit: contain;
}
.aspect-ratio-lock {
width: 100%;
height: 100%;
background-size: 10px 10px;
background-attachment: fixed;
background-image: repeating-linear-gradient(315deg, #aaa 0, #aaa 1px, transparent 0, transparent 50%);
}