--- title: HTML table basics slug: Learn/HTML/Tables/Basics translation_of: Learn/HTML/Tables/Basics original_slug: Aprender/HTML/Tables/Basics ---
Este artigo é uma introdução às tabelas HTML, cobrindo o básico, como linhas e células, cabeçalhos, fazendo as células ocuparem várias colunas e linhas e como agrupar todas as células em uma coluna para fins de estilo.
Pré-requisitos: | Noções básicas de HTML (consulte Introdução ao HTML ). |
---|---|
Objetivo: | Para obter familiaridade básica com tabelas HTML. |
Uma tabela é um conjunto estruturado de dados composto de linhas e colunas (dados tabulares). Uma tabela permite consultar de forma rápida e fácil valores que indicam algum tipo de conexão entre diferentes tipos de dados, por exemplo, uma pessoa e sua idade, ou um dia da semana, ou os horários de uma piscina local.
As tabelas são muito comumente usadas na sociedade humana, e têm sido por muito tempo, como evidenciado por este documento do Censo dos EUA de 1800:
Portanto, não é de se admirar que os criadores do HTML tenham fornecido um meio de estruturar e apresentar dados tabulares na web.
Tabelas possuem estrutura. As informações são facilmente interpretadas fazendo associações visuais entre os cabeçalhos de linha e coluna. Veja a tabela abaixo, por exemplo, e encontre um gigante gasoso Júpiter com 62 luas. Você pode encontrar a resposta associando os cabeçalhos de linha e coluna relevantes.
Nome | Massa (10 24 kg) | Diâmetro (km) | Densidade (kg/m 3 ) | Gravidade (m/s2) | Duração do dia (hours) | Distancia do Sol (106km) | Temperatura Média (°C) | Fases da Lua | Anotações | ||
---|---|---|---|---|---|---|---|---|---|---|---|
Planetas Terrestres | Mercúrio | 0.330 | 4,879 | 5427 | 3.7 | 4222.6 | 57.9 | 167 | 0 | Proximidade do Sol | |
Venus | 4.87 | 12,104 | 5243 | 8.9 | 2802.0 | 108.2 | 464 | 0 | |||
Terra | 5.97 | 12,756 | 5514 | 9.8 | 24.0 | 149.6 | 15 | 1 | Nosso Planeta | ||
Marte | 0.642 | 6,792 | 3933 | 3.7 | 24.7 | 227.9 | -65 | 2 | O Planeta Vermelho | ||
Planetas Jovianos | Gigantes Gasosos | Jupiter | 1898 | 142,984 | 1326 | 23.1 | 9.9 | 778.6 | -110 | 67 | O Maior Planeta |
Saturno | 568 | 120,536 | 687 | 9.0 | 10.7 | 1433.5 | -140 | 62 | |||
Gigante de gelo | Urano | 86.8 | 51,118 | 1271 | 8.7 | 17.2 | 2872.5 | -195 | 27 | ||
Netuno | 102 | 49,528 | 1638 | 11.0 | 16.1 | 4495.1 | -200 | 14 | |||
Planeta Anão | Plutão | 0.0146 | 2,370 | 2095 | 0.7 | 153.3 | 5906.4 | -225 | 5 | Desconsiderado como planeta desde 2006, mas isto permanece controverso. |
Quando criadas corretamente, até pessoas cegas podem interpretar dados tabulares em uma tabela HTML - uma tabela HTML bem sucedida deve melhorar a experiência tanto de usuários com deficiências visuais quanto daqueles capazes de ver
Você também pode dar uma neste exemplo real no GitHub! Uma coisa que você perceberá é que essa tabela parece um pouco mais legível lá — isso ocorre porque a tabela que você vê acima nesta página tem o mínimo de estilização, enquanto a versão do GitHub tem um CSS expressivo aplicado.
Be under no illusion; for tables to be effective on the web, you need to provide some styling information with CSS, as well as good solid structure with HTML. In this module we are focusing on the HTML part; to find out about the CSS part you should visit our Styling tables article after you've finished here.
We won't focus on CSS in this module, but we have provided a minimal CSS stylesheet for you to use that will make your tables more readable than the default you get without any styling. You can find the stylesheet here, and you can also find an HTML template that applies the stylesheet — these together will give you a good starting point for experimenting with HTML tables.
HTML tables should be used for tabular data — this is what they are designed for. Unfortunately, a lot of people used to use HTML tables to lay out web pages, e.g. one row to contain the header, one row to contain the content columns, one row to contain the footer, etc. You can find more details and an example at Page Layouts in our Accessibility Learning Module. This was commonly used because CSS support across browsers used to be terrible; table layouts are much less common nowadays, but you might still see them in some corners of the web.
In short, using tables for layout rather than CSS layout techniques is a bad idea. The main reasons are as follows:
We've talked table theory enough, so, let's dive into a practical example and build up a simple table.
<table></table>
. Add these inside the body of your HTML.<td>
element ('td' stands for 'table data'). Add the following inside your table tags:
<td>Hi, I'm your first cell.</td>
<td>Hi, I'm your first cell.</td> <td>I'm your second cell.</td> <td>I'm your third cell.</td> <td>I'm your fourth cell.</td>
As you will see, the cells are not placed underneath each other, rather they are automatically aligned with each other on the same row. Each <td>
element creates a single cell and together they make up the first row. Every cell we add makes the row grow longer.
To stop this row from growing and start placing subsequent cells on a second row, we need to use the <tr>
element ('tr' stands for 'table row'). Let's investigate this now.
<tr>
tags, like so:
<tr> <td>Hi, I'm your first cell.</td> <td>I'm your second cell.</td> <td>I'm your third cell.</td> <td>I'm your fourth cell.</td> </tr>
<tr>
element, with each cell contained in a <td>
.This should result in a table that looks something like the following:
Hi, I'm your first cell. | I'm your second cell. | I'm your third cell. | I'm your fourth cell. |
Second row, first cell. | Cell 2. | Cell 3. | Cell 4. |
Note: You can also find this on GitHub as simple-table.html (see it live also).
Now let's turn our attention to table headers — special cells that go at the start of a row or column and define the type of data that row or column contains (as an example, see the "Person" and "Age" cells in the first example shown in this article). To illustrate why they are useful, have a look at the following table example. First the source code:
<table> <tr> <td> </td> <td>Knocky</td> <td>Flor</td> <td>Ella</td> <td>Juan</td> </tr> <tr> <td>Breed</td> <td>Jack Russell</td> <td>Poodle</td> <td>Streetdog</td> <td>Cocker Spaniel</td> </tr> <tr> <td>Age</td> <td>16</td> <td>9</td> <td>10</td> <td>5</td> </tr> <tr> <td>Owner</td> <td>Mother-in-law</td> <td>Me</td> <td>Me</td> <td>Sister-in-law</td> </tr> <tr> <td>Eating Habits</td> <td>Eats everyone's leftovers</td> <td>Nibbles at food</td> <td>Hearty eater</td> <td>Will eat till he explodes</td> </tr> </table>
Now the actual rendered table:
Knocky | Flor | Ella | Juan | |
Breed | Jack Russell | Poodle | Streetdog | Cocker Spaniel |
Age | 16 | 9 | 10 | 5 |
Owner | Mother-in-law | Me | Me | Sister-in-law |
Eating Habits | Eats everyone's leftovers | Nibbles at food | Hearty eater | Will eat till he explodes |
The problem here is that, while you can kind of make out what's going on, it is not as easy to cross reference data as it could be. If the column and row headings stood out in some way, it would be much better.
Let's have a go at improving this table.
<th>
element ('th' stands for 'table header'). This works in exactly the same way as a <td>
, except that it denotes a header, not a normal cell. Go into your HTML, and change all the <td>
elements surrounding the table headers into <th>
elements.Note: You can find our finished example at dogs-table-fixed.html on GitHub (see it live also).
We have already partially answered this question — it is easier to find the data you are looking for when the headers clearly stand out, and the design just generally looks better.
Note: Table headings come with some default styling — they are bold and centered even if you don't add your own styling to the table, to help them stand out.
Tables headers also have an added benefit — along with the scope
attribute (which we'll learn about in the next article), they allow you to make tables more accessible by associating each header with all the data in the same row or column. Screenreaders are then able to read out a whole row or column of data at once, which is pretty useful.
Sometimes we want cells to span multiple rows or columns. Take the following simple example, which shows the names of common animals. In some cases, we want to show the names of the males and females next to the animal name. Sometimes we don't, and in such cases we just want the animal name to span the whole table.
The initial markup looks like this:
<table> <tr> <th>Animals</th> </tr> <tr> <th>Hippopotamus</th> </tr> <tr> <th>Horse</th> <td>Mare</td> </tr> <tr> <td>Stallion</td> </tr> <tr> <th>Crocodile</th> </tr> <tr> <th>Chicken</th> <td>Hen</td> </tr> <tr> <td>Rooster</td> </tr> </table>
But the output doesn't give us quite what we want:
Animals | |
---|---|
Hippopotamus | |
Horse | Mare |
Stallion | |
Crocodile | |
Chicken | Hen |
Rooster |
We need a way to get "Animals", "Hippopotamus", and "Crocodile" to span across two columns, and "Horse" and "Chicken" to span downwards over two rows. Fortunately, table headers and cells have the colspan
and rowspan
attributes, which allow us to do just those things. Both accept a unitless number value, which equals the number of rows or columns you want spanned. For example, colspan="2"
makes a cell span two columns.
Let's use colspan
and rowspan
to improve this table.
colspan
to make "Animals", "Hippopotamus", and "Crocodile" span across two columns.rowspan
to make "Horse" and "Chicken" span across two rows.Note: You can find our finished example at animals-table-fixed.html on GitHub (see it live also).
There is one last feature we'll tell you about in this article before we move on. HTML has a method of defining styling information for an entire column of data all in one place — the <col>
and <colgroup>
elements. These exist because it can be a bit annoying and inefficient having to specify styling on columns — you generally have to specify your styling information on every <td>
or <th>
in the column, or use a complex selector such as {{cssxref(":nth-child()")}}.
Note: Styling columns like this is limited to a few properties: border
, background
, width
, and visibility
. To set other properties you'll have to either style every <td>
or <th>
in the column, or use a complex selector such as {{cssxref(":nth-child()")}}.
Take the following simple example:
<table> <tr> <th>Data 1</th> <th style="background-color: yellow">Data 2</th> </tr> <tr> <td>Calcutta</td> <td style="background-color: yellow">Orange</td> </tr> <tr> <td>Robots</td> <td style="background-color: yellow">Jazz</td> </tr> </table>
Which gives us the following result:
Data 1 | Data 2 |
---|---|
Calcutta | Orange |
Robots | Jazz |
This isn't ideal, as we have to repeat the styling information across all three cells in the column (we'd probably have a class
set on all three in a real project and specify the styling in a separate stylesheet). Instead of doing this, we can specify the information once, on a <col>
element. <col>
elements are specified inside a <colgroup>
container just below the opening <table>
tag. We could create the same effect as we see above by specifying our table as follows:
<table> <colgroup> <col> <col style="background-color: yellow"> </colgroup> <tr> <th>Data 1</th> <th>Data 2</th> </tr> <tr> <td>Calcutta</td> <td>Orange</td> </tr> <tr> <td>Robots</td> <td>Jazz</td> </tr> </table>
Effectively we are defining two "style columns", one specifying styling information for each column. We are not styling the first column, but we still have to include a blank <col>
element — if we didn't, the styling would just be applied to the first column.
If we wanted to apply the styling information to both columns, we could just include one <col>
element with a span attribute on it, like this:
<colgroup> <col style="background-color: yellow" span="2"> </colgroup>
Just like colspan
and rowspan
, span
takes a unitless number value that specifies the number of columns you want the styling to apply to.
Now it's time to have a go yourself.
Below you can see the timetable of a languages teacher. On Friday she has a new class teaching Dutch all day, but she also teaches German for a few periods on Tuesday and Thursdays. She wants to highlight the columns containing the days she is teaching.
{{EmbedGHLiveSample("learning-area/html/tables/basic/timetable-fixed.html", '100%', 320)}}
Recreate the table by following the steps below.
<colgroup>
element at the top of the table, just underneath the <table>
tag, in which you can add your <col>
elements (see the remaining steps below).style
attribute is background-color:#97DB9A;
style
attribute is width: 42px;
style
attribute is background-color: #97DB9A;
style
atributo sãobackground-color:#DCC48E; border:4px solid #C1437A;
style
atributo éwidth: 42px;
Veja como você segue com o exemplo. Se você tiver dúvidas ou quiser verificar seu trabalho, pode encontrar nossa versão no GitHub como schedule-fixed.html ( veja ao vivo também ).
Isso envolve o básico das tabelas HTML. No próximo artigo, veremos alguns recursos de mesa um pouco mais avançados e começaremos a pensar como eles são acessíveis para pessoas com deficiência visual.