我試圖創建具有一個響應矩形:與圖像擬合到最大尺寸響應矩形,同時保持縱橫比
height
即是width
background: linear-gradient
- 62%的矩形內是以垂直和水平方向居中且具有最大尺寸的圖像,所有圖像具有相同的
width: 400px
但是不同height
我以前做過:
- 用於獲取響應矩形我用這個方法:
.responsive-rectangle {
width: 100%;
max-width: 450px;
background: -webkit-linear-gradient(bottom, #0071B4, rgba(0, 113, 180, .8));
background: -moz-linear-gradient(bottom, #0071B4, rgba(0, 113, 180, .8));
background: -o-linear-gradient(bottom, #0071B4, rgba(0, 113, 180, .8));
background: linear-gradient(to top, #0071B4, rgba(0, 113, 180, .8));
}
.responsive-rectangle:before {
content: "";
display: block;
padding-top: 62%;
}
<div class="responsive-rectangle"></div>
- 對準內部矩形我已經使用
display: flex;
和text-align:center;
與.img-wrapper
形象:
.responsive-rectangle {
width: 100%;
max-width: 450px;
background: -webkit-linear-gradient(bottom, #0071B4, rgba(0, 113, 180, .8));
background: -moz-linear-gradient(bottom, #0071B4, rgba(0, 113, 180, .8));
background: -o-linear-gradient(bottom, #0071B4, rgba(0, 113, 180, .8));
background: linear-gradient(to top, #0071B4, rgba(0, 113, 180, .8));
display: flex;
text-align: center;
}
.responsive-rectangle:before {
content: "";
display: block;
padding-top: 62%;
}
.image-wrapper {
margin: auto;
}
img {
width: 100%;
height: 100%;
}
<div class="responsive-rectangle">
<div class="image-wrapper">
<img src="https://res.cloudinary.com/zeek/image/upload/v1429436724/whiterryxmsuesx78joy9n9sa.png" alt="">
</div>
</div>
這完美的作品的情況下,與圖像400px x 220px
, 但圖像有更大的height
不使用正確的寬高比:
.responsive-rectangle {
width: 100%;
max-width: 450px;
background: -webkit-linear-gradient(bottom, #0071B4, rgba(0, 113, 180, .8));
background: -moz-linear-gradient(bottom, #0071B4, rgba(0, 113, 180, .8));
background: -o-linear-gradient(bottom, #0071B4, rgba(0, 113, 180, .8));
background: linear-gradient(to top, #0071B4, rgba(0, 113, 180, .8));
display: flex;
text-align: center;
}
.responsive-rectangle:before {
content: "";
display: block;
padding-top: 62%;
}
.image-wrapper {
margin: auto;
}
img {
width: 100%;
height: 100%;
}
<div class="responsive-rectangle">
<div class="image-wrapper">
<img src="https://res.cloudinary.com/zeek/image/upload/v1444889083/o67qntlwitbxnqz5qyjn.png" alt="">
</div>
</div>
有沒有可以解決我的問題,任何方式?
編輯: 哦,我忘了注意,background-image
是不好的解決方案,因爲它不支持SEO。
作品完美,謝謝!我只改變了'height:auto',它按預期工作 – zooblin