Tabelas HTML

O objetivo deste artigo é apresentá-lo aos fundamentos das tabelas HTML. Isso inclui como criar uma tabela usando tags HTML, como adicionar dados e formatação e como projetar tabelas eficazes e fáceis de usar.

As tabelas HTML permitem que os criadores de sites organizem os dados em linhas e colunas.

Tabelas HTML são criadas usando a tag <table> , onde as linhas da tabela são criadas por tags <tr> e as células de dados são criadas por tags <td> .

Os elementos sob <td> são alinhados automaticamente à esquerda.

Company Contact Country
Ankunding PLC Raynor Christopher Italy
Reichert LLC Roberts Marisa USA
Schaefer, Sipes and Macejkovic Anais Kihn France
Schoen Ltd Briana China
Koss LLC Marques Japan
Hermann PLC Coby Wolff London

Example: 

1
2
3
4
5
6
7
<!DOCTYPE html>
<html><body><br>
<br>
<br>
<style><br> h2{<br> font-size:40px;<br> text-align:center;<br> }<br> <br> table{<br> border:3px solid black;<br> }<br> th{<br> border:2px solid black;<br> font-size:25px;<br> padding:10px;<br> }<br> td{<br> border:2px solid black; <br> padding:12px;<br> width:170px;<br> text-align:center;<br> }<br> </style><br><br><br><h2>HTML Table</h2><br><table><br><tr><br><th>Company</th><br><th>Contact</th><br><th>Country</th><br></tr><br><tr><br><td>Ankunding PLC</td><br><td>raynor christopher</td><br><td>Italy</td><br></tr><br><tr><br><td>Reichert LLC</td><br><td>roberts marisa</td><br><td>USA</td><br></tr><br><tr><br><td>Schaefer, Sipes and Macejkovic</td><br><td>anais kihn</td><br><td>France</td><br></tr><br><tr><br><td>Schoen Ltd</td><br><td>briana</td><br><td>china</td><br></tr><br><tr><br><td>Koss LLC</td><br><td>marques</td><br><td>japan</td><br></tr><br><tr><br><td>Hermann PLC</td><br><td>coby wolff</td><br><td>london</td><br></tr><br></table><br><br>
</body></html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


Create an HTML Table

The <table> tag represents a table in an HTML document.

<tr> tags are utilized to characterize each table row.

The header of each table is described by a <th> tag. Several types of table data/cells are represented with the <td> tag.

The text in <th> elements is bold and centered by default, and text in <td> elements is left-aligned and regular by default.

The HTML table looks like this:

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<!DOCTYPE html>
<html>
<body>
<table style="width:100%">
<tr>
<th>F name</th>
<th>L name</th>
<th>Age</th>
</tr>
<tr>
<td>Johnny</td>
<td>Depp</td>
<td>50</td>
</tr>
<tr>
<td>Marc</td>
<td>Anthony</td>
<td>55</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>28</td>
</tr>
</table>
</body>
</html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Note: Table data is contained in the <td> elements. Text, images, lists, other tables, and other HTML elements can be included in these documents.


Add HTML Tables Border

Use CSS border property to add a border to a table:

Example: 

1
2
3
4
<!DOCTYPE html>
<html><body><br><br><br><style><br><br>table, th, td<br>{<br>border: 2px solid grey;<br>}<br><br></style><br><br><table style="width:100%"><br><tr><br><th>F name</th><br><th>L name</th><br><th>Age</th><br></tr><br><tr><br><td>Johnny</td><br><td>Depp</td><br><td>50</td><br></tr><br><tr><br><td>Marc</td><br><td>Anthony</td><br><td>55</td><br></tr><br><br><tr><br><td>John</td><br><td>Doe</td><br><td>28</td><br></tr><br></table><br><br>
</body></html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


HTML Tables – Collapsed Borders

You can collapse the borders into one by adding the CSS border-collapse property:

Example: 

1
2
3
4
<!DOCTYPE html>
<html><body><br><br><br><br><br><style><br><br>table, th, td {<br>border: 2px solid grey;border-collapse: collapse;<br>}<br><br></style><br><br><table style="width:100%"><br><tr><br><th>F name</th><br><th>L name</th><br><th>Age</th><br></tr><br><tr><br><td>Johnny</td><br><td>Depp</td><br><td>50</td><br></tr><br><tr><br><td>Marc</td><br><td>Anthony</td><br><td>55</td><br></tr><br><br><tr><br><td>John</td><br><td>Doe</td><br><td>28</td><br></tr><br></table><br><br>
</body></html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

HTML Tables – Left-align Headings

Table headings are bolded and centered by default.

The CSS text-align property can be used to left-align the table headings:

Example: 

1
2
3
4
<!DOCTYPE html>
<html><body><br><br><br><br><br><style><br><br>table, th, td {<br>border: 2px solid grey;border-collapse: collapse;<br>text-align: center;<br>}<br><br></style><br><br><table style="width:100%"><br><tr><br><th>F name</th><br><th>L name</th><br><th>Age</th><br></tr><br><tr><br><td>Johnny</td><br><td>Depp</td><br><td>50</td><br></tr><br><tr><br><td>Marc</td><br><td>Anthony</td><br><td>55</td><br></tr><br><br><tr><br><td>John</td><br><td>Doe</td><br><td>28</td><br></tr><br></table><br><br><br>
</body></html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

HTML Table – Add Border Spacing

The border-spacing between cells defines the distance between them.

CSS border-spacing can be used to set the border-spacing for a table:

Note that border-spacing has no impact if the table collapses.

Example: 

1
2
3
4
<!DOCTYPE html>
<html><body><br><br><br><br><br><style><br><br>table, th, td {<br>border: 2px solid grey;border-collapse: collapse;<br>text-align: center;<br>border-spacing: 3px;<br><br>}<br><br><br></style><br><br><table style="width:100%"><br><tr><br><th>F name</th><br><th>L name</th><br><th>Age</th><br></tr><br><tr><br><td>Johnny</td><br><td>Depp</td><br><td>50</td><br></tr><br><tr><br><td>Marc</td><br><td>Anthony</td><br><td>55</td><br></tr><br><br><tr><br><td>John</td><br><td>Doe</td><br><td>28</td><br></tr><br></table><br><br>
</body></html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


HTML Tables – Add Cell Padding

The padding between the border and the cell content describes the space between the two.

Table cells will show without padding if you do not select a padding.

The CSS padding property can be utilized to set padding:

Example: 

1
2
3
4
<!DOCTYPE html>
<html><body><br><br><br><br><br><style><br><br>table, th, td {<br>border: 2px solid grey;border-collapse: collapse;<br>text-align: center;<br>border-spacing: 3px;<br>}<br>th, td {<br>padding: 30px;<br>}<br><br><br><br></style><br><br><table style="width:100%"><br><tr><br><th>F name</th><br><th>L name</th><br><th>Age</th><br></tr><br><tr><br><td>Johnny</td><br><td>Depp</td><br><td>50</td><br></tr><br><tr><br><td>Marc</td><br><td>Anthony</td><br><td>55</td><br></tr><br><br><tr><br><td>John</td><br><td>Doe</td><br><td>28</td><br></tr><br></table><br><br><br>
</body></html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

HTML Tables Width and Height

Using the width and height attributes, you can adjust the width and height of a table. In addition to defining table width and height in pixels, you can specify them as percent of available screen space. See example below:

Example: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html>
<head>
<title>Table with Width & Height</title>
</head>
<body>
<table border="1" width="250" height="100">
<tr>
<td>Elon Musk</td>
<td>Jeff Bezos</td>
</tr>
<tr>
<td>Steve Jobs</td>
<td>Mark Zuckerberg</td>
</tr>
</table>
</body>
</html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

HTML Tables – Cell that Spans Many Columns

Utilize the colspan attribute to create a cell span of more than one column:

Example: 

1
2
3
4
<!DOCTYPE html>
<html><body><br><br><br> <style><br> h2{<br> font-size:40px;<br> text-align:center;<br> }<br> <br> table{<br> border:3px solid black;<br> }<br> th{<br> border:2px solid black;<br> font-size:25px;<br> padding:10px;<br> }<br> td{<br> border:2px solid black; <br> padding:12px;<br> width:170px;<br> text-align:center;<br> }<br> </style><br><br><br><br><table style="width:100%"><br><tr><br><th>Name</th><th colspan="2">Telephone</th><br></tr><br><tr><td>Elon Musk</td><br><td>99955544</td><td>99955545</td><br></tr><br></table><br><br><br>
</body></html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

HTML Tables – Cell that Spans Many Rows

Utilize the rowspan attribute to create a cell span of more than one row:

Example: 

1
2
3
4
<!DOCTYPE html>
<html><body><br><br><br> <style><br> h2{<br> font-size:40px;<br> text-align:center;<br> }<br> <br> table{<br> border:3px solid black;<br> }<br> th{<br> border:2px solid black;<br> font-size:25px;<br> padding:10px;<br> }<br> td{<br> border:2px solid black; <br> padding:12px;<br> width:170px;<br> text-align:center;<br> }<br> </style><br><br><br><br><table style="width:100%"><br><tr><br><th>Name:</th><br><td>Elon Musk</td><br></tr><tr><br><th rowspan="2">Telephone:</th><td>99955544</td><br></tr><br><tr><td>99955545</td><br></tr><br></table><br><br><br>
</body></html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

HTML Table – Add a Caption

Captions are added to tables with the <caption> tag:

Please remember to include the <caption> tag right after the <table> tag.

Example: 

1
2
3
4
<!DOCTYPE html>
<html><body><br><br><br> <style><br> h2{<br> font-size:40px;<br> text-align:center;<br> }<br> <br> table{<br> border:3px solid black;<br> }<br> th{<br> border:2px solid black;<br> font-size:25px;<br> padding:10px;<br> }<br> td{<br> border:2px solid black; <br> padding:12px;<br> width:170px;<br> text-align:center;<br> }<br> caption{<br> font-size: 32px;<br> font-weight:bolder;<br> }<br> </style><br><br><br><br><table style="width:100%"><br><caption>Total Monthly savings</caption><br><tr><br><th>Month</th><br><th>Savings</th><br></tr><br><tr><br><td>January</td><br><td>$200</td><br></tr><br><tr><br><td>February</td><br><td>$300</td><br></tr><br></table><br><br><br>
</body></html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

A Special Style for One Table

Adding an id attribute to a table will let you specify a characteristic style for it:

Example: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html>
<html>
<body>
<table id="t01">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>kevin</td>
<td>smith</td>
<td>32</td>
</tr>
</table>
</body>
</html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

You can particularly design only the desired table

1
2
3
4
5
6
7
8
<!DOCTYPE html>
<html><body><p>Example:
Execute
</p><br><br><br><style><br><br>#t01 {<br>width: 100%;<br>background-color: #f1f1c1;<br>}<br><br></style><br><br><br><br><table id="t01"><br><tr><br><th>Firstname</th><br><th>Lastname</th><br><th>Age</th><br></tr><br><tr><br><td>kevin</td><br><td>smith</td><br><td>32</td><br></tr><br></table><br><br><br>
</body></html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Você também pode adicionar mais estilo à tabela, conforme mostrado no exemplo abaixo:

Example: 

1
2
3
4
<!DOCTYPE html>
<html><body><br><br><br><style><br><br>#t01 tr:nth-child(even) {<br>background-color: #eee;<br>}<br>#t01 tr:nth-child(odd) {<br>background-color: #fff;<br>}<br>#t01 th {<br>color: white;<br>background-color: black;<br>}<br><br></style><br><br><br><br><table id="t01"><br><tr><br><th>Firstname</th><br><th>Lastname</th><br><th>Age</th><br></tr><br><tr><br><td>kevin</td><br><td>smith</td><br><td>32</td><br></tr><br></table><br><br><br>
</body></html>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


Etiquetas de Tabela HTML

Marcação Visão geral
<tabela> Cria uma tabela.
<col> Os atributos de coluna são definidos para cada coluna dentro de um elemento <colgroup>.
<tr> Em uma tabela, especifica uma linha.
<td> Cria uma célula em uma tabela.
<legenda> Cria uma legenda de tabela.
<th> Fornece uma célula de cabeçalho para uma tabela.
<colgroup> Formata uma ou mais colunas em uma tabela.
<thead> Uma tabela organizada de conteúdo de cabeçalho.
<pé> Cria uma tabela com o conteúdo do rodapé.
<corpo> Cria uma tabela com o conteúdo do corpo nela.

Observação: Referência de marca HTML lista todas as marcas possíveis.


Resumo do capítulo

  • Representar tabelas usando o elemento Html Tables <table> .
  • Opere o elemento HTML <tr> para descrever uma linha da tabela.
  • Utilize o elemento HTML <td> para definir os dados de uma tabela.
  • Os cabeçalhos das tabelas são especificados usando o elemento HTML <th> .
  • Use o elemento HTML <caption> para adicionar legendas às tabelas.
  • Estabeleça uma borda usando a propriedade border do CSS .
  • Para reduzir as bordas das células, use a propriedade border-collapse do CSS .
  • Para adicionar preenchimento às células, use a propriedade CSS padding .
  • Para alinhamento do texto da célula, use a propriedade CSS text-align .
  • Você pode ajustar o espaçamento entre as células usando a propriedade CSS border-spacing .
  • Crie um span de várias colunas usando o atributo colspan .
  • Faça uma célula abranger muitas linhas usando o atributo rowspan .
  • Para identificar exclusivamente uma tabela, use o atributo id .

Se este artigo ajudou você a aprender sobre tabelas HTML, compartilhe essas informações significativas com seus amigos clicando nos links abaixo.
Nós valorizamos o seu feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Assine a nossa newsletter
Digite seu e-mail para receber um resumo semanal de nossos melhores posts. Saber mais!
ícone