【问题标题】:Div placed after a div with position relative overlaps with the relative div?放置在位置相对与相对div重叠的div之后的div?
【发布时间】:2020-03-02 01:30:54
【问题描述】:

我对元素的位置有点困惑。

代码如下:

<div class="header">

   





    * {
    	margin: 0;
    	padding: 0;
    	box-sizing: border-box;
    	font-family: "Montserrat", sans-serif;
    }
    
    .header {
    	width: 100%;
    	height: 55px;
    	border: 1px solid #e7e7e7;
    	background: #f1f0f4;
    }
    
    .nav {
    	text-align: right;
    }
    
    navbar ul li {
    	display: inline-block;
    	padding: 10px;
    	font-size: 15px;
    	margin: 5px;
    	font-weight: bold;
    }
    
    navbar ul li a {
    	color: black;
    	text-decoration: none;
    }
    
    navbar ul li:hover {
    	background: #d3d2d6;
    }
    
    #logo {
    	float: left;
    	text-decoration: none;
    	margin-left: 100px;
    }
    
    #header-img {
    	width: 100%;
    	height: 700px;
    	background: url('./programming.jpeg') no-repeat center /cover;
    	opacity: 0.6;
    }
    
    #text {
    	position: relative;
    	top: 10%;
    	text-align: center;
    	font-size: 40px;
    	color: rgb(0, 0, 0, 1);
    	font-weight: bold;
    }
    .body {
    }
    <div class="header">
            <navbar class="nav">
                <ul>
                    <li id="logo"><i class="fas fa-robot fa-lg"> TSV</i><li>
                    <li><a href="#">Home</a></li>
                    <li><a href="#">Blog</a></li>
                    <li><a href="#">About</a></li>
                    <li><a href="#">Contact</a></li>
                </ul>
            </navbar>
            <div id="header-img">
                    <p id="text">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Deserunt, mollitia?</p>
            </div>
        </div>
        <div class="body">
            <h1>TEST</h1>
        </div>

主要问题是 .body div 与 header-img div 重叠,我真的不明白为什么会发生这种情况。 header-img div 是相对定位的,我认为它不会破坏流程的元素。 谢谢!

【问题讨论】:

    标签: html css positioning


    【解决方案1】:

    这是因为您的 &lt;header&gt; 元素封装了您的 &lt;navbar&gt;&lt;div id="header-img"&gt;,但是您的 &lt;header&gt; 元素设置为 height: 55px;,所以您的 &lt;div id="header-image"&gt;height: 700px 溢出了&lt;header&gt;,导致此行为。

    .header { 中删除height: 55px;

    .header {
        width: 100%;
        height: 55px;  //REMOVE THIS
        border: 1px solid #e7e7e7;
        background: #f1f0f4;
    }
    

    【讨论】:

      【解决方案2】:

      发生这种情况是因为您将 height: 55px 定义为 .header 元素。 删除它,它应该可以正常工作。

      .header {
        width: 100%;
        height: 55px;
        border: 1px solid #e7e7e7;
        background: #f1f0f4;
      }
      

      您可以根据需要将此移除的高度显式设置为导航。

      .nav {
        text-align: right;
        height: 55px;
        width: 100%;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多