gpt4 book ai didi

html - 如何在 div 标签之间留出垂直空间?

转载 作者:太空狗 更新时间:2023-10-29 12:33:50 24 4
gpt4 key购买 nike

我有三个 div 标签,我想在 div 标签之间垂直留出空间,并且还希望我的第一个 div 标签是固定的。

当我将我的第一个 div 位置设置为不是固定的时,我可以能够制作垂直空间

<html>
<head>
<title>div</title>
</head>
<body style="margin: 0; padding: 0;">
<div style="width:100%; background-color:Lime;">
<div style="width:100%; height:10%; background-color:Blue;">
a
</div>
<div style="width:70%; height:100%; background-color:Gray; margin: auto; margin-top: 2em; margin-bottom: 2em;">
b
</div>
<div style="width:100%; height:5%; background-color:Aqua;">
c
</div>
</div>
</body>
</html>

当我将“div a”位置更改为固定时,“div a”和“div b”都从边距顶部:2em。

<html>
<head>
<title>div</title>
</head>
<body style="margin: 0; padding: 0;">
<div style="width:100%; background-color:Lime;">
<div style="width:100%; height:10%; background-color:Blue; position: fixed;">
a
</div>
<div style="width:70%; height:100%; background-color:Gray; margin: auto; margin-top: 2em; margin-bottom: 2em;">
b
</div>
<div style="width:100%; height:5%; background-color:Aqua;">
c
</div>
</div>
</body>
</html>

请帮我把我的“div a”设为固定并在“div a和b”之间留出空格 .

最佳答案

为了让div.a(固定的那个)保持在页面的顶部,添加top: 0; 如果你想让它保持在顶部其余内容,包括z-index: 2;

为了在 div.adiv.b 之间添加间距,您必须在 周围放置一个容器 div >div.b 并对其进行适当的填充。如果您只是将填充放在主容器 div 上,它将偏移 div.a

如果可能,将确定的高度设置为 div.a,而不是百分比,因为这将使 div.b 的垂直对齐及其容器更容易.这样您就可以将 div.bmargin-top 设置为 div.aheight

下面是为了更好的可读性而重构的 CSS 和 HTML:

/* CSS */
* {
margin: 0;
padding: 0;
}
div {
width: 100%;
}
div.container {
height: 100%;
}
div.container,
div.b-container {
background-color: lime;
}
div.a {
height: 70px;
background-color: blue;
position: fixed;
top: 0;
z-index: 2;
}
div.b-container {
position: relative;
padding-top: 2em;
margin-top: 70px;
}
div.b-container div.b {
width: 70%;
height: 100%;
background-color: gray;
margin: auto;

margin-bottom: 2em;
}
div.c {
height: 5%;
background-color: aqua;
}
<!-- HTML -->
<div class="container">
<div class="a">
a
</div>
<div class="b-container">
<div class="b">b
</div>
</div>
<div class="c">
c
</div>
</div>

关于html - 如何在 div 标签之间留出垂直空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26240714/

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