How to create a table with headers?

WBOY
Release: 2023-08-24 22:49:03
forward
1420 people have browsed it

How to create a table with headers?

We use the

tag to create a table with a title in HTML. The title tag will be inserted after the tag. We can add a title to a table. By default, the label's alignment is centered. We can change the alignment using the CSS align property.

grammar

The following is the syntax of the HTML title tag

<caption>Caption of the table...</caption>
Copy after login

Example 1

In the example below, we create a table containing employee names and employee IDs using Employees as the title.

<!DOCTYPE html>
<html>
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <style>
      table,
         tr,
         th,
         td {
            border: 1px solid black;
         }
   </style>
</head>
<body>
   <table>
      <caption>Employees</caption>
      <tr>
         <th>EmployeeName</th>
         <th>EmployeeId</th>
      </tr>
      <tr>
         <td>Yadav</td>
         <td>022</td>
      </tr>
      <tr>
         <td>Abdul</td>
         <td>025</td>
      </tr>
      <tr>
         <td>Jason</td>
         <td>208</td>
      </tr>
   </table>
</body>
</html>
Copy after login

Alignment properties

We can use the CSS attribute align to change the alignment. The following is its syntax:

<caption style="text-align:value" >Caption of the table...</caption>
Copy after login

Example 2

is translated as:

Example 2

In the example below, we align the title to the left side of the page -

<!DOCTYPE html>
<html>
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <style>
      table,tr,th,td {
         border:1px solid black;
      }
   </style>
</head>
<body>
   <table>
      <caption style="text-align: left">Employees</caption>
      <tr>
         <th>EmployeeName</th>
         <th>EmployeeId</th>
      </tr>
      <tr>
         <td>Yadav</td>
         <td>022</td>
      </tr>
      <tr>
         <td>Abdul</td>
         <td>025</td>
      </tr>
      <tr>
         <td>Jason</td>
         <td>208</td>
      </tr>
   </table>
</body>
</html>
Copy after login

The Chinese translation of

Example 3

is:

Example 3

Let’s look at another example of creating a title -

<!DOCTYPE html>
<html>
<head>
   <style>
      table,
      td,
      th {
         border: 1px solid black;
      }
   </style>
</head>
<body>
   <table>
      <caption>Authors</caption>
      <tr>
         <th>JavaFX</th>
         <th>Krishna</th>
      </tr>
      <tr>
         <td>Scala</td>
         <td>Satish</td>
      </tr>
   </table>
</body>
</html>
Copy after login

The above is the detailed content of How to create a table with headers?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template