<table>

HTML <table> Tag

The <table> tag defines an HTML table. It contains other HTML elements that determine the structure of the table.

The <tr> tag determines table rows. A row can have one or more <td> or <th> elements, which define a table cell and a table header respectively.

The <th> tag is placed in the first row of the table. The text in it is bold and centered by default.

The <th> tag is not a mandatory element in the table, but we recommend to use it, as it helps to have better table layout as well as helps search engines better index the content of the table.

A more complex table can also include <caption>, <thead>, <tbody>, <tfoot>, or <colgroup> elements.

It’s recommended not to use tables for page layout. Sometimes tables are misused in HTML for controlling a page layout. For that, you can use CSS as an alternative to HTML tables.

Syntax

Syntax

The <table> tag comes in pairs. The content is written between the opening (<table>) and closing (</table>) tags.

Example of the HTML <table> tag:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      table {
        width: 80%;
        margin: 30px auto;
        border-collapse: collapse;
      }
      th,
      td {
        padding: 10px;
        border: 1px solid #666;
      }
    </style>
  </head>
  <body>
    <table>
      <tr>
        <th>Month</th>
        <th>Date</th>
      </tr>
      <tr>
        <td>June</td>
        <td>10.06.2018</td>
      </tr>
      <tr>
        <td>July</td>
        <td>15.07.2018</td>
      </tr>
    </table>
  </body>
</html>

Result

Attributes

Attributes

The attributes of the <table> tag are not supported in HTML5. For styling tables use CSS properties listed below.

AttributeValueDescriptionCSS property
alignleftcenterrightDefines how the table must be aligned according to the surrounding text.
Not supported in HTML5.margin
bgcolorrgb(x,x,x)#xxxxxxcolornameDefines the background color for a table.
Not supported in HTML5.background-color
border10Defines the size of the frame surrounding the table.
Not supported in HTML5.border
cellpaddingpixelsDefines the space between the cell wall and the cell content.
Not supported in HTML5.border-collapse
cellspacingpixelsDefines the space between cells.
Not supported in HTML5.border-spacing
framevoidabovebelowhsideslhsrhsvsidesboxborderDefines which side of the frame surrounding the table must be displayed.
Not supported in HTML5.border-styleborder-width
rulesnonegroupsrowscolsallDefines which parts of inside borders should be visible.
Not supported in HTML5.border(Use this property correspondingly with thead,tbody,tfoot,col or colgroup HTML tags).
summarytextDefines the summary for the content of a table.
Not supported in HTML5.caption-side
widthpixelsDefines the width of a table.
Not supported in HTML5.width

The <table> also supports the Global Attributes and the Event Attributes.

How to style <table> tag? Common properties to alter the visual weight/emphasis/size of text in <table> tag:

CSS font-style property sets the style of the font. normal | italic | oblique | initial | inherit. CSS font-family property specifies a prioritized list of one or more font family names and/or generic family names for the selected element. CSS font-size property sets the size of the font. CSS font-weight property defines whether the font should be bold or thick. CSS text-transform property controls text case and capitalization. CSS text-decoration property specifies the decoration added to text, and is a shorthand property for text-decoration-line, text-decoration-color, text-decoration-style.

Coloring text in <table> tag:

CSS color property describes the color of the text content and text decorations. CSS background-color property sets the background color of an element.

Text layout styles for <table> tag:

CSS text-indent property specifies the indentation of the first line in a text block. CSS text-overflow property specifies how overflowed content that is not displayed should be signalled to the user. CSS white-space property specifies how white-space inside an element is handled. CSS word-break property specifies where the lines should be broken.

Other properties worth looking at for <table> tag:

CSS text-shadow property adds shadow to text. CSS text-align-last property sets the alignment of the last line of the text. CSS line-height property specifies the height of a line. CSS letter-spacing property defines the spaces between letters/characters in a text. CSS word-spacing property sets the spacing between words.



请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部