gpt4 book ai didi

html - 相对定位部分出现在奇怪的地方

转载 作者:太空宇宙 更新时间:2023-11-04 09:04:45 25 4
gpt4 key购买 nike

这是可笑的基础,但我正在为相对定位的 div 的放置而苦苦挣扎。

我有一个 100vh 高的绝对标题,其中包含标题和标题,后跟两个部分元素,这两个元素都是相对的。第一个具有 100vh 的“顶部”值,将其放在标题的正下方。我假设第二个会简单地跟在第一个之后,但它却将自己附加到标题标题。

我已将页面提炼成一个片段来演示问题。

/* -------------------- UNIVERSAL TYPES -------------------- */

section {
position: relative;
width: 100%;
padding: 40px 0;
}

/* -------------------- HEADER -------------------- */

header {
position: absolute;
height: 100vh;
width: 100%;
background: #111111;
top: 0;
}

header .content {
width: auto;
max-width: 500px;
padding: 10px;
margin: 0 auto;
margin-top: 40vh;
text-align: center;
color: #ffffff;
}

header .content .headline {
font-size: 100px;
margin-bottom: 0px
height:100px;
margin-bottom: 20px
}

header .content .caption {
position: relative;
display: block;
font-size: 20px;
}

/* -------------------- UNIVERSAL CONTENT -------------------- */

.pagecontent {
position: relative;
width: 1000px;
margin: 0 auto;
padding: 30px;
}

.pagecontent h2 {
font-size: 80px;
}

.pagecontent p {
display: block;
width: 100%;
font-size: 20px;
}

/* -------------------- INDIVIDUAL SECTIONS -------------------- */

#About {
top: 100vh;
background-color: #CCC;
color: #333333;
}

#Portfolio {
background-color: #333;
color: #CCC;
}
<head>
<link rel="stylesheet" href="https://unpkg.com/purecss@0.6.2/build/pure-min.css" integrity="sha384-UQiGfs9ICog+LwheBSRCt1o5cbyKIHbwjWscjemyBMT9YCUMZffs6UqUTd0hObXD" crossorigin="anonymous">
</head>
<body>
<header>
<div class="content">
<div class="headline">Header.</div>
<div class="caption">Caption on dark background. This header section is 100vh tall and positioned absolutely.</div>
</div>
</header>
<section id="About">
<div class="pagecontent">
<h2> About section header </h2>
<p>All subsequent sections, starting with this one, are relative. My problem is that this section is obscured by the "Portfolio" section, which refuses to add on to the end of the page, and instead inserts itself immediately following the header caption. Duis aliquam finibus sagittis.</p>
</div>
</section>
<section id="Portfolio">
<div class="pagecontent">
<h2>Portfolio section header.</h2>
<p>This section refuses to add on to the end of the page, and instead inserts itself immediately following the header caption.</p>
</div>
</section>
</body>

同样,我知道这可能有一个非常简单的解决方案(我对现代 CSS 还是很陌生),但我已经挠头很久了。

最佳答案

这是因为 position: absolute 属性从默认呈现的主流中删除了内容,并将其放置在您定位的位置。正是由于这个原因,第二个元素无法识别前一个元素并从原始位置开始。

* {
margin: 0;
padding: 0;
box-sizing: border-box;
color: #fff;
font-size: 20px;
}

header {
height: 200px;
background: red;
position: absolute;
top: 0;
left: 0;
width: 100%;
}

section {
position: relative;
top: 200px;
height: 300px;
background: blue;
}
<header>This is absolute</header>
<section>This is Relative but aligned by giving TOP: 200px<br>Just to tell where RED DIV ends</section>

关于html - 相对定位部分出现在奇怪的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42524391/

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