Родительский элемент абсолютно позиционированного элемента должен соответствовать росту дочернего элемента. - PullRequest
0 голосов
/ 20 февраля 2019

<!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>

Абсолютно позиционированный элемент отображается над нижним колонтитулом.Я хотел бы, чтобы элемент с классом Относительный поднялся на высоту своего дочернего элемента с классом Абсолютный , чтобы он не отображался поверх нижнего колонтитула.

Ответы [ 2 ]

0 голосов
/ 20 февраля 2019

Теперь здесь я делаю «фон» абсолютным относительно относительного div и делаю ваш «абсолютный» 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>
0 голосов
/ 20 февраля 2019

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>
...