为什么我的'标题'和'主'(HTML)之间有一个边距/空格? [重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

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

Obs:请注意,我试图消除一些利润,但问题没有解决。

@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>
html css margin
1个回答
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>
© www.soinside.com 2019 - 2024. All rights reserved.