如何删除此 HTML 项目中的白色 space 间隙

How to remove white space gap in this HTML Project

好的,我正在使用我在 W3 Schools 上看到的示例。这是确切的问题,我也能够在他们的网站上复制我的问题。 https://www.w3schools.com/howto/howto_css_portfolio_gallery.asp

我的问题是我有这个很大的白色 space 间隙,具体取决于我的网格块的大小。当网格块的大小都相同时,就没有大的差距。

如果右边的格子比较大就没有空隙

只有左边的格子块比右边的格子块大时才有间隙。

所以我的问题是我该怎么做才能消除这个差距?

* {
  box-sizing: border-box;
}

body {
  background-color: #f1f1f1;
  padding: 20px;
  font-family: Arial;
}


/* Center website */

.main {
  max-width: 1000px;
  margin: auto;
}

h1 {
  font-size: 50px;
  word-break: break-all;
}

.row {
  margin: 8px -16px;
}


/* Add padding BETWEEN each column */

.row,
.row>.column {
  padding: 8px;
}


/* Create four equal columns that floats next to each other */

.column {
  float: left;
  width: 50%;
}


/* Clear floats after rows */

.row:after {
  content: "";
  display: table;
  clear: both;
}


/* Content */

.content {
  background-color: white;
  padding: 10px;
}


/* Responsive layout - makes a two column-layout instead of four columns */

@media screen and (max-width: 900px) {
  .column {
    width: 50%;
  }
}


/* Responsive layout - makes the two columns stack on top of each other instead of next to each other */

@media screen and (max-width: 600px) {
  .column {
    width: 100%;
  }
}
<!-- MAIN (Center website) -->
<div class="main">

  <h1>MYLOGO.COM</h1>
  <hr>

  <h2>PORTFOLIO</h2>

  <!-- Portfolio Gallery Grid -->
  <div class="row">
    <div class="column">
      <div class="content">
        <img src="/w3images/mountains.jpg" alt="Mountains" style="width:100%">
        <h3>My Work</h3>
        <p>Lorem ipsum dolor sit amet, tempor prodesset eos no. Temporibus necessitatibus sea ei, at tantas oporteat nam. Lorem ipsum dolor sit amet, tempor prodesset eos no</p>
      </div>
    </div>
    <div class="column">
      <div class="content">
        <img src="/w3images/lights.jpg" alt="Lights" style="width:100%">
        <h3>My Work</h3>
        <p>Less space used here</p>
      </div>
    </div>
    <div class="column">
      <div class="content">
        <img src="/w3images/nature.jpg" alt="Nature" style="width:100%">
        <h3>My Work</h3>
        <p>Lorem ipsum dolor sit amet, tempor prodesset eos no. Temporibus necessitatibus sea ei, at tantas oporteat nam. Lorem ipsum dolor sit amet, tempor prodesset eos no.</p>
      </div>
    </div>
    <div class="column">
      <div class="content">
        <img src="/w3images/mountains.jpg" alt="Mountains" style="width:100%">
        <h3>My Work</h3>
        <p>Lorem ipsum dolor sit amet, tempor prodesset eos no. Temporibus necessitatibus sea ei, at tantas oporteat nam. Lorem ipsum dolor sit amet, tempor prodesset eos no.</p>
      </div>
    </div>
    <!-- END GRID -->
  </div>

  <div class="content">
    <img src="/w3images/p3.jpg" alt="Bear" style="width:100%">
    <h3>Some Other Work</h3>
    <p>Lorem ipsum dolor sit amet, tempor prodesset eos no. Temporibus necessitatibus sea ei, at tantas oporteat nam. Lorem ipsum dolor sit amet, tempor prodesset eos no.</p>
    <p>Lorem ipsum dolor sit amet, tempor prodesset eos no. Temporibus necessitatibus sea ei, at tantas oporteat nam. Lorem ipsum dolor sit amet, tempor prodesset eos no.</p>
  </div>

  <!-- END MAIN -->
</div>

据我了解,您的设计目前使用 css float: left 选项破坏了文档流。

如果您仔细观察,根据您的代码,您的投资组合项目的顺序实际上是正确的,顺序是 1. 山脉,2. 灯光,3. 自然,4. 山脉。 DOM 已被渲染以用“自然”填充“灯光”之后的 space。这是因为文档是从左到右,从上到下呈现的,考虑到这一点,“自然”占据了整个 space,导致呈现“山脉”时出现间隙。

我建议使用 CSS 网格而不是使用浮点数。

检查以下代码:

#grid { 
    display:grid;
    grid-template-column: 1fr 1fr;
    column-gap: 30px;
}

.elem {
    margin-bottom: 25px;
}
<div id="grid">
    <div class="elem"></div>
    <div class="elem"></div>
    <div class="elem"></div>
    <div class="elem"></div>
    <div class="elem"></div>
</div>