【问题标题】:How can I reduce space between images when using <figure>?使用 <figure> 时如何减少图像之间的空间?
【发布时间】:2022-11-02 00:54:24
【问题描述】:

使用&lt;figure&gt;时如何调整图片间距?我想减少空间,但由于某种原因,我无法让它让步。

我想要 3 张图片放在一排,但是因为我无法减少图片之间的空间以让它们舒适地放入 div 框中,所以第三张是包装并位于前两张下方

.container3 {
  width: 1000px;
  height: 600px;
  box-sizing: border-box;
}

.container3 figure {
  display: inline-block;
  box-sizing: border-box;
}
<div class="container3">
<figure>
  <img class="image" height="200px" width="300px" src="/images/img-berryblitz.jpg" alt="berry blitz">
  <figcaption>
    Fall Berry Blitz Tea
  </figcaption>
</figure>
<figure>
  <img class="image" height="200px" width="300px" src="/images/img-spiced-rum.jpg" alt="spiced rum">
  <figcaption>
    Spiced Rum Tea
  </figcaption>
</figure>
<figure>
  <img class="image" height="200px" width="300px" src="/images/img-donut.jpg" alt="donut">
  <figcaption>
    Seasonal Donuts
  </figcaption>
</figure>
</div>

【问题讨论】:

  • display:flex; 添加到.container3 css
  • 你不是在说margin吗?像margin: 0; 这样的东西会完全删除它。根据您的浏览器margin: 8px; 可以减半。

标签: html css


【解决方案1】:

图应用了 16px 的边距。您可以调整边距,如果是这样,您想要什么:

.container3 {
  width: 1000px;
  height: 600px;
  box-sizing: border-box;
  border: 1px dashed black;
}

.container3 figure {
  display: inline-block;
  box-sizing: border-box;
  margin: 0;
}
<div class="container3">
  <figure>
    <img class="image" height="200px" width="300px" src="/images/img-berryblitz.jpg" alt="berry blitz">
    <figcaption>
      Fall Berry Blitz Tea
    </figcaption>
  </figure>
  <figure>
    <img class="image" height="200px" width="300px" src="/images/img-spiced-rum.jpg" alt="spiced rum">
    <figcaption>
      Spiced Rum Tea
    </figcaption>
  </figure>
  <figure>
    <img class="image" height="200px" width="300px" src="/images/img-donut.jpg" alt="donut">
    <figcaption>
      Seasonal Donuts
    </figcaption>
  </figure>
</div>

如果要在图像之间平均分配可用空间,可以使用 flexbox 并使用justify-content: space-between

.container3 {
  display: flex;
  justify-content: space-between;
  width: 1000px;
  height: 600px;
  box-sizing: border-box;
  border: 1px dashed black;
}

.container3 figure {
  display: inline-block;
  box-sizing: border-box;
  margin: 0;
}
<div class="container3">
  <figure>
    <img class="image" height="200px" width="300px" src="/images/img-berryblitz.jpg" alt="berry blitz">
    <figcaption>
      Fall Berry Blitz Tea
    </figcaption>
  </figure>
  <figure>
    <img class="image" height="200px" width="300px" src="/images/img-spiced-rum.jpg" alt="spiced rum">
    <figcaption>
      Spiced Rum Tea
    </figcaption>
  </figure>
  <figure>
    <img class="image" height="200px" width="300px" src="/images/img-donut.jpg" alt="donut">
    <figcaption>
      Seasonal Donuts
    </figcaption>
  </figure>
</div>

我根据 OP 的评论添加了另一个 sn-p:

.container {
  background: tomato;
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-flow: column nowrap;
  width: 600px;
  height: 300px;
  box-sizing: border-box;
  border: 1px dashed black;
}

.row {
  display: flex;
  justify-content: center;
  gap: 10px;
}

.row figure {
  background: #DEDEDE;
  /* or width */
  flex-basis: calc(100% * 1/3);
  /* you may use max-width: 100px; to limit the size */
  flex-shrink: 1;
  flex-grow: 0;
  box-sizing: border-box;
  height: 50px;
  margin: 0;
}
<div class="container">
  <div class="row row-3">
    <figure>Fig. 1</figure>
    <figure>Fig. 2</figure>
    <figure>Fig. 3</figure>
  </div>
  <div class="row row-2">
    <figure>Fig. 4</figure>
    <figure>Fig. 5</figure>
  </div>
  <div class="row row-1">
    <figure>Fig. 6</figure>
  </div>
</div>

【讨论】:

  • 没有 flex,你可以使用text-align-last仅供参考.但是 flex 是为此完成的,当然还有图上的边距重置。
  • 这工作得很好,谢谢。我想我一定是在添加 margin: 0;在我沮丧的情况下到父容器。使用 justify-content: space-around 时,如何使下面的两个图像与前 3 个图像共享相同的间距?我又添加了 2 个,但它们有更多空间,因为它们的空间更大,第二行没有第三张图片
  • 您必须设置一些边界:您必须为图形元素指定尺寸,或者您可以让 flexbox 负责间距和大小调整(使用 flex-basis、flex-shrink、flex-grow)。在我刚刚提供的示例中,您可以添加多行,并且可以根据需要单独处理每一行。
【解决方案2】:

inline-block 元素具有在标记后包含空格的“有趣”方式。您有 2 种可能性来删除空格:

  1. 正如@Breezer 建议的那样,将display: flex; 添加到父容器中。
  2. 在每个图之后删除 html 中的空格,如下所示:
        <figure>
            <img class="image" height="200px" width="300px" src="/images/img-berryblitz.jpg" alt="berry blitz">
            <figcaption>
                Fall Berry Blitz Tea
            </figcaption>
        </figure><figure>
            <img class="image" height="200px" width="300px" src="/images/img-spiced-rum.jpg" alt="spiced rum">
            <figcaption>
                Spiced Rum Tea
            </figcaption>
        </figure><figure>
            <img class="image" height="200px" width="300px" src="/images/img-donut.jpg" alt="donut">
            <figcaption>
                Seasonal Donuts
            </figcaption>
        </figure>
    

【讨论】:

  • 您还可以将容器的字体大小设置为 0。它将摆脱神奇的空白
  • @mok_ku 很好,学到了一些新东西 ^^ 但这意味着您必须为例如设置特定的字体大小无花果。
【解决方案3】:

在容器上使用display: flex 就足够了。

此外,&lt;img&gt; 标签需要无单位的widthheight 属性(没有“px”):

.container3 {
  width: 1000px;
  height: 600px;
  display: flex;
  justify-content: space-between;
}

.container3 figure {
   margin: 0;
}
  <div class="container3">
<figure>
  <img class="image" height="200" width="300" src="https://via.placeholder.com/300x150" alt="berry blitz">
  <figcaption>
    Fall Berry Blitz Tea
  </figcaption>
</figure>
<figure>
  <img class="image" height="200" width="300" src="https://via.placeholder.com/300x150" alt="spiced rum">
  <figcaption>
    Spiced Rum Tea
  </figcaption>
</figure>
<figure>
  <img class="image" height="200" width="300" src="https://via.placeholder.com/300x150" alt="donut">
  <figcaption>
    Seasonal Donuts
  </figcaption>
</figure>
</div>

【讨论】:

    猜你喜欢
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 2022-11-22
    • 1970-01-01
    • 2021-08-19
    相关资源
    最近更新 更多