gpt4 book ai didi

css - 100% 窗口高度 | 100% 父 div 宽度

转载 作者:行者123 更新时间:2023-11-28 13:23:41 24 4
gpt4 key购买 nike

我想创建一个 HTML 页面:

  • 水平居中显示
  • 整个窗口的高度都是白色背景
  • 包含固定的标题和可滚动的内容

我有两个与 {width: 100%} 和 {height: 100%} 相关的问题。

我的页眉是页面宽度的 100%,而我希望它是其父宽度的 100%。背景显示在窗口高度的 100% 处,但随后会随着内容向上滚动。

如果您能帮助我理解 CSS 在这两种情况下如何处理 100% 的值,我将不胜感激。我不是在寻求解决方法:我已经有了。我要求深入了解 CSS 的思维方式。

非常感谢,

詹姆斯

Here's a demo of the issue

这是基本的 HTML:

<!DOCTYPE html>
<head>
<title>Width & Height 100%</title>

<style>
html {
height:100%;
}

body {
background: #666;
height: 100%;
margin: 0;
}

#container {
position: relative;
height:100%;
background: white;
width: 400px;
margin: 0 auto;
padding: 0 0;
}

#header {
position:fixed;
z-index:100;
background:#ffe;
/* width:760px; */
width:100%;
height:64px;
}

#content {
position: absolute;
left:20px;
width:360px;
height:360px;
margin:64px 0 0 0;
background:#efe;
}
</style>
</head>

<body>
<div id="container">
<div id="header">
Fixed header
</div>

<div id="content">
Scrollable content
</div>
</div>
</body>
</html>

最佳答案

所有这些固定位置都会让您头疼。

关于宽度:盒子模型通常是问题所在。我开始将每个 CSS 的 body 高度和宽度设置为 100%,然后重置我的盒子模型以使其在浏览器之间匹配,并将我所有的填充应用到盒子的内部而不是外部:

/* Set box models to match across browsers. */
* {
box-sizing:border-box;
-moz-box-sizing:border-box;
webkit-box-sizing:border-box;
max-width:100%;
}

然后使用填充在容器上设置宽度,首先是移动宽度,然后是要覆盖的屏幕宽度:

#container {
padding: 0px 10px;
}
@media only screen
and (min-width : 700px) {

#container {
padding: 0% 30%;
}

}

如需完整布局,您可以访问我的网站: http://instancia.net/fluid-sticky-footer-layout/

我应该将移动位添加到该页面。 :)

关于css - 100% 窗口高度 | 100% 父 div 宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14405515/

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