負責任地改變div尺寸保持高寬比當我給一個圖像百分之一的寬度或高度,它會增長/縮小,保持它的縱橫比,但如果我想要與另一個元素相同的效果,它有可能把寬度和高度用百分比綁在一起嗎?
3 回答

UYOU
TA貢獻1878條經驗 獲得超4個贊
padding-top
.wrapper {
width: 50%;
/* whatever width you want */
display: inline-block;
position: relative;
}
.wrapper:after {
padding-top: 56.25%;
/* 16:9 ratio */
display: block;
content: '';
}
.main {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
/* fill parent */
background-color: deepskyblue;
/* let's see it! */
color: white;
}
<div class="wrapper">
<div class="main">
This is your div with the specified aspect ratio.
</div>
</div>

慕娘9325324
TA貢獻1783條經驗 獲得超4個贊
<style>#aspectRatio{ position:fixed; left:0px; top:0px; width:60vw; height:40vw; border:1px solid; font-size:10vw;}</style><body><div id="aspectRatio">Aspect Ratio?</div></body>
vw
vh
- 3 回答
- 0 關注
- 426 瀏覽
相關問題推薦
添加回答
舉報
0/150
提交
取消