HTML Element Style How to - Align table text center








Question

We would like to know how to align table text center.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
table {<!--   w ww .ja  va 2 s . com-->
  text-align: center;
  width: 400px;
}

td {
  padding: 10px;
  border: 1px solid black;
}
</style>
</head>
<body>
  <table>
    <tbody>
      <tr>
        <td colspan="4">header</td>
      </tr>
      <tr>
        <td>1</td>
        <td width="20">2</td>
        <td>3</td>
      </tr>
      </tobdy>
  </table>
</body>
</html>

The code above is rendered as follows: