HTML <caption> Tag

The <caption> tag is used to define the header of the table. The tag itself must be inside the <table> element immediately after the opening (<table>) tag.

Syntax

The <caption> tag comes in pairs, which means that the content is written between the opening (<caption>) and the closing (</caption>) tags.

Example

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <table  width="400" border="1">
      <caption>Evaluation paper</caption>
      <thead>
        <tr>
          <th>Student</th>
          <th>1st exam</th>
          <th>2nd exam</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>John Johnson</td>
          <td>75</td>
          <td>65</td>
        </tr>
        <tr>
          <td>Mary Nicolson</td>
          <td>55</td>
          <td>80</td>
        </tr>
        <tr>
          <td>Max Thomson</td>
          <td>60</td>
          <td>47</td>
        </tr>
      </tbody>
    </table>
  </body>
</html>

Result

Evaluation paper
Student 1st exam 2nd exam
John Johnson 75 65
Mary Nicolson 55 80
Max Thomson 60 47

Attributes

Attribute Value Description
align Aligns the header horizontally.
Not used in HTML5.
right the header is placed on top and aligned to the right.
left the header is placed on top and aligned to the left.
top the header is placed on top and aligned to the center.
bottom the header is placed below and aligned to the center.

The <caption> tag supports the Global Attributes and the Event Attributes.

How to style <caption> tag

Common properties to alter the visual weight/emphasis/size of text in <caption> tag:?

  • CSS font-style property sets the style of the font. normal | italic | oblique | initial | inherit
  • CSS font-family property specifies a prioritized list of one or more font family names and/or generic family names for the selected element.
  • CSS font-size property sets the size of the font.
  • CSS font-weight property defines whether the font should be bold or thick. CSS text-transform Property controls text case and capitalization.
  • CSS text-decoration property specifies the decoration added to text, and is a shorthand property for text-decoration-line, text-decoration-color, text-decoration-style

Coloring text in <caption> tag:

  • CSS color property describes the color of the text content and text decorations
  • CSS background-color property sets the background color of an element.

Text layout styles for <caption> tag:

  • CSS text-indent property specifies the indentation of the first line in a text block.
  • CSS text-overflow property specifies how overflowed content that is not displayed should be signaled to the user.
  • CSS white-space property specifies how white-space inside an element is handled.
  • CSS word-break property specifies where the lines should be broken.

Other properties worth looking at for <caption> tag


Browser support



Do you find this helpful?

Related articles