w3resource

HTML5: How to define a table?

Go to Exercise page

Solution:

HTML Code:

<!DOCTYPE html><!-- Define document type as HTML -->
<html><!-- Begin HTML document -->
<head><!-- Start of document header -->
<meta charset="utf-8"><!-- Define character encoding -->
<title>How to define a table</title><!-- Title of the HTML page -->
</head><!-- End of document header -->
<body><!-- Start of document body -->
<table border="1"><!-- Table element with border attribute set to "1" -->
<tr><!-- Table row element -->
<th>Student code </th><!-- Table header cell for student code column -->
<th>% of marks </th><!-- Table header cell for percentage of marks column -->
</tr><!-- End of table row -->
<tr><!-- Table row element -->
<td>S001</td><!-- Table data cell with student code "S001" -->
<td>85</td><!-- Table data cell with marks percentage "85" -->
</tr><!-- End of table row -->
<tr><!-- Table row element -->
<td>S002</td><!-- Table data cell with student code "S002" -->
<td>86</td><!-- Table data cell with marks percentage "86" -->
</tr><!-- End of table row -->
<tr><!-- Table row element -->
<td>S003</td><!-- Table data cell with student code "S003" -->
<td>72</td><!-- Table data cell with marks percentage "72" -->
</tr><!-- End of table row -->
<tr><!-- Table row element -->
<td>S004</td><!-- Table data cell with student code "S004" -->
<td>89</td><!-- Table data cell with marks percentage "89" -->
</tr><!-- End of table row -->
</table><!-- End of table -->
</body><!-- End of document body -->
</html><!-- End of HTML document -->

Explanation:

  • This HTML code defines a webpage with a simple table displaying student codes and their corresponding percentages of marks.
  • The <table> tag creates a table structure.
  • Each row of the table is defined by the <tr> (table row) tag.
  • Within each row, data cells are defined using the <td> (table data) tag for non-header cells and <th> (table header) tag for header cells.
  • The table includes two columns: "Student code" and "% of marks", represented by the <th> elements in the first row.
  • Subsequent rows contain student codes and their respective marks percentages within <td> elements.
  • The border="1" attribute in the <table> tag adds a border around the table cells for better visualization.

Live Demo:

See the Pen table-answer by w3resource (@w3resource) on CodePen.


See the solution in the browser

Supported browser

Firefox logo Chrome logo Opera logo Safari logo Internet Explorer logo
Yes Yes Yes Yes Yes

Go to Exercise page

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/html-css-exercise/basic/table-answer.php