The caption tag is used to specify the caption of a table. This tag will be inserted just after the <table> tag. Only one caption can be specified for one table. It is by default aligned to the center.
Syntax:
<caption align = "value" ></caption>
Attributes: This tag accepts a single attribute as mentioned above and described below.
- align: This attribute is used to specify the alignment of text content.
Below examples illustrate the <caption> tag in HTML:
Example 1: Adding a caption to the table i.e. by default aligned to the center.
html
<!DOCTYPE html> < html > < body > < h1 >GeeksForGeeks</ h1 > < h2 >HTML < Caption > Tag</ h2 > < table > <!-- Adding caption to the table --> < caption >Students</ caption > < tr > < th >Firstname</ th > < th >Lastname</ th > < th >Age</ th > </ tr > < tr > < td >Priya</ td > < td >Sharma</ td > < td >24</ td > </ tr > < tr > < td >Arun</ td > < td >Singh</ td > < td >32</ td > </ tr > < tr > < td >Sam</ td > < td >Watson</ td > < td >41</ td > </ tr > </ table > </ body > </ html > |
Output:
Example 2: Adding a caption to the table and adding align attribute to it to align the caption to the left.
html
<!DOCTYPE html> < html > < body > < h1 >GeeksforGeeks</ h1 > < h2 >HTML < Caption > Tag</ h2 > < table > <!-- Adding a caption to the table and aligning it to the left--> < caption style = "text-align: left" > Students </ caption > < tr > < th >Firstname</ th > < th >Lastname</ th > < th >Age</ th > </ tr > < tr > < td >Priya</ td > < td >Sharma</ td > < td >24</ td > </ tr > < tr > < td >Arun</ td > < td >Singh</ td > < td >32</ td > </ tr > < tr > < td >Sam</ td > < td >Watson</ td > < td >41</ td > </ tr > </ table > </ body > </ html > |
Output:
Supported Browsers:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari