/

HTML / CSS / JavaScript Tutorial

HTML Element: <caption>

[this page | pdf | back links]

The HTML <caption> element indicates a table caption. It should be inserted immediately after opening <table> tag of the <table> element.

 

The attributes it can take are HTML global attributes and HTML event attributes. It used to support the align attribute, but this is no longer supported in HTML 5.

 

To create or access such an element in JavaScript see here. The corresponding HTML DOM object supports standard DOM properties and methods. The default style applicable to this element is shown here.

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
  table,td,th,tr,caption {border: thin solid black; border-collapse: collapse;}
  th {background-color: yellow;}
  thead {color: blue}
</style>
</head>
<body>
Created using HTML:<br>
<table>
  <caption>A table</caption>
  <thead>
    <tr><th>col1</th><th>col2</th></tr>
  </thead>
</table>

<br>Created using JavaScript:<br>
<span id="element"></span>

<script>
var y1 = document.createElement("CAPTION"); y1.textContent = "A table";
var y2a1 = document.createElement("TH"); y2a1.textContent = "col1";
var y2a2 = document.createElement("TH"); y2a2.textContent = "col2";
var y2a = document.createElement("TR"); y2a.appendChild(y2a1); y2a.appendChild(y2a2);
var y2 = document.createElement("THEAD"); y2.appendChild(y2a);
var x = document.createElement("TABLE");
x.appendChild(y1); x.appendChild(y2);
document.getElementById("element").appendChild(x);
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | HTML Elements


Desktop view | Switch to Mobile