<caption>

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, and it must be the first child of its parent <table> element. It is possible to define only one caption per table.

When the <table> element containing <caption> is the only descendant of the <figure> element, you must use the <figcaption> element instead of <caption>.

By default, a table caption is center-aligned above a table. But it is possible to use the text-align and caption-side properties to align and place the caption.

Syntax

Syntax

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

Example of the HTML <caption> tag:

<!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

Student1st exam2nd exam
John Johnson7565
Mary Nicolson5580
Max Thomson6047

Example of the HTML <caption> tag with the CSS

caption-side property:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      caption {
        background: #1c87c9;
        color: #fff;
        caption-side: bottom;
      }
      table,
      th,
      td {
        border: 1px solid #1c87c9;
        padding: 3px;
      }
      td {
        background-color: #cccccc;
        color: #666666;
      }
    </style>
  </head>
  <body>
    <h2>Caption-side property example</h2>
    <p>Here the caption-side is set to "bottom":</p>
    <table>
      <caption>Some title here</caption>
      <tr>
        <td>Some text</td>
        <td>Some text</td>
      </tr>
    </table>
  </body>
</html>

Attributes

Attributes

AttributeValueDescription
alignAligns the header horizontally.
Not used in HTML5.
rightthe header is placed on top and aligned to the right.
leftthe header is placed on top and aligned to the left.
topthe header is placed on top and aligned to the center.
bottomthe 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 signalled 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:

CSS text-shadow property adds shadow to text. CSS text-align-last property sets the alignment of the last line of the text. CSS line-height property specifies the height of a line. CSS letter-spacing property defines the spaces between letters/characters in a text. CSS word-spacing property sets the spacing between words.



请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部