【发布时间】:2016-03-09 08:39:20
【问题描述】:
我目前正在构建一个视差页面,但在将元素定位在部分上时遇到了问题。因此我的问题是绝对定位。
有没有办法让绝对位置只在一个部分而不是整个页面/视口中起作用?
我做了一个非常简单的例子来说明我正在尝试做的事情:https://jsfiddle.net/zu2epxxq/1/
如您所见,#second 部分内的段落应位于该部分内,而不是主要部分 #first。显然这是使用绝对定位的副作用。
我如何以干净和良好的方式解决这个问题?
HTML:
<section id="first">
<div>
<p>This is a test</p>
</div>
</section>
<section id="second">
<div>
<p>More text that has to be pushed arund in this section</p>
</div>
</section>
CSS:
html,
body {
height: 100%;
color: #fff;
font-size: 16px;
margin: 0;
padding: 0;
}
section {
min-height: 100%;
}
#first {
background-color: #000;
}
#second {
background-color: #ddd;
}
section > div {
bottom: 0;
margin: 0;
font-size: 4em;
position: absolute;
transform: translate(-50%, 50%);
left: 50%;
}
【问题讨论】:
-
我没有看到
position: relative的任何absolute元素。你可能需要这个。
标签: html css parallax absolute