gpt4 book ai didi

html - 带百分比的嵌套 Div 内的顶部/底部填充

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

我在处理嵌套 div 内的填充时遇到问题。

问题是我在顶部有一个横幅(小条),在它下面有一个内容更多的横幅。横幅的高度为 150 像素,我希望将整个空间的顶部和底部填充 10%。我还有一个包含在“bannerspace”div 中的“center”div,它将所有内容集中在一条直线上,以便于组织。

但是,每当我应用 padding-top: 10% 时,填充都会应用到整个 body 元素,使填充比我预期的大得多。

我花了几个小时寻找解决方案,甚至问过我的网络开发 friend ,但我无法得到有效的答案。我发现这可能是垂直填充的预期行为,但我不确定你们是否有解决方案。

我尝试过更改大多数元素的位置以及调整框大小的方法,但均无济于事。我也试过使用边距,但同样的问题仍然存在。

这是我的正文 HTML(与标题无关):

<body>
<div class="header">
<!--Header Bar (Top) - "Stats" on PD-->
<div class="center">
<div class="stat1">
Filler: <br>
Test
</div>
<div class="stat2">
Filler: <br>
Test
</div>
<div class="stat3">
Filler: <br>
Test
</div>
<div class="stat4">
Filler: <br>
Test
</div>
<div class="stat5">
Filler: <br>
Test
</div>
</div>
</div>
<div class="banner">
<!--Banner Bar (Middle) - "Banner" on PD-->
<div class="center">
<div class="bannerlogo">
Logo
</div>
<div class="bannerspace">
<div class="loginarea">
Filler
</div>
<div class="registerarea">
Filler
</div>
</div>
</div>
</div>
This will be the homepage!
<form name="input" action="LoginPage.html" method="get">
<input type="submit" value="Login">
</form>
<form name="input" action="AccountPage.html" method="get">
<input type="submit" value="Account">
</form>
<form name="input" action="OtherPage1.html" method="get">
<input type="submit" value="Other 1">
</form>
<form name="input" action="OtherPage2.html" method="get">
<input type="submit" value="Other 2">
</form>
<form name="input" action="OtherPage3.html" method="get">
<input type="submit" value="Other 3">
</form>
</body>

还有我的 CSS:

html {
height: 100%;
width: 100%;
margin: 0px;
font-family: "Roboto Normal 400", "Roboto", sans-serif;
font-size: 20px;
padding: 0px;
margin: 0px;

}

body {
height: 100%;
width: 100%;
margin: 0px;
text-align: center;
background: #DAFFDA;
padding: 0px;
text-align: center;
padding: 0px;
margin: 0px;
}


.center {
width: 90%;
height: 100%;
margin-left: 5%;
margin-right: 5%;
position: absolute;
}

.header {
height: 50px;
width: 100%;
border: 0px solid black;
background-color: #7C7C7C;
margin: 0px;
text-align: center;
border-bottom-width: 2px;
border-bottom-color: #3D3D3D;
position: relative;
}

.banner {
height: 150px;
width: 100%;
border: 0px solid black;
background-color: #6EFF81;
margin: 0px;
text-align: center;
border-bottom-width: 2px;
border-bottom-color: #3D3D3D;
position: relative;
}

.bannerlogo {
margin-top: 15px;
margin-bottom: 15px;
margin-left: 2%;
margin-right: 2%;
height: 80%;
width: 36%;
text-align: center;
float: left;
display: table-cell;
line-height: 120px;
vertical-align: middle;
border-width: 1px;
border-color: black;
border-style: solid;
position: absolute;
}

.bannerspace {
height: 100%;
width: 60%;
float: right;
padding-top: 10%;
padding-top: 10%;
}

.loginarea{
height: 80%;
width: 50%;
float: left;
}

.registerarea {
height: 80%;
width: 50%;
float: left;
}

抱歉打扰了。如果您能指出任何涵盖信息的指南/教程,我将不胜感激。

编辑:此外,重要的是要注意此行为仅在我使用百分比时发生。当我使用像素测量(最大 130 像素,因为字体大小为 20 像素)时,填充是从“横幅”类 div 的末尾添加的,这是正确的。

最佳答案

当使用 width % 值时,垂直填充或边距使用父级的宽度作为引用。

您可以尝试使用额外的元素或伪元素来填充您想要填充的区域,使用高度继承。

这将是空伪:

.banner {height:150px;}
.center, .bannerspace {height:100%;}
.bannerspace:before , .bannerspace:after {
content:'';
display:block;
height:10%;
}

也请注意 box-sizing 的使用。

关于html - 带百分比的嵌套 Div 内的顶部/底部填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24264769/

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