29

I have 9 images in total and three on each row, I have managed to add a caption for one of my images, but failed to do so for the other ones as it just centers everything underneath and not aligned text to rows per image.

<figure>
<center>
<img src='images/album1.jpg' alt='missing' />
<figcaption>Album name goes here
<br>Year goes here
<br>artist name goes here</figcaption>

<img src='images/album2.jpg' alt='missing' />
<figcaption>Album name goes here
<br>Year goes here
<br>artist name goes here</figcaption>

<img src='images/album2.jpg' alt='missing' />
<figcaption>Album name goes here
<br>Year goes here
<br>artist name goes here</figcaption>
</figure><center>

And so on.

Brian Lam
  • 811
  • 3
  • 9
  • 10

5 Answers5

58

I would set up the code this way:

<figure>
    <img src='http://placehold.it/200x200' alt='missing' />
    <figcaption>Album name goes here
        <br>Year goes here
        <br>artist name goes here</figcaption>
</figure>

and apply the following CSS:

figure {
    display: inline-block;
    border: 1px dotted gray;
    margin: 20px; /* adjust as needed */
}
figure img {
    vertical-align: top;
}
figure figcaption {
    border: 1px dotted blue;
    text-align: center;
}

Because each figure is an inline-block, you can basically repeat the unit three times per row, either adding a <br> after the every third one, or wrapping three in a block element, or using a CSS3 nth-of-type(3n) selector to add a line break or similar.

Use text-align: center on figcaption to align the test to the center.

See demo at: http://jsfiddle.net/audetwebdesign/4njG8/

The results look like this (for a wide enough screen):

enter image description here

Marc Audet
  • 46,011
  • 11
  • 63
  • 83
  • 1
    This is really in depth Marc. I'm giving it a shot right now and thanks in regards! – Brian Lam Oct 02 '13 at 20:37
  • 1
    I would like to add that if your figure captions have unequal heights, you you can still align the images by adding `figure { vertical-align: top; ... }`. – Sam De Meyer Jan 11 '18 at 13:15
  • If you have long text (in the "Album name goes here..." ), then this stretches out the image container unfortunately so some hard-coding is required – information_interchange Mar 01 '18 at 03:53
  • Suppose I wanted the first line of images to have only 2 images, and the second line to have 3. For example, maybe the first two belong to one album, the next three belong to another album, and you want everything from an album to be grouped together. Using the code given, a
    will not place the third image on a new line but will display it to the side but not aligned with the other two.
    – Victor Engel Oct 09 '22 at 23:03
2

this works for me.

figure {
  display: inline-block;
  text-align: center;
  border: 1px dotted gray;
  margin: 5px; /* adjust as needed */
}
figure img {
  vertical-align: top;
}
figure figcaption {
  border: 1px dotted blue;
}

text-align: center; is the only thing needed.

TylerH
  • 20,799
  • 66
  • 75
  • 101
anatak
  • 440
  • 2
  • 7
  • 16
  • this is actually more general than the current accepted answer, since this will also work if the text is wider than the image. – amenthes Apr 12 '18 at 21:49
1

Each figure should only contain one image and one figcaption.

<figure>
    <img>
   <figcaption>
   </figcaption>
</figure>

BTW...the 'center' element no longer exists.

Codepen Example

Paulie_D
  • 107,962
  • 13
  • 142
  • 161
1

Image caption alignment in jupyter notebook markdown cell:
(includes: how to adjust text alignment margins and font style)

<table>
    <tr>
    <td style='text-align:center;'>
        <img src="https://cdn.pixabay.com/photo/2015/04/23/22/00/tree-736885__480.jpg"
             style='zoom:65%;'> <b> Tree and Sun </b><img>
    </td>
    <td> 
        <img src="https://cdn.pixabay.com/photo/2015/04/23/21/59/tree-736877__340.jpg" 
             style='zoom:91%;'/>
    <p style='text-align: right; margin-right: 3em; font-family: Serif;'><b> Moon and meow </b></p>
    </td>
    </tr>
</table>

enter image description here

rahul-ahuja
  • 1,166
  • 1
  • 12
  • 24
0

It is enough to provide alignment attribute in the figcaption: text-start, text-center and text-end

<figure>
   <img src='http://placehold.it/150x150' alt='missing' class="figure-img img-fluid rounded-circle"/>
   <figcaption class="figure-caption text-center">Name</figcaption>
</figure>

enter image description here

Ramis
  • 13,985
  • 7
  • 81
  • 100