DEV Community

Stefan Judis
Stefan Judis

Posted on • Originally published at on

TIL: caption-side controls the position of a table caption

Today I came across a tweet by CSS-Tricks. It shares the behavior of the CSS property caption-side. I've never seen caption-side before.

The property can be used when you're dealing with HTML tables. Tables could potentially include a caption element. The clue with caption elements is that they have to be the first element in the table.

This rule makes them usually appear in the top region of the table and gives no options for a different position.

  <caption>Populations of cities</caption>
    <td>New York City</td>
Enter fullscreen mode Exit fullscreen mode

It turns out that you can use the caption-side CSS property to move the position of the caption element somewhere else. Values that you can safely use today are unfortunately only top and bottom. right, left and a few others are available but not supported cross-browser.

The following lines of CSS can change the position of the caption element to the bottom even though it's the first element inside of the table! 🎉

table {
  /* moves the caption to the bottom */
  caption-side: bottom;
Enter fullscreen mode Exit fullscreen mode

Visualisation of the caption CSS property including two tables: one with the caption in the top region (caption-side: top) and one with the caption in the bottom region (caption-side: bottom)

If you want to read more about the caption-side property, head over to MDN or CSS-Tricks.

Additionally, if you want to see it in action, you can have a look at this CodePen.

Top comments (0)