我在semiHero类中添加了display:block;
。为什么不占用空间并使带有hr的容器低于整个div?我希望半英雄占用空间,以便将容器推下。
Here is the image of the situation.
以下是相关代码:
.semiHero {
background-image: url("../assets/semiHero.png");
height: 50%;
width: 100%;
background-position: center;
background-repeat: scroll;
background-size: cover;
position: absolute;
display: block;
}
<div class="semiHero">
<div class="container" id="NavMain">
<nav class="navbar navbar-custom navbar-expand-lg navbar-light">
<a class="navbar-brand" href="#">Logo</a>
<button class="navbar-toggler custom-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav"
aria-expanded="false" aria-label="Toggle navigation" id="hamburger">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse justify-content-end" id="navbarNav">
<ul class="navbar-nav navbar-custom">
<li class="nav-item active">
<a class="nav-link" href="#">Start
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About Me</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Work</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact</a>
</li>
</ul>
</div>
</nav>
<div class="hero-text">
<h1>May I Introduce Myself?</h1>
<hr>
<h2>Frontend Design | UI/UX Design</h2>
<br>
</div>
</div>
<button id="goAhead">Go Ahead</button>
</div>
<!-- MainSection -->
<div class="container">
<div class="row">
<div class="col" id="Name">
<h1>
Raphael Lechner.
</h1>
</div>
</div>
</div>
最佳答案
position: absolute
相对于其最接近的祖先定位特定元素。
这就是为什么将元素从页面流中删除并因此不占用任何空间的原因。
关于html - 为什么整个div都不占用空间?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49335770/