Home > Software engineering >  How can I reduce space between images when using <figure>?
How can I reduce space between images when using <figure>?

Time:10-31

How can I adjust the space between images when using <figure>? I want to reduce the space, but for some reason I just can't get it it to budge.

I want 3 images to sit on one row, but because I can't reduce the space between images to allow them to fit comfortably in the div box, the 3rd is wrapping and sitting below the first two

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

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

CodePudding user response:

Figure has a margin of 16px applied. You can adjust the margin, if it is that, what you want:

.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 >
  <figure>
    <img  height="200px" width="300px" src="/images/img-berryblitz.jpg" alt="berry blitz">
    <figcaption>
      Fall Berry Blitz Tea
    </figcaption>
  </figure>
  <figure>
    <img  height="200px" width="300px" src="/images/img-spiced-rum.jpg" alt="spiced rum">
    <figcaption>
      Spiced Rum Tea
    </figcaption>
  </figure>
  <figure>
    <img  height="200px" width="300px" src="/images/img-donut.jpg" alt="donut">
    <figcaption>
      Seasonal Donuts
    </figcaption>
  </figure>
</div>

If you want to distribute the available space evenly between the images, you can use flexbox and use 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 >
  <figure>
    <img  height="200px" width="300px" src="/images/img-berryblitz.jpg" alt="berry blitz">
    <figcaption>
      Fall Berry Blitz Tea
    </figcaption>
  </figure>
  <figure>
    <img  height="200px" width="300px" src="/images/img-spiced-rum.jpg" alt="spiced rum">
    <figcaption>
      Spiced Rum Tea
    </figcaption>
  </figure>
  <figure>
    <img  height="200px" width="300px" src="/images/img-donut.jpg" alt="donut">
    <figcaption>
      Seasonal Donuts
    </figcaption>
  </figure>
</div>

CodePudding user response:

inline-block elements have the "funny" way of including the whitespace after the tag. You have 2 possibilities to remove the whitespace:

  1. As @Breezer suggested add display: flex; to the parent-container.
  2. remove the whitespace in html after each figure like so:
    <figure>
        <img  height="200px" width="300px" src="/images/img-berryblitz.jpg" alt="berry blitz">
        <figcaption>
            Fall Berry Blitz Tea
        </figcaption>
    </figure><figure>
        <img  height="200px" width="300px" src="/images/img-spiced-rum.jpg" alt="spiced rum">
        <figcaption>
            Spiced Rum Tea
        </figcaption>
    </figure><figure>
        <img  height="200px" width="300px" src="/images/img-donut.jpg" alt="donut">
        <figcaption>
            Seasonal Donuts
        </figcaption>
    </figure>

CodePudding user response:

Using display: flex on the container should be enough.

Also, the <img> tag expects unitless width and height attributes (without "px"):

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

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

  • Related