如果flex子项的宽度总计为容器的100%,如何在它们之间添加空间

时间:2019-05-13 14:41:16

标签: html css flexbox

我有一个带有4个子容器的flex容器,根据屏幕的宽度,我希望每行显示1,2或3个帖子。我试图根据视图将帖子的宽度设置为100%,50%和33.3%,但我的问题是-如果帖子的总和为100%,那么如何在帖子之间增加间距?目前,他们正溢出到下一行。

我在下面包含了我的代码,但我希望它看起来类似于此处的帖子部分:https://carney.co/daily-carnage/

我尝试添加边距和填充,但这会导致帖子溢出到下一行。

  .container {
  max-width: 1140px;
  padding-left: 2rem;
  padding-right: 2rem;
  margin-right: auto;
  margin-left: auto;
  box-sizing: border-box;
  display: flex;
  flex-wrap: wrap;
}

article {
  box-shadow: 0 0 0.6rem 0.25rem rgba(0, 0, 0, .1);
  margin: 2rem 0;
  width: 100%;
}

.post-meta-and-title {
  padding: 2rem 1.5rem 0;
}

.post-meta {
  font-size: .7rem;
}

.post-title {
  font-size: 1.25rem;
}

.post-description {
  padding: 0 1.5rem;
}

footer {
  position: relative;
  padding: 0 1.5rem 2rem;
}

.btn {
  background-image: linear-gradient(150deg, #ffb064 -1%, #ff6496 101%);
  color: white;
  position: absolute;
  right: -1rem;
  bottom: -1rem;
  padding: .5rem 2rem;
  line-height: 1.5;
}

.author-name {
  display: inline;
}

span {
  padding: 0 .4rem;
}

@media (min-width: 810px) {
  .container {
    padding: 0;
  }
  article {
    width: 50%;
  }
  @media (min-width: 1200px) {
    article {
      width: 33.3%;
      margin-right: 3rem;
    }
  }
<section class="container">
  <article>
    <header class="post-meta-and-title">
      <div class="post-meta">
        <time datetime="2019-05-09 20:00">May 9, 2019</time>
        <p class="author-name"><span>|</span>
          <a href="https://carney.co/author/adamkunes/" rel="author">Adam Kunes</a>
        </p>
      </div>
      <h2><a class="post-title" href="#">Sky High WiFi</a></h2>
    </header>

    <p class="post-description">plus – better landing pages, heatmaps, and Starbucks.</p>

    <footer>
      <a href="#" class="btn">READ MORE</a>
    </footer>
  </article>

  <article>
    <header class="post-meta-and-title">
      <div class="post-meta">
        <time datetime="2019-05-09 20:00">May 9, 2019</time>
        <p class="author-name"><span>|</span>
          <a href="https://carney.co/author/adamkunes/" rel="author">Adam Kunes</a>
        </p>
      </div>
      <h2><a class="post-title" href="#">Are you afraid of clowns?</a></h2>
    </header>

    <p class="post-description">plus – tech overload, productivity, and Tom Brady.</p>

    <footer>
      <a href="#" class="btn">READ MORE</a>
    </footer>
  </article>

  <article>
    <header class="post-meta-and-title">
      <div class="post-meta">
        <time datetime="2019-05-09 20:00">May 9, 2019</time>
        <p class="author-name"><span>|</span>
          <a href="https://carney.co/author/adamkunes/" rel="author">Adam Kunes</a>
        </p>
      </div>
      <h2><a class="post-title" href="#">It's time to get real folks</a></h2>
    </header>

    <p class="post-description">plus – sell more event tickets, and faster feedback.</p>

    <footer>
      <a href="#" class="btn">READ MORE</a>
    </footer>
  </article>

  <article>
    <header class="post-meta-and-title">
      <div class="post-meta">
        <time datetime="2019-05-09 20:00">May 9, 2019</time>
        <p class="author-name"><span>|</span>
          <a href="https://carney.co/author/adamkunes/" rel="author">Adam Kunes</a>
        </p>
      </div>
      <h2><a class="post-title" href="#">Burger King goes plant-based</a></h2>
    </header>

    <p class="post-description">plus – how to create content for boring industries.</p>

    <footer>
      <a href="#" class="btn">READ MORE</a>
    </footer>
  </article>

</section>

我希望每行显示1,2或3个帖子,具体取决于屏幕尺寸。帖子的宽度应设置为100%,50%和33.3%,但它们之间也应有间隔,例如https://carney.co/daily-carnage/

2 个答案:

答案 0 :(得分:1)

您可以在.container上使用justify-content并计算所需宽度减去所需间距。

.container {
  max-width: 1140px;
  padding-left: 2rem;
  padding-right: 2rem;
  margin-right: auto;
  margin-left: auto;
  box-sizing: border-box;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between; /* ADDED */
}

article {
  box-shadow: 0 0 0.6rem 0.25rem rgba(0, 0, 0, .1);
  margin: 2rem 0;
  width: 100%;
}

.post-meta-and-title {
  padding: 2rem 1.5rem 0;
}

.post-meta {
  font-size: .7rem;
}

.post-title {
  font-size: 1.25rem;
}

.post-description {
  padding: 0 1.5rem;
}

footer {
  position: relative;
  padding: 0 1.5rem 2rem;
}

.btn {
  background-image: linear-gradient(150deg, #ffb064 -1%, #ff6496 101%);
  color: white;
  position: absolute;
  right: -1rem;
  bottom: -1rem;
  padding: .5rem 2rem;
  line-height: 1.5;
}

.author-name {
  display: inline;
}

span {
  padding: 0 .4rem;
}

@media (min-width: 810px) {
  .container {
    padding: 0;
  }
  article {
    width: calc(50% - 4rem);  /* USE CALC */
  }
  @media (min-width: 1200px) {
    article {
      width: calc(33.3% - 4rem);  /* USE CALC */
    }
  }
<section class="container">
  <article>
    <header class="post-meta-and-title">
      <div class="post-meta">
        <time datetime="2019-05-09 20:00">May 9, 2019</time>
        <p class="author-name"><span>|</span>
          <a href="https://carney.co/author/adamkunes/" rel="author">Adam Kunes</a>
        </p>
      </div>
      <h2><a class="post-title" href="#">Sky High WiFi</a></h2>
    </header>

    <p class="post-description">plus – better landing pages, heatmaps, and Starbucks.</p>

    <footer>
      <a href="#" class="btn">READ MORE</a>
    </footer>
  </article>

  <article>
    <header class="post-meta-and-title">
      <div class="post-meta">
        <time datetime="2019-05-09 20:00">May 9, 2019</time>
        <p class="author-name"><span>|</span>
          <a href="https://carney.co/author/adamkunes/" rel="author">Adam Kunes</a>
        </p>
      </div>
      <h2><a class="post-title" href="#">Are you afraid of clowns?</a></h2>
    </header>

    <p class="post-description">plus – tech overload, productivity, and Tom Brady.</p>

    <footer>
      <a href="#" class="btn">READ MORE</a>
    </footer>
  </article>

  <article>
    <header class="post-meta-and-title">
      <div class="post-meta">
        <time datetime="2019-05-09 20:00">May 9, 2019</time>
        <p class="author-name"><span>|</span>
          <a href="https://carney.co/author/adamkunes/" rel="author">Adam Kunes</a>
        </p>
      </div>
      <h2><a class="post-title" href="#">It's time to get real folks</a></h2>
    </header>

    <p class="post-description">plus – sell more event tickets, and faster feedback.</p>

    <footer>
      <a href="#" class="btn">READ MORE</a>
    </footer>
  </article>

  <article>
    <header class="post-meta-and-title">
      <div class="post-meta">
        <time datetime="2019-05-09 20:00">May 9, 2019</time>
        <p class="author-name"><span>|</span>
          <a href="https://carney.co/author/adamkunes/" rel="author">Adam Kunes</a>
        </p>
      </div>
      <h2><a class="post-title" href="#">Burger King goes plant-based</a></h2>
    </header>

    <p class="post-description">plus – how to create content for boring industries.</p>

    <footer>
      <a href="#" class="btn">READ MORE</a>
    </footer>
  </article>

</section>

答案 1 :(得分:0)

我过去所做的是将子元素分别包装在自己的容器中,然后将其宽度设置为33.3%或任何宽度,然后对其进行填充以在实际子元素之间增加空间。除了宽度之外,还添加了边距(因此实际上是33.33%+ 20px)。填充不会增加宽度。 So something like this maybe

另一种可能性是使用宽度计算。

width: calc(33.333% - 20px);

请不要在弹性基础上使用calc。在IE中变得很奇怪。