【问题标题】:Why are these two elements overlapping? CSS issue为什么这两个元素重叠? CSS 问题
【发布时间】:2020-06-18 10:51:04
【问题描述】:

很难弄清楚为什么这两个 html 元素会重叠。下面是各个元素的 HTML 和 CSS 文件。我在这里搜索了很多线程,但没有一个解决方案有效。谢谢!

main.css:

    #titlesection {
  height: auto;
  width: 100%;
  margin: auto;
  padding: 0px;
  margin-top: 25px;
  margin-bottom: 25px;
  position: absolute;
  display: block;
}

#contentdiv {
  top: 30%;
  left: 2%;
  width: 97%;
  margin: auto;
  height: 100%;
  position: absolute;
  display: block;
}

#title {
  padding-top: 50px;
  text-align: center;
  font-size: 60px;
  letter-spacing: 6px;
  display: block;
  margin-bottom: 1%;
  height: auto;
}

#titlesubheading {
  margin-top: 10px;
  margin-bottom: 10px;
  font-size: 30px;
  text-align: center;
  letter-spacing: 7px;
  display: block;
  height: auto;
}

#socialmedia {
  width: 97%;
  display: block;
}

#smtable {
  width: 10%;
  float: right;
}

#aboutheader {
  width: 95%;
  margin: auto;
  margin-top: 5%;
  padding: 10px;
  font-size: 30px;
  display: block;
  position: static;
}

#about {
  width: 95%;
  margin: auto;
  margin-top: 1%;
  padding: 0px;
  height: auto;
  font-size: 20px;
  display: block;
}

#abouttext {
  width: 60%;
  height: auto;
  display: block;
  margin-left: 10px;
  min-height: 140px;
}

index.html:

<body>
    <section id="titlesection">
      <div id="title">
        Abdullah Rehmat
      </div>
      <div id="titlesubheading">
        Freelance Developer
      </div>
      <div id="socialmedia">
        <table id="smtable">
          <tbody>
            <tr>
              <td class="a">
                <a href="mailto: rehmat.dev@gmail.com">
                  <img
                    src="{{url_for('static', filename='images/email.png')}}"
                    width="33"
                    height="33"
                  />
                </a>
              </td>
              <td class="a">
                <a href="https://twitter.com/ARehmat20" target="_blank">
                  <img
                    src="{{url_for('static', filename='images/twitter0.svg')}}"
                    width="50"
                    height="50"
                  />
                </a>
              </td>
              <td class="a">
                <a href="https://github.com/10CodeDev" target="_blank">
                  <img
                    src="{{url_for('static', filename='images/github.png')}}"
                    width="30"
                    height="30"
                  />
                </a>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </section>

    <div id="contentdiv">
      <div id="aboutheader">
        About:
      </div>

这是问题的图片。 aboutheader 与 titlesubheading 重叠:

【问题讨论】:

  • 嗨,阿卜杜拉。看看你的 HTML-Sample,它在最后被切断了。有了完整的示例,SO 上的人可以更好地帮助您。
  • 现在编辑

标签: html css overlap


【解决方案1】:

这是因为您将这两个元素都作为position: absolute;。您可以通过删除来修复它。将您的 CSS 更新为:

编辑:同样为了您的信息,位置可让您在特定上下文中重新定位元素。 position: absolute; 从页面左上角定位一个元素,或者如果它的父元素有 position: relative; 则从该元素代替。 position: relative; 将某些东西从其初始位置重新定位。

然后您使用topleftrightbottom 来选择如何重新定位项目。

#titlesection {
  height: auto;
  width: 100%;
  margin: auto;
  padding: 0px;
  margin-top: 25px;
  margin-bottom: 25px;
  position: relative;
  display: block;
}

#contentdiv {
  top: 0;
  left: 0;
  width: 97%;
  margin: auto;
  height: 100%;
  position: relative;
  display: block;
}

#title {
  padding-top: 50px;
  text-align: center;
  font-size: 60px;
  letter-spacing: 6px;
  display: block;
  margin-bottom: 1%;
  height: auto;
}

#titlesubheading {
  margin-top: 10px;
  margin-bottom: 10px;
  font-size: 30px;
  text-align: center;
  letter-spacing: 7px;
  display: block;
  height: auto;
}

#socialmedia {
  width: 97%;
  display: block;
}

#smtable {
  width: 10%;
  float: right;
}

#aboutheader {
  width: 95%;
  margin: auto;
  margin-top: 5%;
  padding: 10px;
  font-size: 30px;
  display: block;
  position: static;
}

#about {
  width: 95%;
  margin: auto;
  margin-top: 1%;
  padding: 0px;
  height: auto;
  font-size: 20px;
  display: block;
}

#abouttext {
  width: 60%;
  height: auto;
  display: block;
  margin-left: 10px;
  min-height: 140px;
}

【讨论】:

    【解决方案2】:

    您可以将 CSS position 属性与 z-index 属性将单个 div 覆盖在另一个 div 元素上。 z-index 属性决定了 定位元素

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-02
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多