14

有没有办法用 CSS 清除绝对定位的元素?我正在创建一个页面,我需要网站的每个部分(部分元素)都绝对定位,并且我想在这些元素下方应用一个包含内容的页脚。
尝试相对定位页眉和页脚以查看是否会考虑总高度,但页脚仍会“被困”在节元素下方。有任何想法吗?

<header style="position: relative;"></header>

<div id="content" style="position: relative;">

    <section id="a" style="position: absolute;"></section>

    <section id="b" style="position: absolute;"></section>

    <section id="c" style="position: absolute;"></section>

    <section id="d" style="position: absolute;"></section>

    <section id="e" style="position: absolute;"></section>

</div>

<footer style="position: relative;"></footer>
4

4 回答 4

22

绝对定位的元素不再是布局的一部分——父项不知道绝对定位的子元素有多大。您需要自己设置“内容”的高度,以确保它不会与页脚重叠。

于 2012-10-10T14:15:49.453 回答
6

不要在布局中使用绝对定位的元素,因为这些元素会从正常流程中移除,并且不再影响它们周围的元素。而且它们不受其他元素的影响。

在条件允许的情况下,使用绝对定位在容器内移动元素。

对于浮动元素,我建议您使用一种称为 clearfix 的特定清除技术。我虔诚地使用它。

http://nicolasgallagher.com/micro-clearfix-hack/

http://jsfiddle.net/necolas/K538S/

于 2012-10-10T15:08:17.590 回答
2

有同样的问题,所有的都是绝对定位的,但是让第一个是相对的,至于高度发生变化的响应式布局,它确实有助于跟踪元素的高度变化,注意在这种情况下所有元素都具有相同的高度:

.gallery3D-item {
    position: absolute;
    top: 0;
    left: 0;
}
.gallery3D-item:first-of-type {
    position: relative;
    display: inline-block;
}

于 2016-11-30T09:16:04.993 回答
0

我发现了一个简单的解决方案,可能无法涵盖所有​​可能的问题,但至少它解决了我的问题。

HTML:

<p>Content before</p>
<div class="offset-wrapper">
    <div class="regular">
        <img src="https://www.gravatar.com/avatar/bdf0bf75e96fa18e57769865ebeb9a6e?s=48&d=identicon&r=PG" />
    </div>
    <div class="special">
        <img src="https://www.gravatar.com/avatar/bdf0bf75e96fa18e57769865ebeb9a6e?s=48&d=identicon&r=PG" />
    </div>
</div>
<p>Content after</p>

CSS:

.offset-wrapper {
  background: green;
  position: relative;
  width: 100px;
}
.offset-wrapper .regular {
  visibility: hidden;
}
.offset-wrapper .special {
  bottom: -15px;
  left: -15px;
  position: absolute;
}
于 2018-10-05T07:57:26.553 回答