gpt4 book ai didi

html - 使用 CSS 更改浏览器高度时调整 div 宽度

转载 作者:行者123 更新时间:2023-11-28 03:28:03 25 4
gpt4 key购买 nike

几天来一直在寻找一种仅使用 CSS 的解决方案来保持 div 的纵横比。重要的部分是 div 高度应与浏览器窗口高度相同(不滚动且不 overflow hidden ),宽度百分比应调整以保持纵横比正确。到目前为止我发现的所有内容(主要是填充技巧)都使用父元素的宽度来保持纵横比,并在 div 下方添加大量额外空间,尤其是在大型显示器的全屏显示中。

真的在努力避免 javascpript。

这是我的基本设置:

::Edit::添加链接到 jsfiddle -- http://jsfiddle.net/SUbYB/::Edit 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>

提前感谢您的帮助!

最佳答案

什么是使用:

display: table;
display: table-row;
display: table-cell;

在您的情况下(除了您的代码之外)看起来像:

.aspect-container{
display: table-row;
}
.aspect-critical-content{
display:table-cell;
height:100%;
}

.aspect-container2{
display:table-row;
height:100px;
}

和 html:

<div class="super-container">
<div class="aspect-container">
<div class="aspect-critical-content">
some text
</div>
</div>
<div class="aspect-container2">
<div>
text2
</div>
</div>
</div>

我添加的第二行只是为了展示如何轻松地在同一个容器中调整另一个 block 的高度。

The code on plunker.

关于html - 使用 CSS 更改浏览器高度时调整 div 宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19884827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com