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
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
Student | 1st exam | 2nd exam |
---|---|---|
John Johnson | 75 | 65 |
Mary Nicolson | 55 | 80 |
Max Thomson | 60 | 47 |
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
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 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.
Browser support
|
|
|
|
|
---|---|---|---|---|
✓ | ✓ | ✓ | ✓ | ✓ |
Practice Your Knowledge
Quiz Time: Test Your Skills!
Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.