html-图像伸缩项目不会在Flexbox中缩小高度

html-图像伸缩项目不会在Flexbox中缩小高度,第1张

概述我有一个Flex容器,其中包含一个img flex项目.我希望此img忽略其固有高度(90px),以便高度可以缩小以匹配同级的flex项目高度(根据默认的align-items:拉伸样式)..container { border: 1px solid; display: flex; } .item { border: 1px sol

我有一个Flex容器,其中包含一个img flex项目.

我希望此img忽略其固有高度(90px),以便高度可以缩小以匹配同级的flex项目高度(根据默认的align-items:拉伸样式).

.container {    border: 1px solID;    display: flex;}.item {    border: 1px solID;}.content {    wIDth: 200px;    height: 50px;    background-color: hotPink;}
<div >    <img  src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRu-3yBSd2b6JCOMcGVSOFf8X49QB3Ik-OI87gKEMwWLrdJxP5qOErmZQ">    <div >        <div ></div>    </div></div>

如果将img交换为div,则可以看到所需的行为:

.container {    border: 1px solID;    display: flex;}.item {    border: 1px solID;}.dynamicHeightContent {    wIDth: 120px;    height: 100%;    background-color: green;}.content {    wIDth: 200px;    height: 50px;    background-color: hotPink;}
<div >    <div >        <div ></div>    </div>    <div >        <div ></div>    </div></div>

我尝试了img的最小高度:0,无济于事.

>适用于img但不适用于div的特殊行为是什么?
>我们如何选择退出img的特殊行为,使其表现得与其他flex项目(例如div)类似?如果没有选择退出的方法,是否有建议的解决方法?

请注意,尽管img不会缩小其高度,但它确实会增大:

.container {    border: 1px solID;    display: flex;}.item {    border: 1px solID;}.content {    wIDth: 200px;    height: 300px;    background-color: hotPink;}
<div >    <img  src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRu-3yBSd2b6JCOMcGVSOFf8X49QB3Ik-OI87gKEMwWLrdJxP5qOErmZQ">    <div >        <div ></div>    </div></div>

注意:我很乐意忽略img的宽高比.我计划避免通过object-fit:cover使img倾斜.最佳答案请注意,在您的示例中,项目是d性项目,而不是内容-您应在此处检查项目的拉伸行为.

How can we opt out of img’s special behavIoUr so that it behaves like
other flex items (such as div)?

它的行为类似于其他flex项目-< img>作为d性项目可能不是很有用,但是拉伸行为可以正常工作:

>如果图像的高度大于项目的高度,则项目会延伸到图像的高度
>如果图像的高度小于项目的高度,则图像会拉伸到内容的高度,从而破坏其宽高比.

请参见下面的演示:

.container {  border: 1px solID;  display: flex;}.item {  background: cadetblue;}.content {  wIDth: 200px;  background-color: hotPink;}img {  display: block;}
<h2>Small content</h2><div >  <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRu-3yBSd2b6JCOMcGVSOFf8X49QB3Ik-OI87gKEMwWLrdJxP5qOErmZQ">  <div >    <div >some text here some text here some text here </div>  </div></div><br/><h2>Large Content</h2><div >  <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRu-3yBSd2b6JCOMcGVSOFf8X49QB3Ik-OI87gKEMwWLrdJxP5qOErmZQ">  <div >    <div >some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here </div>  </div></div>

I have trIEd min-height: 0 on the img,to no avail.

沿伸缩方向将min-height:0赋予列flexBox以覆盖默认的自动行为(对于行方向,该属性为min-wIDth)-在横轴上不起作用.

您可以在下面查看详细信息和一些示例:

> Flexbox affects overflow-wrap behavior
> Flexbox resize and scrollable overflow
> Why don’t flex items shrink past content size?

将图像包装在容器中

现在包装< img>在div中看到相同的情况-拉伸行为再次良好:

.container {  border: 1px solID;  display: flex;}.item {  background: cadetblue;}.content {  wIDth: 200px;  background-color: hotPink;}img {  display: block;}
<h2>Small content</h2><div >  <div >    <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRu-3yBSd2b6JCOMcGVSOFf8X49QB3Ik-OI87gKEMwWLrdJxP5qOErmZQ"></div>  <div >    <div >some text here some text here some text here </div>  </div></div><br/><h2>Large Content</h2><div >  <div >    <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRu-3yBSd2b6JCOMcGVSOFf8X49QB3Ik-OI87gKEMwWLrdJxP5qOErmZQ"></div>  <div >    <div >some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here </div>  </div></div>

现在的区别是,您现在可以在图像上成功使用对象适配(如果它是d性项目,则无法正常工作):

.container {  border: 1px solID;  display: flex;}.item {  background: cadetblue;}.content {  wIDth: 200px;  background-color: hotPink;}img {  display: block;  wIDth: 100%;  height: 100%;  object-fit: cover;}
<h2>Small content</h2><div >  <div >    <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRu-3yBSd2b6JCOMcGVSOFf8X49QB3Ik-OI87gKEMwWLrdJxP5qOErmZQ"></div>  <div >    <div >some text here some text here some text here </div>  </div></div><br/><h2>Large Content</h2><div >  <div >    <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRu-3yBSd2b6JCOMcGVSOFf8X49QB3Ik-OI87gKEMwWLrdJxP5qOErmZQ"></div>  <div >    <div >some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here </div>  </div></div>

I want this img to disregard its intrinsic height (90px) so that the
height may shrink to match the sibling’s flex item height

忽略图像高度的唯一方法是在图像包装器上使用定位:

>绝对相对于包装纸定位图像,
>您可以给图像包装器指定宽度,或者给项目设置flex:1以水平获得可用宽度的一半.

请参见下面的演示:

.container {  border: 1px solID;  display: flex;}.item {  background: cadetblue;  flex: 1; /* equal wIDth for items */}.content {  wIDth: 200px;  background-color: hotPink;}.item:first-child { /* image container */  position: relative;}img {  display: block;  wIDth: 100%;  height: 100%;  object-fit: cover;  position: absolute; /* position absolutely */  top: 0;  left: 0;}
<h2>Small content</h2><div >  <div >    <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRu-3yBSd2b6JCOMcGVSOFf8X49QB3Ik-OI87gKEMwWLrdJxP5qOErmZQ"></div>  <div >    <div >some text here some text here some text here </div>  </div></div><br/><h2>Large Content</h2><div >  <div >    <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRu-3yBSd2b6JCOMcGVSOFf8X49QB3Ik-OI87gKEMwWLrdJxP5qOErmZQ"></div>  <div >    <div >some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here </div>  </div></div>
总结

以上是内存溢出为你收集整理的html-图像伸缩项目不会在Flexbox中缩小高度 全部内容,希望文章能够帮你解决html-图像伸缩项目不会在Flexbox中缩小高度 所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1105489.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存