[英]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.
标头应完全响应。
The header consists of three parts: 标头包含三部分:
|-------------------|----------------------------------------------|--------------|
||-----------------|| ||------------||
|| The site logo || A text message ||Another logo||
||-----------------|| ||------------||
|-------------------|----------------------------------------------|--------------|
I want the rules to be like this: 我希望规则像这样:
width
and max-width
. width
和max-width
。 Height is dynamic/auto. 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.
我希望它具有列的高度,好像徽标不在其中。
I've tried both the table solution (" div
with display:table
") and the flex grid solution ( div
with display:flex
). 我已经尝试了表解决方案(“
div
与display:table
”)和弹性网格解决方案( div
与display: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: 这里有两个主要问题:
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像素的图像。
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. 通过设置项目的宽度,可以解决您的问题,而不会违反您的任何要求。
.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.