爲了保持div的寬高比,一直在尋找僅有CSS的解決方案的幾天時間。重要的部分是div高度應該是瀏覽器窗口的高度(不滾動和不隱藏溢出),並且寬度百分比應該調整以保持縱橫比正確。到目前爲止我發現的所有東西(主要是填充技巧)都使用父元素的寬度來維持方面,並在div下面增加了很多額外空間,尤其是在大屏幕上的全屏幕中。使用CSS更改瀏覽器高度時調整div寬度
真的試圖避免javascpript。
這裏是我的基本設置:
::編輯::添加鏈接的jsfiddle - http://jsfiddle.net/SUbYB/ ::編輯2 ::只是使用jQuery來處理基於高度的div的設置寬度。謝謝大家!
樣式
body, html{
width: 100%;
height: 100%;
max-height: 100%;
max-width: 100%;
}
.super-container{
position: relative;
width: 69.8%;
height: 95%;
max-width: 2008px;
margin: 0 auto;
padding: 0 15.1% 0 15.1%;
background: rgba(200,200,200,.2);
}
.aspect-container{
position: relative;
display: block;
height: 95%;
margin: 0 auto;
background: rgba(200,200,200,.4);
}
.aspect-critical-content{
position: absolute;
top:0; right: 0; bottom: 0; left: 0;
background: rgba(200,0,0,.2);
}
和HTML
<html>
<head>
</head>
<body>
<div class="super-container">
<div class="aspect-container">
<div class="aspect-critical-content">
</div>
</div>
</div>
</body>
</html>
感謝提前任何幫助!
你能提供一個小提琴或蹲跳? –
http://jsfiddle.net/SUbYB/ jsfiddle鏈接到上面的代碼 –
你可以嘗試使用calc,如果跨瀏覽器對你來說不是太重要:https://developer.mozilla.org/en-US/docs/Web/CSS/calc?redirectlocale = en-US&redirectslug = CSS%2Fcalc –