My current challenge involves solving a seemingly simple problem. Imagine I have a layout consisting of a header, content, and footer. Within the content section, there is a row of items that fill all available space. Each item has equal width and contains one child element.
The goal is for the children elements to perfectly fit within their parent containers, expanding to utilize maximum space without scaling up - similar to what object-fit: scale-down
does for images.
Let's consider the sizes of the elements that need to be accommodated:
200 x 300
1920 x 1080
1080 x 1920
Here is the desired outcome:
https://i.sstatic.net/Oaxe6.png
I am facing difficulties in achieving this result; larger elements either overflow the container or are clipped.
html,
body {
background-color: #000;
height: 100%;
margin: 0;
padding: 0;
}
.layout {
display: flex;
flex-direction: column;
height: 100%;
}
.top,
.bottom {
background-color: #444;
flex-shrink: 0;
height: 50px;
}
.middle {
background-color: #222;
flex-grow: 1;
overflow: hidden;
}
.items {
display: flex;
gap: 20px;
height: 100%;
overflow: hidden;
}
.item {
align-items: center;
background-color: #666;
display: flex;
flex-grow: 1;
justify-content: center;
overflow: hidden;
}
.block {
background-color: #f80;
background-image: linear-gradient(45deg, orange, purple);
height: 100%;
width: 100%;
}
.block1 {
max-height: 200px;
max-width: 300px;
}
.block2 {
max-height: 1080px;
max-width: 1920px;
}
.block3 {
max-height: 1920px;
max-width: 1080px;
}
To summarize, I have rectangles with specific dimensions that need to scale down but not up while maintaining aspect ratio. I have experimented with the aspect-ratio
property without success. Is it possible to achieve this without JavaScript calculations?