简体   繁体   English

如何在桌子/ flex内以100%的高度放置图像?

[英]How to have an image inside table/flex with 100% height?

I'm struggling with getting my header area to behave the way I want when shrinking and enlarging the website. 在缩小和扩大网站时,我很难使标题区域表现出我想要的方式。 The header should be fully responsive. 标头应完全响应。

What I want 我想要的是

The header consists of three parts: 标头包含三部分:

|-------------------|----------------------------------------------|--------------|
||-----------------||                                              ||------------||
||  The site logo  ||    A text message                            ||Another logo||
||-----------------||                                              ||------------||
|-------------------|----------------------------------------------|--------------|

I want the rules to be like this: 我希望规则像这样:

  • The logo is 40% of the document width but can't be wider than 400 px. 徽标是文档宽度的40%,但不能超过400 px。 This is done with width and max-width . 这是通过widthmax-width Height is dynamic/auto. 高度是动态/自动。
  • The text message column is gonna be as wide as it can be, but don't have any fixed width. 文本消息列将尽可能宽,但没有任何固定宽度。
  • The other logo to the right is gonna be 100% height of the column, which is based of the height of the site logo to the left. 右边的另一个徽标将是该列的100%高度,该高度基于左侧站点徽标的高度。

The problem 问题

The problem is that it seems impossible to get that logo to the right to be 100% in height without being as big as the entire document. 问题在于,要使徽标的高度正确达到100%而又不像整个文档那么大,这似乎是不可能的。 I want it to have the height of the column as if the logo wasn't in there. 我希望它具有列的高度,好像徽标不在其中。

What I've tried 我尝试过的

I've tried both the table solution (" div with display:table ") and the flex grid solution ( div with display:flex ). 我已经尝试了解决方案(“ divdisplay:table ”)和弹性网格解决方案( divdisplay:flex )。 The latest mentioned is the one I'm currently struggling with. 最近提到的是我目前正在努力解决的问题。

 body { font-family: Roboto, Arial, Helvetica, sans-serif; font-size: 14px; color: #fff; background: #20262e; } .container { display: flex; flex-direction: row; } .col { vertical-align: middle; background: hsla(0,0%,100%,.1); margin:2px; padding:10px; } .col.logo { width: 40%; max-width: 400px; } .col.logo img { width: 100%; height: auto; } .col.game-host-logo img { width:auto; max-width: 100%; height: 100%; } 
 <div class="container"> <div class="col logo"> <img src="https://dromfemman.se/assets/img/dromfemman_logo.png" /> </div> <div class="col live-msg"> Text Message </div> <div class="col game-host-logo"> <img src="https://dromfemman.se/assets/img/ssl_logo.svg" /> </div> </div> 

There are two main problems here: 这里有两个主要问题:

  1. There is no fixed height on the flex container or image item. flex容器或图像项目上没有固定的高度。
  2. There is no fixed width on the flex item containing the image. 包含图像的弹性项目上没有固定的宽度。

Problem #1: There is no fixed height on the flex container or image item. 问题1:伸缩容器或图像项目上没有固定的高度。

Because there is no fixed height on the flex container, or the item containing the image ( .col.game-host-logo ), both are sized based on the size of the content ( height: auto default, per the spec ). 因为flex容器或包含图像的项目( .col.game-host-logo )上没有固定的高度,所以两者的大小都是根据内容的大小确定的( height: auto 根据规格 height: auto默认值)。

In this case, the content is an image with natural dimensions of 482 x 565 px. 在这种情况下,内容是自然尺寸为482 x 565像素的图像。

在此处输入图片说明

在此处输入图片说明


Problem #2: There is no fixed width on the flex item containing the image. 问题2:包含图像的弹性项目上没有固定的宽度。

Because there is no fixed width on the item containing the image, the image is again free to expand to its natural dimensions. 由于包含图像的项目上没有固定的宽度,因此图像又可以自由扩展到其自然尺寸。

By setting a width to the item, your problem appears to be resolved without violating any of your requirements. 通过设置项目的宽度,可以解决您的问题,而不会违反您的任何要求。

jsFiddle demo jsFiddle演示

 .container { display: flex; } .col.logo { width: 40%; max-width: 400px; } .col.logo img { max-width: 100%; height: auto; } .col.live-msg { flex: 1; /* consumes all free space */ } .col.game-host-logo { width: 20%; /* new */ } .col.game-host-logo img { max-width: 100%; } .col { background: hsla(0, 0%, 100%, .1); margin: 2px; padding: 10px; } body { font-family: Roboto, Arial, Helvetica, sans-serif; font-size: 14px; color: #fff; background: #20262e; } 
 <div class="container"> <div class="col logo"> <img src="https://dromfemman.se/assets/img/dromfemman_logo.png" /> </div> <div class="col live-msg"> Text Message </div> <div class="col game-host-logo"> <img src="https://dromfemman.se/assets/img/ssl_logo.svg" /> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM