你所拥有的是合乎逻辑的,你只需要了解它是如何object-fit
工作的。让我们从一个更简单的例子开始:
.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>
如您所见,我使用了300x200
在框内拉伸的图像,300x300
因此我打破了它的比例,如果您检查图像的宽度/高度,您将看到它的尺寸仍然是300x300
(应用前的尺寸object-fit
)。
从规范:
object-fit 属性指定被替换元素的内容应如何适合由其使用的 height 和 width 建立的框。
基本上,我们在视觉上改变图像的内容,使其适合图像建立的空间。object-fit
不会更改图像的大小,而是使用该大小作为参考来更改其内部内容。
让我们用同样的例子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>
现在图像的尺寸为150x150
,在其中我们更改内容以具有包含效果。
所有值都会发生相同的逻辑
.box {
width:300px;
height:300px;
border:1px solid;
}
img {
width:50%;
height:50%;
}
<div class="box">
<img src="https://picsum.photos/300/200?image=1069" style="object-fit:contain;">
</div>
<div class="box">
<img src="https://picsum.photos/300/200?image=1069" style="object-fit:cover;">
</div>
在你的例子中,你有同样的事情。没有object-fit
图像如下
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>
添加object-fit
不会改变它的大小,只会改变我们看到的:
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>
1024px
现在,另一个问题是,您的图像的min-width
宽度为 min-width:0
这样做您将不再有溢出问题,然后您的图像将包含在 flexbox 布局定义的区域内。
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;
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>
background-image
考虑到你也可以有相同的输出,因为没有更多的内容background-size:contain
,你不必再为约束而烦恼min-width
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>