【发布时间】:2019-02-20 14:23:12
【问题描述】:
<!DOCTYPE html>
<html>
<head>
<style>
.Background {
background-image:url("https://images.unsplash.com/photo-1517524285303-d6fc683dddf8?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1275&q=80");
height: 220px;
background-size: 100% 100%;
background-repeat: no-repeat;
}
.Relative {
position:relative;
}
.Absolute {
position: absolute;
left: 100px;
top: 150px;
border: 1px solid red;
}
h2 {
}
</style>
</head>
<body>
<div class="Main">
<div class="Relative">
<div class="Background"></div>
<div class="Absolute">
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
<h1>Hi</h1>
</div>
</div>
<footer>With absolute positioning, an element can be placed anywhere on a page. The heading below is placed 100px from the left of the page and 150px from the top of the page.</footer>
</div>
绝对定位的元素显示在页脚上方。我想要的是具有类 Relative 的元素占据其具有类 Absolute 的子元素的高度,这样它就不会显示在页脚上.
【问题讨论】:
-
你想要什么..我不明白:D
-
class Relative正在从Backgroundchild 获取高度,它具有`高度:220px;` 所以class="Absolute"显示在页脚上。 -
你想在图片和页脚之间显示绝对值吗?
-
父元素不取任何绝对子元素的高度,如果要设置父元素的高度只能通过js实现
-
如果您希望背景始终位于文本之后,则将背景设为绝对,将文本设为相对。
标签: html css css-position absolute