【发布时间】:2020-11-06 05:59:28
【问题描述】:
总之,我创建了一个位置为:relative 的容器 div(父级),然后添加了 3 个位置为:absolute 的子 div。我现在正在尝试在所有这些之下添加另一个 div,即网站的下一部分。但是现在下一个 div 出现在第一个主要的“父”div 下。从在这里和谷歌的无尽搜索中,我虽然具有相对位置的主 div 不会破坏流程,但显然它确实这样做了,否则我不会发布。
我现在想在父级之外创建另一个 div,这样它就可以放在第一个 div 下,并创建一个漂亮的、有争议的网站。请查看我的 CSS 并帮助我理解为什么相对元素中的绝对元素会扰乱流程。 (我是 CSS 新手,因此感谢任何专业提示!)
Here is an image of the website so you get a feel
*{
margin: 0;
padding: 0;
border: 0;
}
body {
font-family: 'Noto Sans HK', sans-serif;
}
/* Arranging the parent and child elements so
images can overlap */
.child {
top: 0;
}
.child-1 {
position: absolute;
left: 0;
z-index: 100;
}
.child-2 {
position: absolute;
right: 0;
z-index: 1;
}
.child-3 {
position: absolute;
padding-top: 38%;
left: 0;
}
#parent {
position: relative;
height: auto;
}
.hero-text {
position: absolute;
text-align: center;
right: 10vw;
top: 28vw;
z-index: 9;
font-size: 3.5vw;
float: right;
color: white;
}
/* Responsive viewport area,
Logo resize based on the screen size */
#logo_png {
max-width: 25vw;
}
#hero_img {
max-width: 85vw;
}
#green_circle_png {
max-width: 40vw;
}
/* NAV BAR STYLING */
#container {
position: absolute;
z-index: 900;
width: 95%;
margin: 0 auto;
}
nav {
float: right;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
margin-left: 5vw; /* margin-left obly touches the left margin, not L & R */
padding-top: 25px;
position: relative;
}
nav a {
color: white;
text-decoration: none;
text-transform: uppercase;
font-size: 1.4vw;
}
nav a:hover {
color: black;
}
.p1 {
color: #f5f7ff;
font-size: 10vw;
}
#test {
position: relative;
}
<body>
<div id="parent">
<div class="child child-1">
<h1>
<a href='THIS WILL BE LINK TO HOME PAGE'>
<img id="logo_png" src="C:\Users\rebec\Desktop\LBS WEBSITE\Images\lbs_blue_circle_logo_1500x1500.png" alt="Little Big Scientists"/>
</a>
</h1>
</div>
<div class="child child-2">
<h1>
<img id="hero_img" src="Images/circle_hands_lbsphoto.png" alt="Little Big Scientists"/>
</h1>
</div>
<div class="child child-3">
<h1>
<img id="green_circle_png" src="Images/green_circle_lbswebsite.png" alt="Little Big Scientists"/>
</h1>
</div>
<div class="hero-text">
<p>We’re on a mission to teach,
<br>guide, and empower the next
<br> generation of scientists
</p>
</div>
<!-- Div for Nav Bar-->
<div id="container">
<nav>
<ul>
<li><a href="#">Mission</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Events</a></li>
<li><a href="#">Donate</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</div>
</div>
<div id="test">
<h2 class="p1">Inspiring Education</h2>
</div>
<h2 class="p1">HELP MEEEE</h2>
</body>
【问题讨论】:
-
欢迎来到 Stack Overflow,您能否指出各个元素并解释您需要的输出?
-
您的
position: absolute元素会扼杀流程。不是您的实际定位元素。去掉allposition: absolute就可以正常滚动网站了。提示:你的布局根本不需要position: absolute。 -
欢迎来到 Stack Overflow!绝对定位是一种非常糟糕的网页布局方法。它非常不灵活,并且有更好、响应速度更快的选项。查看LearnLayout.com
-
@cloned 我使用 absolute 来强制每个部分中的图像,但我可以尝试返回并重新设计它。哎呀
-
也许张贴一张关于你的设计应该是什么样子的图片。