简体   繁体   English

CSS Flexbox图像不保留宽高比

[英]CSS Flexbox images not preserving aspect ratio

I'm trying to learn to use Flexbox. 我正在尝试学习使用Flexbox。 I have a page with two images separated by a bar in the middle, stacked top to bottom. 我有一个页面,其中两个图像由中间的横条隔开,上下堆叠。 I want the bar to stay the same size regardless of the browser and the two images to resize when the browser shrinks (keeping the original image size if there is extra space. I'm enforcing this through max-height on the containing divs). 无论浏览器如何,我都希望条形保持不变,并且当浏览器缩小时,两个图像也要调整大小(如果有额外空间,请保持原始图像大小。我通过包含div的max-height来强制执行此操作)。 I can get the images to resize height-wise, but the width doesn't change to maintain aspect ratio. 我可以使图像在高度方向上调整大小,但宽度不会改变以保持纵横比。

I've read a couple similar questions and tutorials, but I can't seem to apply their solutions to fit my scenario. 我已经阅读了几个类似的问题和教程,但是我似乎无法应用他们的解决方案来适应我的情况。 This solution seems to be the closest to my needs: http://goo.gl/iE6Wbh 该解决方案似乎最符合我的需求: http : //goo.gl/iE6Wbh

Here is what I have so far. 这是我到目前为止所拥有的。 I don't know why it's causing the content to scroll on codepen either... http://codepen.io/anon/pen/VLvxgo 我不知道为什么会导致内容在Codepen上滚动... http://codepen.io/anon/pen/VLvxgo

 <style>
    #container
{
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    flex-wrap: nowrap;
    justify-content: center;
    align-items: center;
}

.topbottom
{
    flex: 3;
    display: flex;
}

.topbottom.top
{
    max-height: 93px;
}

.topbottom.bottom
{
    max-height: 152px;
}

.topbottom > img
{
    width: auto;
    height: 100%;
}

#dividerbar
{
    background-color: blue;
    width: 100%;
    height: 50px;
    margin: 20px 0;
}
    </style>

    <div id="container">
          <div class="topbottom top"><img src="http://placekitten.com/g/200/93" /></div>

        <div id="dividerbar">Hello world</div>

        <div class="topbottom bottom"><img src="http://placekitten.com/g/152/152" /></div>
    </div>

Any thoughts? 有什么想法吗?

I'm currently working on a similar problem and it seems to be a bit tricky. 我目前正在处理类似的问题,这似乎有些棘手。 I found a solution which works in safari, but won't work in chrome and firefox. 我找到了一种适用于野生动物园的解决方案,但不适用于Chrome和Firefox。 They ignore the 他们忽略了

max-height: 100%;

property. 属性。 Check this out and resize browser. 签出并调整浏览器大小。

Although i'm not 100% sure what you are trying to achieve (your codepen doesn't change anyhow when resizing the browser window), i've build this one . 尽管我不是100%不确定您要实现的目标(调整浏览器窗口大小时您的Codepen不会改变),但我已经构建了这一版本

Let me know if it's the right direction 让我知道这是正确的方向

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

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