gpt4 book ai didi

html - 百分比 CSS 不准确

转载 作者:行者123 更新时间:2023-11-28 07:29:16 26 4
gpt4 key购买 nike

我的代码有问题:

 @import url(http://fonts.googleapis.com/css?family=Open+Sans:400,300&subset=latin,latin-ext);
body {
background-image: url("img/background.jpg");
background-size: 100% auto;
background-repeat: no-repeat;
margin: 0;
height: 100%;
width: 100%;
}
.container {
width: 100%;
height: 100%;
}
.header {
display: table;
width: 100%;
position: fixed;
height: 10%;
background-color: rgba(255, 255, 255, 0.95);
top: 0;
}
.content {
position: fixed;
height: 84%;
width: 100%;
top: 10%;
padding: 2%;
}
.footer {
position: fixed;
z-index: 2;
bottom: 0;
left: 0;
width: 100%;
height: 6%;
background-color: #263238;
}
<html>

<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>

<section class="container">

<section class="header"></section>



<section class="content">

<div class="index_news">
aaa
</div>

<div class="index_top">
ccc
</div>

</section>

<section class="footer"></section>

</section>

</body>

</html>

带有 CONTENT 类的部分应该准确地位于 HEADER 和 FOOTER 之间,但在页面上它比它应该的要大。你知道为什么吗?

最佳答案

使用 border-box 值来调整框的大小,这样 padding 不会影响外部宽度,而是相对于边框。此外,您的页脚位于 fixed 底部,这会让人产生内容不适合中间的错觉,但如果您删除 height: 84%,您的内容 div 大小是合适的;,否则它将占据容器高度的 84%。

.content {
position: fixed;
height: 84%;
width: 100%;
top: 10%;
padding: 2%;



/* box sizing - brower compatible */
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}

代码片段:

@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,300&subset=latin,latin-ext);
body {
background-image: url("img/background.jpg");
background-size: 100% auto;
background-repeat: no-repeat;
margin: 0;
height: 100%;
width: 100%;
}
.container {
width: 100%;
height: 100%;
}
.header {
display: table;
width: 100%;
position: fixed;
height: 10%;
background-color: rgba(255, 255, 255, 0.95);
top: 0;
}
.content {
position: fixed;
height: 84%;
width: 100%;
top: 10%;
padding: 2%;



/* use the border box for the box sizing, in this way the padding will not affect the outer width, instead it will be relative to the border */
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.footer {
position: fixed;
z-index: 2;
bottom: 0;
left: 0;
width: 100%;
height: 6%;
background-color: #263238;
}
<html>

<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>

<section class="container">

<section class="header"></section>



<section class="content">

<div class="index_news">
aaa
</div>

<div class="index_top">
ccc
</div>

</section>

<section class="footer"></section>

</section>

</body>

</html>

关于html - 百分比 CSS 不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31637362/

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