绝对定位的元素的父元素绝对必须容纳子元素的高度
Parent element of an element absolutely positioned absolute must accomodate the height of the child
<!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>
绝对定位的元素显示在页脚上方。我想要的是具有 class Relative 的元素占据其具有 class Absolute 的子元素的高度 这样它就不会显示在页脚上。
function setHeight() {
let rel = document.querySelector(".Relative");
let abs = document.querySelector(".Absolute");
let hei = abs.scrollHeight;
hei += abs.offsetTop;
rel.style.height = hei + "px";
}
setHeight();
window.addEventListener("resize", setHeight);
.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;
background-color: #333;
}
.Absolute {
position: absolute;
left: 100px;
top: 150px;
border: 1px solid red;
}
<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>
现在,我将 "background" 设为相对 div 的绝对值,将 "absolute" div 设为相对值,这样它就会给出你的父元素高度内容div。
在这种情况下,背景将始终采用父元素的高度和宽度,无论您可以放置多少文本,它都不会与页脚重叠。希望对你有帮助
.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;
position: absolute;
height: 100%;
width: 100%;
z-index:0;
}
.Relative {
position:relative;
}
.Absolute {
position:relative;
z-index: 1;
}
h2 {
}
<!DOCTYPE html>
<html>
<head>
</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>
<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>
<!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>
绝对定位的元素显示在页脚上方。我想要的是具有 class Relative 的元素占据其具有 class Absolute 的子元素的高度 这样它就不会显示在页脚上。
function setHeight() {
let rel = document.querySelector(".Relative");
let abs = document.querySelector(".Absolute");
let hei = abs.scrollHeight;
hei += abs.offsetTop;
rel.style.height = hei + "px";
}
setHeight();
window.addEventListener("resize", setHeight);
.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;
background-color: #333;
}
.Absolute {
position: absolute;
left: 100px;
top: 150px;
border: 1px solid red;
}
<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>
现在,我将 "background" 设为相对 div 的绝对值,将 "absolute" div 设为相对值,这样它就会给出你的父元素高度内容div。 在这种情况下,背景将始终采用父元素的高度和宽度,无论您可以放置多少文本,它都不会与页脚重叠。希望对你有帮助
.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;
position: absolute;
height: 100%;
width: 100%;
z-index:0;
}
.Relative {
position:relative;
}
.Absolute {
position:relative;
z-index: 1;
}
h2 {
}
<!DOCTYPE html>
<html>
<head>
</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>
<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>