html - 元素之间存在差距,它正在吞噬我的大脑

标签 html css margin padding

下面是两个 HTML 元素之间的差距 - section 和 footer

/* contact form */

#contact {
  min-height: 500px;
  height: auto;
}

#contact .contact-form {
  background: #93cb52;
  width: 50%;
  height: 100%;
}

#contact .contact-form p {
  text-align: center;
  font-size: 16px;
  padding-bottom: 1.3rem;
}

#contact .contact-form h1 {
  text-align: center;
  padding-top: 1.3rem;
}

#contact .contact-form .input {
  display: flex;
  flex-direction: column;
  padding: 0px;
}

#contact .contact-form label {
  color: white;
  padding: 10px;
}

.i20 {
  padding: 10px;
  margin: 10px 20px;
}

.submit-btn {
  color: white;
  text-align: center;
  background: #333;
  margin: auto;
  padding: 1rem 2rem;
  border-radius: 40px;
  margin-bottom: 20px;
  margin-top: 20px;
}


/* footer */

#footer {
  height: 200px;
  background: #333;
  color: white;
  text-align: center;
}

#footer p {
  position: relative;
  top: 50%;
}
<!-- Contact and Map -->
<section id="contact">
  <div class="contact-form">
    <h1>Contact Us</h1>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Beatae, perspiciatis?</p>
    <div class="input">
      <label for="name"> Name</label>
      <input type="text" class="i20" placeholder="Enter Name">

      <label for="email"> Email</label>
      <input type="email" class="i20" placeholder="Enter Name">

      <label for="phone-number"> Phone number</label>
      <input type="text" class="i20" placeholder="Enter Name">

      <label for="Message"> Message</label>
      <input type="text" class="i20" placeholder="Enter Name">

      <a href="#" class="submit-btn">Submit</a>
    </div>
  </div>

  <div class="map"></div>

</section>


<!-- footer -->
<section id="footer">
  <p>Copyright &copy; Sahara Equity, All Rights Reserved</p>
</section>

我正在使用 HTML5 和 CSS3。也将不胜感激任何使这段代码不那么冗余和紧凑的建议。新来的学习者。

这是当前页面的样子。需要有 0 个空格。

website image

最佳答案

问题来自段落元素的默认边距。只需将它的 margin-top 设置为 0 然后间隙就会消失。

/* contact form */

#contact {
  min-height: 500px;
  height: auto;
}

#contact .contact-form {
  background: #93cb52;
  width: 50%;
  height: 100%;
}

#contact .contact-form p {
  text-align: center;
  font-size: 16px;
  padding-bottom: 1.3rem;
}

#contact .contact-form h1 {
  text-align: center;
  padding-top: 1.3rem;
}

#contact .contact-form .input {
  display: flex;
  flex-direction: column;
  padding: 0px;
}

#contact .contact-form label {
  color: white;
  padding: 10px;
}

.i20 {
  padding: 10px;
  margin: 10px 20px;
}

.submit-btn {
  color: white;
  text-align: center;
  background: #333;
  margin: auto;
  padding: 1rem 2rem;
  border-radius: 40px;
  margin-bottom: 20px;
  margin-top: 20px;
}


/* footer */

#footer {
  height: 200px;
  background: #333;
  color: white;
  text-align: center;
}

#footer p {
  position: relative;
  top: 50%;
  margin-top: 0;
}
<!-- Contact and Map -->
<section id="contact">
  <div class="contact-form">
    <h1>Contact Us</h1>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Beatae, perspiciatis?</p>
    <div class="input">
      <label for="name"> Name</label>
      <input type="text" class="i20" placeholder="Enter Name">

      <label for="email"> Email</label>
      <input type="email" class="i20" placeholder="Enter Name">

      <label for="phone-number"> Phone number</label>
      <input type="text" class="i20" placeholder="Enter Name">

      <label for="Message"> Message</label>
      <input type="text" class="i20" placeholder="Enter Name">

      <a href="#" class="submit-btn">Submit</a>
    </div>
  </div>

  <div class="map"></div>

</section>


<!-- footer -->
<section id="footer">
  <p>Copyright &copy; Sahara Equity, All Rights Reserved</p>
</section>

该行为称为 Collapsing margins .

关于html - 元素之间存在差距,它正在吞噬我的大脑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59473363/

相关文章:

javascript - 图片就在另一张图片之上

html - 响应式背景图像 div 和固定导航的重叠内容问题

css - 简单的CSS高度问题

jquery - 嵌套表格响应对齐

html - 可以使用 CSS 属性作为 HTML 属性吗?

css - Extjs fieldset - 折叠时删除标题后的行

javascript - jQuery keyup 事件只触发一次

html - 为什么我的 href 路径在 AWS 上测试网页与在本地测试时需要额外的 '/'?

php - 将 HTML-PHP 表导出/下载到 Excel 文件

css - 为什么我的右边距与 IE 中的左边距重叠?