为什么在我的“标题”和“主要”(HTML)之间存在边距/空格?

时间:2019-04-20 01:12:12

标签: html css margin

我正在一个响应式网页上工作,现在我正在进行其媒体查询之一。我需要将标题和主体的第一部分放在一起,但是我遇到了麻烦。我已经尝试删除标题,主体和主体的第一部分的边距,但是即使如此,标题和该部分之间也有空格。为什么会这样呢?我的代码摘录如下:

糟糕:请注意,我试图消除一些空白,但问题仍未解决。

@charset "UTF-8";
@import url('https://fonts.googleapis.com/css?family=Oxygen');
body {
  font-family: Arial, Helvetica, sans-serif;
}

h1,
h2,
h3,
h4,
h5,
h6,
#firstsection p {
  font-family: 'Oxygen', sans-serif;
}

a {
  text-decoration: none;
}

header {
  margin-bottom: 50px;
}

header img,
main img {
  display: block;
  margin-right: auto;
  margin-left: auto;
}

nav ul {
  list-style: none;
  text-align: center;
  background-color: rgb(90, 32, 102);
  line-height: 3.125em;
  padding: 1em 0;
  width: 105.7%;
}

header a {
  color: white;
}

nav ul li:hover {
  background-color: rgb(98, 46, 109);
}

main {
  width: 105.75%;
}

#firstsection {
  text-align: center;
  margin-bottom: 45px;
}

#firstsection h2 {
  color: rgb(85, 26, 119);
  font-size: 1.35em;
  margin-bottom: 1.38888889em;
  letter-spacing: 0.04629629629em;
}

#firstsection p {
  font-weight: bold;
  color: rgb(167, 120, 199);
  margin: 1.03025em;
  line-height: 1.5em;
}


/* END OF FIRST LAYOUT */


/* START OF FIRST MEDIA QUERIE */

@media screen and (min-width: 48.75em) and (max-width: 74em) {
  header {
    margin-bottom: 0;
  }
  header img {
    margin-left: 10px;
  }
  main {
    margin-top: 0;
  }
  #firstsection {
    background-color: rgb(85, 26, 119);
    margin-top: 0;
  }
  #firstsection h2 {
    color: white;
  }
}
<header>
  <img src="images/logo_mob.svg" alt="Logo Range Hotels">
  <nav>
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">Products</a></li>
      <li><a href="#">Services</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </nav>
</header>
<main>
  <section id="firstsection">
    <h2>An exciting new venture over the<br>range</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua consectetur adipisicing elit.</p>
    <a href="#">Get Started</a>
  </section>

1 个答案:

答案 0 :(得分:1)

要完全消除垂直间隙,请添加以下规则:

header,
nav ul {
  margin-bottom: 0;
}

#firstsection > h2 {
  margin-top: 0;
}

演示

@charset "UTF-8";
@import url('https://fonts.googleapis.com/css?family=Oxygen');
body {
  font-family: Arial, Helvetica, sans-serif;
}

h1,
h2,
h3,
h4,
h5,
h6,
#firstsection p {
  font-family: 'Oxygen', sans-serif;
}

a {
  text-decoration: none;
}

header {
  margin-bottom: 50px;
}

header img,
main img {
  display: block;
  margin-right: auto;
  margin-left: auto;
}

nav ul {
  list-style: none;
  text-align: center;
  background-color: rgb(90, 32, 102);
  line-height: 3.125em;
  padding: 1em 0;
  width: 105.7%;
}

header a {
  color: white;
}

nav ul li:hover {
  background-color: rgb(98, 46, 109);
}

main {
  width: 105.75%;
}

#firstsection {
  text-align: center;
  margin-bottom: 45px;
}

#firstsection h2 {
  color: rgb(85, 26, 119);
  font-size: 1.35em;
  margin-bottom: 1.38888889em;
  letter-spacing: 0.04629629629em;
}

#firstsection p {
  font-weight: bold;
  color: rgb(167, 120, 199);
  margin: 1.03025em;
  line-height: 1.5em;
}


/* END OF FIRST LAYOUT */


/* START OF FIRST MEDIA QUERIE */

@media screen and (min-width: 48.75em) and (max-width: 74em) {
  header {
    margin-bottom: 0;
  }
  header img {
    margin-left: 10px;
  }
  main {
    margin-top: 0;
  }
  #firstsection {
    background-color: rgb(85, 26, 119);
    margin-top: 0;
  }
  #firstsection h2 {
    color: white;
  }
}

header,
nav ul {
  margin-bottom: 0;
}

#firstsection > h2 {
  margin-top: 0;
}
<header>
  <img src="images/logo_mob.svg" alt="Logo Range Hotels">
  <nav>
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">Products</a></li>
      <li><a href="#">Services</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </nav>
</header>
<main>
  <section id="firstsection">
    <h2>An exciting new venture over the<br>range</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua consectetur adipisicing elit.</p>
    <a href="#">Get Started</a>
  </section>
</main>