2017-04-18 74 views
0

我在<figure>内有图像。我想在窗口大小调整时,图像具有响应行为,我希望始终能够观察图像的高度和宽度100%。我希望整个图像总能被看到。在高度和宽度内的响应式图像<figure>

http://jsfiddle.net/ckdm0joj/

figure.figure_container { 
 
    position: relative; 
 
    width: 100%; 
 
    max-height: 250px; 
 
    border-radius: 6px; 
 
    -moz-border-radius: 6px; 
 
    -webkit-border-radius: 9px; 
 
    overflow: hidden; 
 
    margin-top: 10px; 
 
    margin: 0 auto; 
 
} 
 

 
.figure_container img { 
 
    width: 100%; 
 
}
<figure class='figure_container'> 
 
    <img src='https://lumiere-a.akamaihd.net/v1/images/dory_characters_0afa6e45.jpeg?region=0%2C0%2C1200%2C778' /> 
 
</figure>

+0

这是你想要做的吗? http://jsfiddle.net/ckdm0joj/1/最大高度:250px上图不会让图像100%宽度没有伸展它或溢出它 –

+0

@GCyrillus谢谢,但是,我希望图像的宽度为100% ,以及在容器中,并且高度是容器的100%。 – yavg

+0

您是否还想保持图像宽高比? – Stickers

回答

0

是的,你可以做到这一点。

替换以下CSS此:

.figure_container img{ 
    max-width: 100%; 
    max-height: inherit; 
    width: auto; 
} 

通过添加inheritmax-height财产,你excerly告诉CSS不作比它的父元素的图像heigher。所以,如果你改变了.figure-container高度350px图像的max-height也将350px

继承关键字指定属性应该继承其父元素的值 。

编辑看完您的意见:

如果你希望你充满其容器widthwidth: 100%取代width: auto。然而,这将舒展你的形象,因为.figure-containermax-width250px

.figure_container img{ 
    max-width: 100%; 
    max-height: inherit; 
    width: 100%; 
} 

figure.figure_container{ 
 
    position: relative; 
 
    width: 100%; 
 
    max-height: 250px; 
 
    border-radius: 6px; 
 
    -moz-border-radius: 6px; 
 
    -webkit-border-radius: 9px; 
 
    overflow: hidden; 
 
    margin-top: 10px; 
 
    margin: 0 auto; 
 
} 
 

 
.figure_container img{ 
 
    max-width: 100%; 
 
    max-height: inherit; 
 
    width: 100%; 
 
}
<figure class='figure_container'> 
 
    <img src='https://lumiere-a.akamaihd.net/v1/images/dory_characters_0afa6e45.jpeg?region=0%2C0%2C1200%2C778' /> 
 
</figure>

+0

太棒了,但我需要的图像适合100%的数字的宽度,在这种情况下,这是100% – yavg

0

如果你不介意的话,看看你的形象是拉伸,弯曲可以是这样: http://jsfiddle.net/ckdm0joj/2/

figure { 
 
    display:flex; 
 
    max-height:250px; 
 
    border-radius:6px; 
 
    overflow:hidden; 
 
    width:100%; 
 
    margin:0; 
 
    padding:0 
 
} 
 
img { 
 
    flex:1; 
 
} 
 
body { 
 
margin:0; 
 
}
<figure class='figure_container'> 
 
    <img src='https://lumiere-a.akamaihd.net/v1/images/dory_characters_0afa6e45.jpeg?region=0%2C0%2C1200%2C778' /> 
 
</figure>

+0

谢谢,我重视你的努力,但在这种情况下图像是不完全看到(宽度) – yavg

+0

??它怎么没有完全看到?这是这里唯一的内容.... exept的半径角落。你甚至可以设置一个保证金http://jsfiddle.net/ckdm0joj/3/你认为在这里失踪了什么? –