绝对定位的元素的父元素必须适应孩子的身高

时间:2019-02-20 05:18:51

标签: html css css-position absolute

<!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 的子代的高度,以便它不会显示在页脚上方

2 个答案:

答案 0 :(得分:1)

import
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;
}

答案 1 :(得分:1)

现在,在这里,我将“背景”设为相对div的绝对值,并将“绝对” div设为相对的div,以使其具有内容div的父元素高度。 在这种情况下,背景将始终采用父元素的高度和宽度,无论您可以放置​​多少文本,背景都不会与页脚重叠。希望对您有帮助

<!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>
public function crudsList()
{
     $cruds = Cache::remember('cruds',10, function(){
        return Crud::all();
     });
     return view('cruds_view',compact('cruds'));
}

    @foreach($cruds as $row)
        <tr>
           <td>{{$row->id}}</td>
           <td>{{$row->name}}</td>
           <td>{{$row->color}}</td>
           <td>{{$row->created_at}}</td>
           <td>{{$row->updated_at}}25</td>
           <td></td>
       </tr>
    @endforeach