If you understand the logic behind it, using the object-fit
property is quite simple. Let's break it down with an example:
.box {
width:300px;
height:300px;
border:1px solid;
}
img {
width:100%;
height:100%;
object-fit:contain;
}
<div class="box">
<img src="https://picsum.photos/300/200?image=1069">
</div>
In this example, I've used a 300x200
image inside a 300x300
box, which stretches and breaks its original ratio. However, even after applying object-fit
, the image dimensions remain 300x300
.
According to the specification:
The object-fit property specifies how the contents of a replaced element should be fitted to the box established by its used height and width.
In simpler terms, visually we adjust the content of the image to fit within the space defined by the image itself.
Extending the same example with 50%
:
.box {
width:300px;
height:300px;
border:1px solid;
}
img {
width:50%;
height:50%;
object-fit:contain;
}
<div class="box">
<img src="https://picsum.photos/300/200?image=1069">
</div>
Now, the image size becomes 150x150
where the content maintains the contain effect.
This concept applies to all values of object-fit
.
Considering your scenario without object-fit
, the image behaves differently as shown below:
html,
body {
margin: 0;
height: 100%;
}
.page {
height: 100%;
display: flex;
}
.main-container {
flex: 1 1 0;
display: flex;
flex-direction: column;
}
.half-containers {
flex: 0 1 50%;
overflow: auto;
box-sizing: border-box;
border: 0.5px solid red;
display: flex;
}
.page-header {
flex: 0 0 auto;
background-color: #dcdcdc;
}
.page-footer {
flex: 0 0 auto;
background-color: #dcdcdc;
}
img {
/*object-fit: contain;*/
}
<div class="page">
<div class="main-container">
<div class="page-header">
This is a header
</div>
<div class="half-containers">
<img src='https://i.imgur.com/tqQvuFr.jpg' />
</div>
<div class="half-containers">
<img src='https://i.imgur.com/tqQvuFr.jpg' />
</div>
<div class="page-footer">
This is a footer
</div>
</div>
</div>
Introducing object-fit
does not alter the image size but only affects what is visible:
html,
body {
margin: 0;
height: 100%;
}
.page {
height: 100%;
display: flex;
}
.main-container {
flex: 1 1 0;
display: flex;
flex-direction: column;
}
.half-containers {
flex: 0 1 50%;
overflow: auto;
box-sizing: border-box;
border: 0.5px solid red;
display: flex;
}
.page-header {
flex: 0 0 auto;
background-color: #dcdcdc;
}
page-footer {
flex: 0 0 auto;
background-color: #dcdcdc;
}
img {
object-fit: contain;
}
<div class="page">
<div class="main-container">
<div class="page-header">
This is a header
</div>
<div class="half-containers">
<img src='https://i.imgur.com/tqQvuFr.jpg' />
</div>
<div class="half-containers">
<img src='https://i.imgur.com/tqQvuFr.jpg' />
</div>
<div class="page-footer">
This is a footer
</div>
</div>
</div>
The issue you are facing with the image having a width of 1024px
relates to the flex items not stretching beyond their content size due to the min-width
constraint. To resolve this, include min-width:0
to achieve the desired effect, containing the image within the flexbox layout area.
html,
body {
margin: 0;
height: 100%;
}
.page {
height: 100%;
display: flex;
}
.main-container {
flex: 1 1 0;
display: flex;
min-width: 0; /*added*/
flex-direction: column;
}
.half-containers {
flex: 0 1 50%;
overflow: auto;
box-sizing: border-box;
border: 0.5px solid red;
display: flex;
}
.page-header {
flex: 0 0 auto;
background-color: #dcdcdc;
}
.page-footer {
flex: 0 0 auto;
page-background-color: #dcdcdc;
}
img {
object-fit: contain;
min-width: 0; /*added*/
}
<div class="page">
<div class="main-container">
<div class="page-header">
This is a header
</div>
<div class="half-containers">
<img src='https://i.imgur.com/tqQvuFr.jpg' />
</div>
<div class="half-containers">
<img src='https://i.imgur.com/tqQvuFr.jpg' />
</div>
<div class="page-footer">
This is a footer
</div>
</div>
</div>
Another approach could be utilizing background-image
along with background-size:contain
, removing the need for min-width
constraints since there's no actual content involved:
html,
body {
margin: 0;
height: 100%;
}
.page {
height: 100%;
display: flex;
}
.main-container {
flex: 1 1 0;
display: flex;
flex-direction: column;
}
.half-containers {
flex: 0 1 50%;
overflow: auto;
box-sizing: border-box;
border: 0.5px solid red;
display: flex;
background:url(https://i.imgur.com/tqQvuFr.jpg) center/contain no-repeat;
}
page-header {
flex: 0 0 auto;
background-color: #dcdcdc;
}
page-footer {
flex: 0 0 auto;
background-color: dcdcdc;
}
<div class="page">
<div class="main-container">
<div class="page-header">
This is a header
</div>
<div class="half-containers">
</div>
<div class="half-containers">
</div>
<div class="page-footer">
This is a footer
</div>
</div>
</div>