【问题标题】:Removing white space between two sections删除两个部分之间的空白
【发布时间】:2016-08-31 16:26:01
【问题描述】:

我有两个这样的部分:

<section id="achievements"> ... </section>
<section id="services"> ... </section>

它们之间有一个空白区域。虽然我的其余部分并非如此。

我没有为这些部分设置任何显示属性,但它是从 css 文件继承块的显示属性。 (从 Chrome 中的开发者工具中检查)

如果出于某种原因,我还检查了这两个部分的内容。好像不是这样的。

是什么导致了这个空白,我该如何删除它?

PS:如果我应该添加更多信息,请告诉我。

PS2:我在这个网站上查看的帖子谈到了从 inline-block 元素中删除空格。

更新

这里是相关部分的Fiddle

/* main */

body {
  font-family: "Montserrat", sans-serif;
  margin: 0px;
  padding: 0px;
}
.upper {
  text-transform: uppercase;
}
/* Positioning */

.vertically_centered {
  position: relative;
  top: 50%;
  -webkit-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
}
.left {
  float: left;
}
.right {
  float: right;
}
/* For tagline sections in 5 articles */

.tagline {
  height: 305px;
  padding: 0;
  margin: 0;
}
.tagline_text {
  margin: auto;
  text-align: center;
}
.tagline_sub {
  font-size: 18px;
  color: #999999;
}
.bottom_line:after {
  content: "";
  display: block;
  width: 60px;
  border-bottom: 3px solid #b3b3b3;
  margin: 30px auto 40px;
}
/* row 4: achievements */

#achievements,
#achievements article {
  height: 320px;
  margin: 0px;
  padding: 0px;
}
#experiments {
  background-color: #1a1a1a;
  width: 33.33%;
  height: 320px;
}
#awards {
  background-color: #212121;
  width: 33.33%;
  height: 320px;
}
#messages {
  background-color: #262626;
  width: 33.33%;
  height: 320px;
}
.results {
  margin: auto;
  height: 35%;
  width: 75%;
  text-align: center;
}
.data_set {
  width: 70%;
}
.number {
  font-size: 42px;
  color: white;
}
.details {
  font-size: 16px;
  color: #cccccc;
}
.first_icon_set {
  max-width: 30%;
  height: auto;
}
/* row 5: services */

#services {
  background-color: #e6e6e6;
  height: 1005px;
  margin: 0px;
  padding: 0px;
}
#services_details {
  height: 700px;
  width: 100%;
}
.services_box {
  background-color: white;
  width: 32%;
  height: 330px;
  margin: 20px 0.66% 0% 0.66%;
  text-align: center;
}
.services_box p {
  padding: 0 15px 0 20px;
}
.icon_set_services {
  width: 60%;
}
#services_details_sub {
  margin-top: 25px;
  font-size: 14px;
  color: #999999;
  line-height: 25px;
}
<div class="">
  <!-- row 4: achievements -->
  <section class="" id="achievements">
    <article id="experiments" class="left">
      <section class="results vertically_centered">
        <img src="icons/flask.svg" alt="enigma_flask_icon" class="first_icon_set left"></img>
        <div class="data_set upper left">
          <p class="number">1200</p>
          <p class="details">design experiments</p>
        </div>
      </section>
    </article>
    <article id="awards" class="left">
      <section class="results vertically_centered">
        <img src="icons/trophy.svg" alt="enigma_trophy_icon" class="first_icon_set left"></img>
        <div class="data_set upper left">
          <p class="number">578</p>
          <p class="details">awards won</p>
        </div>
      </section>
    </article>
    <article id="messages" class="left">
      <section class="results vertically_centered">
        <img src="icons/envelope.svg" alt="enigma_trophy_icon" class="first_icon_set left"></img>
        <div class="data_set upper left">
          <p class="number">12489</p>
          <p class="details">messages sent</p>
        </div>
      </section>
    </article>
  </section>

  <!-- Remove white space from top-->
  <!-- row 5: services -->
  <section id="services">
    <article class="tagline">
      <section class="tagline_text vertically_centered">
        <h2 class="upper bottom_line">Our services</h2>
        <p class="tagline_sub">Continuously Creating Daring Digital Solutions</p>
      </section>
    </article>
    <article id="services_details">
      <section class="services_box left">
        <div class="vertically_centered">
          <img src="icons/star.svg" alt="enigma_star_icon" class="icon_set_services"></img>
          <h4 class="upper">Great design</h4>
          <p id="services_details_sub">Our web design team will spend time with our digital marketing team to ensure the core principles of effective websites.</p>
        </div>
      </section>
      <section class="services_box left">
        <div class="vertically_centered">
          <img src="icons/files.svg" alt="enigma_files_icon" class="icon_set_services"></img>
          <h4 class="upper">Perfect coding</h4>
          <p id="services_details_sub">Our web design team will spend time with our digital marketing team to ensure the core principles of effective websites.</p>
        </div>
      </section>
      <section class="services_box left">
        <div class="vertically_centered">
          <img src="icons/graph.svg" alt="enigma_graph_icon" class="icon_set_services"></img>
          <h4 class="upper">Marketing</h4>
          <p id="services_details_sub">Our web design team will spend time with our digital marketing team to ensure the core principles of effective websites.</p>
        </div>
      </section>
      <section class="services_box left">
        <div class="vertically_centered">
          <img src="icons/face.svg" alt="enigma_face_icon" class="icon_set_services"></img>
          <h4 class="upper">Support</h4>
          <p id="services_details_sub">Our web design team will spend time with our digital marketing team to ensure the core principles of effective websites.</p>
        </div>
      </section>
      <section class="services_box left">
        <div class="vertically_centered">
          <img src="icons/gear.svg" alt="enigma_gear_icon" class="icon_set_services"></img>
          <h4 class="upper">Development</h4>
          <p id="services_details_sub">Our web design team will spend time with our digital marketing team to ensure the core principles of effective websites.</p>
        </div>
      </section>
      <section class="services_box left">
        <div class="vertically_centered">
          <img src="icons/screen.svg" alt="enigma_screen_icon" class="icon_set_services"></img>
          <h4 class="upper">Web design</h4>
          <p id="services_details_sub">Our web design team will spend time with our digital marketing team to ensure the core principles of effective websites.</p>
        </div>
      </section>
    </article>
  </section>
</div>

【问题讨论】:

  • Сan 你做小提琴吗?没有你的风格就很难解决问题
  • 好的,给我一些时间,我会做的。谢谢! :)
  • 当你不显示大部分 css 时很难知道问题是什么......但我假设这是一个边距或填充问题。
  • 请记住,您需要在此处发布显示问题的代码。只要这里也有一个最小的例子,小提琴就可以了。
  • stackoverflow.com/questions/18508442/… 这可能会有所帮助。没有代码就无法识别您的问题

标签: css html whitespace sections


【解决方案1】:

这就是第二个&lt;section&gt; 中的margin 中的&lt;h2&gt;,这使您的部分之间产生了差距。

您可以通过向&lt;h2&gt; 添加一个类并像这样设置margin-top: 0; 来纠正它:

html:

<h2 class="fix-gap upper bottom_line">Our services</h2>

css:

.fix-gap {
  margin-top: 0;
}

【讨论】:

    【解决方案2】:

    这就是所谓的collapsed margins

    您需要从子h2 重置默认margin

    注意:

    小心img是一个自闭标签

    /* main */
    
    body {
      font-family: "Montserrat", sans-serif;
      margin: 0px;
      padding: 0px;
    }
    .upper {
      text-transform: uppercase;
      margin:0
    }
    /* Positioning */
    
    .vertically_centered {
      position: relative;
      top: 50%;
      -webkit-transform: translateY(-50%);
      -ms-transform: translateY(-50%);
      transform: translateY(-50%);
    }
    .left {
      float: left;
    }
    .right {
      float: right;
    }
    /* For tagline sections in 5 articles */
    
    .tagline {
      height: 305px;
      padding: 0;
      margin: 0;
    }
    .tagline_text {
      margin: auto;
      text-align: center;
    }
    .tagline_sub {
      font-size: 18px;
      color: #999999;
    }
    .bottom_line:after {
      content: "";
      display: block;
      width: 60px;
      border-bottom: 3px solid #b3b3b3;
      margin: 30px auto 40px;
    }
    /* row 4: achievements */
    
    #achievements,
    #achievements article {
      height: 320px;
      margin: 0px;
      padding: 0px;
    }
    #experiments {
      background-color: #1a1a1a;
      width: 33.33%;
      height: 320px;
    }
    #awards {
      background-color: #212121;
      width: 33.33%;
      height: 320px;
    }
    #messages {
      background-color: #262626;
      width: 33.33%;
      height: 320px;
    }
    .results {
      margin: auto;
      height: 35%;
      width: 75%;
      text-align: center;
    }
    .data_set {
      width: 70%;
    }
    .number {
      font-size: 42px;
      color: white;
    }
    .details {
      font-size: 16px;
      color: #cccccc;
    }
    .first_icon_set {
      max-width: 30%;
      height: auto;
    }
    /* row 5: services */
    
    #services {
      background-color: #e6e6e6;
      height: 1005px;
      margin: 0px;
      padding: 0px;
    }
    #services_details {
      height: 700px;
      width: 100%;
    }
    .services_box {
      background-color: white;
      width: 32%;
      height: 330px;
      margin: 20px 0.66% 0% 0.66%;
      text-align: center;
    }
    .services_box p {
      padding: 0 15px 0 20px;
    }
    .icon_set_services {
      width: 60%;
    }
    #services_details_sub {
      margin-top: 25px;
      font-size: 14px;
      color: #999999;
      line-height: 25px;
    }
    <div class="">
      <!-- row 4: achievements -->
      <section class="" id="achievements">
        <article id="experiments" class="left">
          <section class="results vertically_centered">
            <img src="icons/flask.svg" alt="enigma_flask_icon" class="first_icon_set left"/>
            <div class="data_set upper left">
              <p class="number">1200</p>
              <p class="details">design experiments</p>
            </div>
          </section>
        </article>
        <article id="awards" class="left">
          <section class="results vertically_centered">
            <img src="icons/trophy.svg" alt="enigma_trophy_icon" class="first_icon_set left"/>
            <div class="data_set upper left">
              <p class="number">578</p>
              <p class="details">awards won</p>
            </div>
          </section>
        </article>
        <article id="messages" class="left">
          <section class="results vertically_centered">
            <img src="icons/envelope.svg" alt="enigma_trophy_icon" class="first_icon_set left"/>
            <div class="data_set upper left">
              <p class="number">12489</p>
              <p class="details">messages sent</p>
            </div>
          </section>
        </article>
      </section>
    
      <!-- Remove white space from top-->
      <!-- row 5: services -->
      <section id="services">
        <article class="tagline">
          <section class="tagline_text vertically_centered">
            <h2 class="upper bottom_line">Our services</h2>
            <p class="tagline_sub">Continuously Creating Daring Digital Solutions</p>
          </section>
        </article>
        <article id="services_details">
          <section class="services_box left">
            <div class="vertically_centered">
              <img src="icons/star.svg" alt="enigma_star_icon" class="icon_set_services"/>
              <h4 class="upper">Great design</h4>
              <p id="services_details_sub">Our web design team will spend time with our digital marketing team to ensure the core principles of effective websites.</p>
            </div>
          </section>
          <section class="services_box left">
            <div class="vertically_centered">
              <img src="icons/files.svg" alt="enigma_files_icon" class="icon_set_services"/>
              <h4 class="upper">Perfect coding</h4>
              <p id="services_details_sub">Our web design team will spend time with our digital marketing team to ensure the core principles of effective websites.</p>
            </div>
          </section>
          <section class="services_box left">
            <div class="vertically_centered">
              <img src="icons/graph.svg" alt="enigma_graph_icon" class="icon_set_services"/>
              <h4 class="upper">Marketing</h4>
              <p id="services_details_sub">Our web design team will spend time with our digital marketing team to ensure the core principles of effective websites.</p>
            </div>
          </section>
          <section class="services_box left">
            <div class="vertically_centered">
              <img src="icons/face.svg" alt="enigma_face_icon" class="icon_set_services"/>
              <h4 class="upper">Support</h4>
              <p id="services_details_sub">Our web design team will spend time with our digital marketing team to ensure the core principles of effective websites.</p>
            </div>
          </section>
          <section class="services_box left">
            <div class="vertically_centered">
              <img src="icons/gear.svg" alt="enigma_gear_icon" class="icon_set_services"/>
              <h4 class="upper">Development</h4>
              <p id="services_details_sub">Our web design team will spend time with our digital marketing team to ensure the core principles of effective websites.</p>
            </div>
          </section>
          <section class="services_box left">
            <div class="vertically_centered">
              <img src="icons/screen.svg" alt="enigma_screen_icon" class="icon_set_services"/>
              <h4 class="upper">Web design</h4>
              <p id="services_details_sub">Our web design team will spend time with our digital marketing team to ensure the core principles of effective websites.</p>
            </div>
          </section>
        </article>
      </section>
    </div>

    【讨论】:

    • 非常感谢您的详细解答!我不知道边距也可以以这种方式继承。看来我还有很多东西要学。
    【解决方案3】:

    您需要将 ma​​rgin:0; 添加到 h2

    .upper {margin:0;}
    

    【讨论】:

      【解决方案4】:

      使用这个:我用小提琴对其进行了测试,它有效。

      #services {
        background-color: #e6e6e6;
        height: 1005px;
        margin: 0px;
        padding: 0px;
        float: left;
        clear: none;
      }   
      

      或者您可以将其与 div 一起使用:

      <div id="first_div">
              ...
          </div><div id="second_div">
              ...
          </div>
      

      通过将 div 内联,它消除了元素之间的空间,因为如果它们之间有空间,它就会成为布局中的空间。

      【讨论】:

        【解决方案5】:

        您可以尝试使用margin-(right/left/bottom/top):0px

        【讨论】:

          【解决方案6】:

          如果您遇到这些问题,找到解决方案的最佳方法是通过 Inspect Element。在这里,当您这样做时,您将能够看到有一个默认边距。因此,为了摆脱这种情况,请转到您的 CSS 文件并设置边距:0;这应该会有所帮助。

          【讨论】: