aboutsummaryrefslogtreecommitdiff
path: root/files/pt-pt/learn/html/tables/basics/index.html
blob: 7dac1c7d99f1a9063c64707faf982d26718ea8ec (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
---
title: HTML - o essencial de tabela
slug: Learn/HTML/Tables/Basics
tags:
  - Aprender
  - Artigo
  - HTML
  - Linha
  - Principiante
  - básicos
  - cabeçalho
  - coluna
  - célula
  - essencial
  - tabelas
translation_of: Learn/HTML/Tables/Basics
original_slug: Learn/HTML/Tables/Basicos
---
<div>{{LearnSidebar}}</div>

<div>{{NextMenu("Learn/HTML/Tables/Avancada", "Learn/HTML/Tables")}}</div>

<p class="summary">Este artigo inicia-o nas tabelas HTML, abrangendo o essencial, tais como linhas e células, cabeçalhos, distribuir as células por várias colunas e linhas, e como agrupar todas as células em uma coluna para fins de estilização.</p>

<table class="learn-box standard-table">
 <tbody>
  <tr>
   <th scope="row">Pré-requisitos:</th>
   <td>O essencial de HTML (consulte <a href="/pt-PT/docs/Learn/HTML/Introducao_ao_HTML">Introdução ao HTML</a>).</td>
  </tr>
  <tr>
   <th scope="row">Objetivo:</th>
   <td>Familiarizar-se com as tabelas HTML.</td>
  </tr>
 </tbody>
</table>

<h2 id="O_que_é_uma_tabela">O que é uma tabela?</h2>

<p>Uma tabela é um conjunto estruturado de dados constituído por linhas e colunas (<strong>dados tabulares</strong>). Uma tabela permite consultar, fácil e rapidamente, valores que indiquem alguma relação entre diferentes tipos de dados, por exemplo, uma pessoa e a sua idade, ou os dias da semana num horário de uma piscina.</p>

<p><img alt="A sample table showing names and ages of some people - Chris 38, Dennis 45, Sarah 29, Karen 47." src="https://mdn.mozillademos.org/files/14583/numbers-table.png" style="display: block; height: 156px; margin: 0px auto; width: 350px;"></p>

<p><img alt="A swimming timetable showing a sample data table" src="https://mdn.mozillademos.org/files/14587/swimming-timetable.png" style="display: block; height: 301px; margin: 0px auto; width: 794px;"></p>

<p>As tabelas são utilizadas muito frequentemente na sociedade humana, já há muito tempo, como prova este documento dos US Census, datado de 1800:</p>

<p><img alt="A very old parchment document; the data is not easily readable, but it clearly shows a data table being used." src="https://mdn.mozillademos.org/files/14585/1800-census.jpg" style="display: block; height: 505px; margin: 0px auto; width: 800px;"></p>

<p>Não é, portanto, surpreendente que os criadores do HTML forneceram um meio de estruturar e apresentar dados tabulares na web.</p>

<h3 id="Como_é_que_uma_tabela_funciona">Como é que uma tabela funciona?</h3>

<p>O propósito de uma tabela é ser rígida. A informação é interpretada facilmente através de associação visual entre cabeçalhos de linhas e colunas. Olhe para a tabela em baixo, por exemplo, e encontre um gigante gasoso Joviano com 62 luas. Pode chegar à resposta associando os cabeçalhos das linhas e colunas relevantes.</p>

<table>
 <caption>Dados de planetas no sistema solar (Factos planetários retirados de  <a href="http://nssdc.gsfc.nasa.gov/planetary/factsheet/">Nasa's Planetary Fact Sheet - Metric</a>)</caption>
 <thead>
  <tr>
   <td colspan="2"> </td>
   <th scope="col">Nome</th>
   <th scope="col">Massa (10<sup>24</sup>kg)</th>
   <th scope="col">Diâmetro (km)</th>
   <th scope="col">Densidade (kg/m<sup>3</sup>)</th>
   <th scope="col">Gravidade (m/s<sup>2</sup>)</th>
   <th scope="col">Duração do dia (horas)</th>
   <th scope="col">Distância do Sol (10<sup>6</sup>km)</th>
   <th scope="col">Temperatura média (°C)</th>
   <th scope="col">Número de luas</th>
   <th scope="col">Notas</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <th colspan="2" rowspan="4" scope="rowgroup">Planetas rochosos</th>
   <th scope="row">Mercúrio</th>
   <td>0.330</td>
   <td>4 879</td>
   <td>5427</td>
   <td>3,7</td>
   <td>4222,6</td>
   <td>57,9</td>
   <td>167</td>
   <td>0</td>
   <td>O mais próximo do Sol</td>
  </tr>
  <tr>
   <th scope="row">Vénus</th>
   <td>4.87</td>
   <td>12 104</td>
   <td>5243</td>
   <td>8,9</td>
   <td>2802,0</td>
   <td>108,2</td>
   <td>464</td>
   <td>0</td>
   <td> </td>
  </tr>
  <tr>
   <th scope="row">Terra</th>
   <td>5.97</td>
   <td>12 756</td>
   <td>5514</td>
   <td>9,8</td>
   <td>24,0</td>
   <td>149,6</td>
   <td>15</td>
   <td>1</td>
   <td>O nosso mundo</td>
  </tr>
  <tr>
   <th scope="row">Marte</th>
   <td>0.642</td>
   <td>6 792</td>
   <td>3933</td>
   <td>3,7</td>
   <td>24,7</td>
   <td>227,9</td>
   <td>-65</td>
   <td>2</td>
   <td>O planeta vermelho</td>
  </tr>
  <tr>
   <th rowspan="4" scope="rowgroup">Gigantes Jovianos</th>
   <th rowspan="2" scope="rowgroup">Gigantes gasosos</th>
   <th scope="row">Júpiter</th>
   <td>1898</td>
   <td>142 984</td>
   <td>1326</td>
   <td>23,1</td>
   <td>9,9</td>
   <td>778,6</td>
   <td>-110</td>
   <td>67</td>
   <td>O maior planeta</td>
  </tr>
  <tr>
   <th scope="row">Saturno</th>
   <td>568</td>
   <td>120 536</td>
   <td>687</td>
   <td>9,0</td>
   <td>10,7</td>
   <td>1433,5</td>
   <td>-140</td>
   <td>62</td>
   <td> </td>
  </tr>
  <tr>
   <th rowspan="2" scope="rowgroup">Gigantes gelado</th>
   <th scope="row">Urano</th>
   <td>86.8</td>
   <td>51 118</td>
   <td>1271</td>
   <td>8,7</td>
   <td>17,2</td>
   <td>2872,5</td>
   <td>-195</td>
   <td>27</td>
   <td> </td>
  </tr>
  <tr>
   <th scope="row">Neptuno</th>
   <td>102</td>
   <td>49 528</td>
   <td>1638</td>
   <td>11,0</td>
   <td>16,1</td>
   <td>4495,1</td>
   <td>-200</td>
   <td>14</td>
   <td> </td>
  </tr>
  <tr>
   <th colspan="2" scope="rowgroup">Planetas anões</th>
   <th scope="row">Plutão</th>
   <td>0.0146</td>
   <td>2 370</td>
   <td>2095</td>
   <td>0,7</td>
   <td>153,3</td>
   <td>5906,4</td>
   <td>-225</td>
   <td>5</td>
   <td>Desclassificado como planeta em 2006, mas tal <a href="http://www.usatoday.com/story/tech/2014/10/02/pluto-planet-solar-system/16578959/">ainda é controverso</a>.</td>
  </tr>
 </tbody>
</table>

<p>Quando executada corretamente, até cegos podem interpretar dados tabulares numa tabela de HTML — uma tabela de HTML bem sucedida deve melhorar a experiência utilizadores com deficiências visuais.</p>

<h3 id="Estilização_da_tabela">Estilização da tabela</h3>

<p>Também pode <a href="https://mdn.github.io/learning-area/html/tables/assessment-finished/planets-data.html">ver este exemplo ao vivo</a> no GitHub! Algo que vai notar é que a tabela parece um pouco mais legível lá — isto acontece porque a tabela em cima tem estilização mínima, ao passo que a versão do GitHub aplica estilos CSS mais significativos.</p>

<p>Não se iluda; para as tabelas serem eficazes na internet, tem que fornecer alguma informação de estilos com <a href="/en-US/docs/Learn/CSS">CSS</a>, assim como uma boa estrutura sólida com HTML. Neste módulo focamo-nos na parte de HTML; para saber mais sobre a parte de CSS deve visitar o nosso artigo sobre <a href="/en-US/docs/Learn/CSS/Styling_boxes/Styling_tables">Estilizar tabelas</a>, quando acabar de ler este.</p>

<p>Não nos vamos focar em CSS neste módulo, mas proporcionamos uma folha de estilos CSS mínima que pode usar para tornar as suas tabelas mais legíveis do que se não tivesse estilo. Pode obter a <a href="https://github.com/mdn/learning-area/blob/master/html/tables/basic/minimal-table.css">folha de estilos aqui</a>, e também pode obter um <a href="https://github.com/mdn/learning-area/blob/master/html/tables/basic/blank-template.html">modelo HTML</a> que aplica a folha de estilos — juntando estes dois documentos terá um bom ponto de partida para fazer experiências com tabelas HTML.</p>

<h3 id="Quando_NÃO_deverá_utilizar_tabelas_HTML">Quando NÃO deverá utilizar tabelas HTML?</h3>

<p>Tabelas em HTML devem ser utilizdas para mostrar dados tabulares — é para isso que foram concebidas. Infelizmente, muitas pessoas costumavam usar tabelas HTML para dispor páginas web: por exemplo, uma linha contém o cabeçalho, uma linha possui o conteúdo, organizado em colunas, outra linha contém o rodapé, etc. Esta prática era comum porque o suporte para CSS nos vários navegadores costumava ser horrível. Hoje em dia, layouts com tabelas são muito menos comuns, mas ainda os poderá encontrar em alguns cantos da internet. Poderá encontrar mais detalhes e um exemplo no artigo <a href="/en-US/docs/Learn/Accessibility/HTML#Page_layouts">Layouts de páginas</a> no nosso <a href="/en-US/docs/Learn/Accessibility">Módulo de Acessibilidade</a>.</p>

<p>Resumidamente, recorrer a tabelas para dispor elementos em vez de <a href="/en-US/docs/Learn/CSS/CSS_layout">Técnicas de layout CSS</a> é má ideia. As razões principais são as seguintes:</p>

<ol>
 <li><strong>Layouts com tabelas reduzem acessibilidade de utilizadores com deficiências visuais</strong>: <a href="/en-US/docs/Learn/Tools_and_testing/Cross_browser_testing/Accessibility#Screenreaders">Os leitores de ecrãs</a>, usados por cegos, interpretam as etiquetas existentes na página HTML e lêem o conteúdo em voz alta. Como tabelas não são uma ferramenta adequada para o layout, e a marcação é mais complexa do que se recorresse a técnicas de layout de CSS, a informação dada pelos leitores de ecrãs será confusa para os seus utilizadores.</li>
 <li><strong>Tabelas produzem "sopa" de etiquetas</strong>: Como já foi referido, layouts com tabelas geralmente envolvem estruturas de marcação mais complexas do que as técnicas de layout próprias. Isto torna o código mais difícil de escrever, manter e depurar.</li>
 <li><strong>Tabelas não são automaticamente responsivas</strong>: Ao usar contentores próprios (como {{htmlelement("header")}}, {{htmlelement("section")}}, {{htmlelement("article")}}, ou {{htmlelement("div")}}), a sua largura por omissão é 100% do elemento pai. Tabelas, pelo contrário, são dimensionadas de acordo com o seu conteúdo, pelo que são necessárias medidas adicionais para conseguir que o estilo do layout funcione eficazmente numa variedade de dispositivos.</li>
</ol>

<h2 id="Aprendizagem_ativa_Criar_a_sua_primeira_tabela">Aprendizagem ativa: Criar a sua primeira tabela</h2>

<p>We've talked table theory enough, so, let's dive into a practical example and build up a simple table.</p>

<ol>
 <li>First of all, make a local copy of <a href="https://github.com/mdn/learning-area/blob/master/html/tables/basic/blank-template.html">blank-template.html</a> and <a href="https://github.com/mdn/learning-area/blob/master/html/tables/basic/minimal-table.css">minimal-table.css</a> in a new directory on your local machine.</li>
 <li>The content of every table is enclosed by these two tags : <strong><code><a href="/en-US/docs/Web/HTML/Element/table">&lt;table&gt;&lt;/table&gt;</a></code></strong>. Add these inside the body of your HTML.</li>
 <li>The smallest container inside a table is a table cell, which is created by a <strong><code><a href="/en-US/docs/Web/HTML/Element/td">&lt;td&gt;</a></code></strong> element ('td' stands for 'table data'). Add the following inside your table tags:
  <pre class="brush: html">&lt;td&gt;Hi, I'm your first cell.&lt;/td&gt;</pre>
 </li>
 <li>If we want a row of four cells, we need to copy these tags three times. Update the contents of your table to look like so:
  <pre class="brush: html">&lt;td&gt;Hi, I'm your first cell.&lt;/td&gt;
&lt;td&gt;I'm your second cell.&lt;/td&gt;
&lt;td&gt;I'm your third cell.&lt;/td&gt;
&lt;td&gt;I'm your fourth cell.&lt;/td&gt;</pre>
 </li>
</ol>

<p>As you will see, the cells are not placed underneath each other, rather they are automatically aligned with other on the same row. Each <code>&lt;td&gt;</code> element creates a single cell and together they make up the first row. Every cell we add makes the row grow longer.</p>

<p>To stop this row from growing and start placing subsequent cells on a second row, we need to use the <strong><code><a href="/en-US/docs/Web/HTML/Element/tr">&lt;tr&gt;</a></code></strong> element ('tr' stands for 'table row'). Let's investigate this now.</p>

<ol>
 <li>Place the four cells you've already created inside <code>&lt;tr&gt;</code> tags, like so:

  <pre class="brush: html">&lt;tr&gt;
  &lt;td&gt;Hi, I'm your first cell.&lt;/td&gt;
  &lt;td&gt;I'm your second cell.&lt;/td&gt;
  &lt;td&gt;I'm your third cell.&lt;/td&gt;
  &lt;td&gt;I'm your fourth cell.&lt;/td&gt;
&lt;/tr&gt;</pre>
 </li>
 <li>Now you've made one row, have a go at making one or two more — each row needs to be wrapped in an additional <code>&lt;tr&gt;</code> element, with each cell contained in a <code>&lt;td&gt;</code>.</li>
</ol>

<p>This should result in a table that looks something like the following:</p>

<table>
 <tbody>
  <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>
   <td>Second row, first cell.</td>
   <td>Cell 2.</td>
   <td>Cell 3.</td>
   <td>Cell 4.</td>
  </tr>
 </tbody>
</table>

<div class="note">
<p><strong>Nota</strong>:também pode encontrar isto no GitHub, como <em><a href="https://github.com/mdn/learning-area/blob/master/html/tables/basic/simple-table.html">simple-table.html</a></em> (<a href="http://mdn.github.io/learning-area/html/tables/basic/simple-table.html">veja-a em execução</a>).</p>
</div>

<h2 id="Adicionar_cabeçalhos_com_elementos_&lt;th>">Adicionar cabeçalhos com elementos &lt;th&gt;</h2>

<p>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:</p>

<pre class="brush: html">&lt;table&gt;
  &lt;tr&gt;
    &lt;td&gt;&amp;nbsp;&lt;/td&gt;
    &lt;td&gt;Knocky&lt;/td&gt;
    &lt;td&gt;Flor&lt;/td&gt;
    &lt;td&gt;Ella&lt;/td&gt;
    &lt;td&gt;Juan&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Breed&lt;/td&gt;
    &lt;td&gt;Jack Russell&lt;/td&gt;
    &lt;td&gt;Poodle&lt;/td&gt;
    &lt;td&gt;Streetdog&lt;/td&gt;
    &lt;td&gt;Cocker Spaniel&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Age&lt;/td&gt;
    &lt;td&gt;16&lt;/td&gt;
    &lt;td&gt;9&lt;/td&gt;
    &lt;td&gt;10&lt;/td&gt;
    &lt;td&gt;5&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Owner&lt;/td&gt;
    &lt;td&gt;Mother-in-law&lt;/td&gt;
    &lt;td&gt;Me&lt;/td&gt;
    &lt;td&gt;Me&lt;/td&gt;
    &lt;td&gt;Sister-in-law&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Eating Habits&lt;/td&gt;
    &lt;td&gt;Eats everyone's leftovers&lt;/td&gt;
    &lt;td&gt;Nibbles at food&lt;/td&gt;
    &lt;td&gt;Hearty eater&lt;/td&gt;
    &lt;td&gt;Will eat till he explodes&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;</pre>

<p>Em baixo, tem a tabela renderizada:</p>

<table>
 <tbody>
  <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>
 </tbody>
</table>

<p>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.</p>

<h3 id="Aprendizagem_ativa_cabeçalhos_de_tabela">Aprendizagem ativa: cabeçalhos de tabela</h3>

<p>Let's have a go at improving this table.</p>

<ol>
 <li>First, make a local copy of our <a href="https://github.com/mdn/learning-area/blob/master/html/tables/basic/dogs-table.html">dogs-table.html</a> and <a href="https://github.com/mdn/learning-area/blob/master/html/tables/basic/minimal-table.css">minimal-table.css</a> files in a new directory on your local machine. The HTML contains the same Dogs example as you saw above.</li>
 <li>To recognize the table headers as headers, both visually and semantically, you can use the <strong><code><a href="/en-US/docs/Web/HTML/Element/th">&lt;th&gt;</a></code></strong> element ('th' stand for 'table header'). This works in exactly the same way as a <code>&lt;td&gt;</code>, except that it denotes a header, not a normal cell. Go into your HTML, and change all the <code>&lt;td&gt;</code> elements surrounding the table headers into <code>&lt;th&gt;</code> elements.</li>
 <li>Save your HTML and load it in a browser, and you should see that the headers now look like headers.</li>
</ol>

<div class="note">
<p><strong>Nota</strong>: You can find our finished example at <a href="https://github.com/mdn/learning-area/blob/master/html/tables/basic/dogs-table-fixed.html">dogs-table-fixed.html</a> on GitHub (<a href="http://mdn.github.io/learning-area/html/tables/basic/dogs-table-fixed.html">see it live also</a>).</p>
</div>

<h3 id="Porque_é_que_os_cabeçalhos_são_úteis">Porque é que os cabeçalhos são úteis?</h3>

<p>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.</p>

<div class="note">
<p><strong>Nota</strong>: 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.</p>
</div>

<p>Tables headers also have an added benefit — along with the <code>scope</code> 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.</p>

<h2 id="Permitir_que_as_células_sejam_distribuídas_por_múltiplas_filas_e_colunas">Permitir que as células sejam distribuídas por múltiplas filas e colunas</h2>

<p>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.</p>

<p>The initial markup looks like this:</p>

<pre class="brush: html">&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;Animals&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;Hippopotamus&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;Horse&lt;/th&gt;
    &lt;td&gt;Mare&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Stallion&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;Crocodile&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;Chicken&lt;/th&gt;
    &lt;td&gt;Cock&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Rooster&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;</pre>

<p>But the output doesn't give us quite what we want:</p>

<table>
 <tbody>
  <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>Cock</td>
  </tr>
  <tr>
   <td>Rooster</td>
  </tr>
 </tbody>
</table>

<p>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 <code>colspan</code> and <code>rowspan</code> 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, <code>colspan="2"</code> makes a cell span two columns.</p>

<p>Let's use <code>colspan</code> and <code>rowspan</code> to improve this table.</p>

<ol>
 <li>First, make a local copy of our <a href="https://github.com/mdn/learning-area/blob/master/html/tables/basic/animals-table.html">animals-table.html</a> and <a href="https://github.com/mdn/learning-area/blob/master/html/tables/basic/minimal-table.css">minimal-table.css</a> files in a new directory on your local machine. The HTML contains the same animals example as you saw above.</li>
 <li>Next, use <code>colspan</code> to make "Animals", "Hippopotamus", and "Crocodile" span across two columns.</li>
 <li>Finally, use <code>rowspan</code> to make "Horse" and "Chicken" span across two rows.</li>
 <li>Save and open your code in a browser to see the improvement.</li>
</ol>

<div class="note">
<p><strong>Note</strong>: You can find our finished example at <a href="https://github.com/mdn/learning-area/blob/master/html/tables/basic/animals-table-fixed.html">animals-table-fixed.html</a> on GitHub (<a href="http://mdn.github.io/learning-area/html/tables/basic/animals-table-fixed.html">see it live also</a>).</p>
</div>

<table id="tabular" style="background-color: white;">
</table>

<h2 id="Proporcionar_estilização_comum_para_colunas">Proporcionar estilização comum para colunas</h2>

<p>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 <strong><code><a href="/en-US/docs/Web/HTML/Element/col">&lt;col&gt;</a></code></strong> and <strong><code><a href="/en-US/docs/Web/HTML/Element/colgroup">&lt;colgroup&gt;</a></code></strong> 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 <em>every</em> <code>&lt;td&gt;</code> or <code>&lt;th&gt;</code> in the column, or use a complex selector such as {{cssxref(":nth-child()")}}.</p>

<p>Take the following simple example:</p>

<pre class="brush: html">&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;Data 1&lt;/th&gt;
    &lt;th style="background-color: yellow"&gt;Data 2&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Calcutta&lt;/td&gt;
    &lt;td style="background-color: yellow"&gt;Orange&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Robots&lt;/td&gt;
    &lt;td style="background-color: yellow"&gt;Jazz&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;</pre>

<p>Which gives us the following result:</p>

<table>
 <tbody>
  <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>
 </tbody>
</table>

<p>This isn't ideal, as we have to repeat the styling information across all three cells in the column (we'd probably have a <code>class</code> 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 <code>&lt;col&gt;</code> element. <code>&lt;col&gt;</code> elements are  specified inside a <code>&lt;colgroup&gt;</code> container just below the opening <code>&lt;table&gt;</code> tag. We could create the same effect as we see above by specifying our table as follows:</p>

<pre class="brush: html">&lt;table&gt;
  &lt;colgroup&gt;
    &lt;col&gt;
    &lt;col style="background-color: yellow"&gt;
  &lt;/colgroup&gt;
  &lt;tr&gt;
    &lt;th&gt;Data 1&lt;/th&gt;
    &lt;th&gt;Data 2&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Calcutta&lt;/td&gt;
    &lt;td&gt;Orange&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Robots&lt;/td&gt;
    &lt;td&gt;Jazz&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;</pre>

<p>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 <code>&lt;col&gt;</code> element — if we didn't, the styling would just be applied to the first column.</p>

<p>If we wanted to apply the styling information to both columns, we could just include one <code>&lt;col&gt;</code> element with a span attribute on it, like this:</p>

<pre class="brush: html">&lt;colgroup&gt;
  &lt;col style="background-color: yellow" span="2"&gt;
&lt;/colgroup&gt;</pre>

<p>Just like <code>colspan</code> and <code>rowspan</code>, <code>span</code> takes a unitless number value that specifies the number of columns you want the styling to apply to.</p>

<h3 id="Aprendizagem_ativa_colgroup_e_col">Aprendizagem ativa: <em>colgroup </em>e<em> </em><em>col</em></h3>

<p>Now it's time to have a go yourself.</p>

<p>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.</p>

<p>{{EmbedGHLiveSample("learning-area/html/tables/basic/timetable-fixed.html", '100%', 320)}}</p>

<p>Recreate the table by following the steps below.</p>

<ol>
 <li>First, make a local copy of our <a href="https://github.com/mdn/learning-area/blob/master/html/tables/basic/timetable.html">timetable.html</a> file in a new directory on your local machine. The HTML contains the same table you saw above, minus the column styling information.</li>
 <li>Add a <code>&lt;colgroup&gt;</code> element at the top of the table, just underneath the <code>&lt;table&gt;</code> tag, in which you can add your <code>&lt;col&gt;</code> elements (see the remaining steps below).</li>
 <li>The first two columns need to be left unstyled.</li>
 <li>Add a background color to the third column. The value for your <code>style</code> attribute is <code>background-color:#97DB9A;</code></li>
 <li>Set a separate width on the fourth column. The value for your <code>style</code> attribute is <code>width: 42px;</code></li>
 <li>Add a background color to the fifth column. The value for your <code>style</code> attribute is <code>background-color: #97DB9A;</code></li>
 <li>Add a different background color plus a border to the sixth column, to signify that this is a special day and she's teaching a new class. The values for your <code>style</code> attribute are <code>background-color:#DCC48E; border:4px solid #C1437A;</code></li>
 <li>The last two days are free days, so just set them to no background color but a set width; the value for the <code>style</code> attribute is <code>width: 42px;</code></li>
</ol>

<p>See how you get on with the example. If you get stuck, or want to check your work, you can find our version on GitHub as <a href="https://github.com/mdn/learning-area/blob/master/html/tables/basic/timetable-fixed.html">timetable-fixed.html</a> (<a href="http://mdn.github.io/learning-area/html/tables/basic/timetable-fixed.html">see it live also</a>).</p>

<h2 id="Resumo">Resumo</h2>

<p>That just about wraps up the basics of HTML Tables. In the next article we will look at some slightly more advanced table features, and start to think how accessible they are for visually impaired people.</p>

<div>{{NextMenu("Learn/HTML/Tables/Advanced", "Learn/HTML/Tables")}}</div>

<div>
<h2 id="Neste_módulo">Neste módulo</h2>

<ul>
 <li><a href="/pt-PT/docs/Learn/HTML/Tables/Basicos">HTML table basics</a></li>
 <li><a href="/pt-PT/docs/Learn/HTML/Tables/Avancada">HTML - funcionalidades avançadas de tabela e acessibilidade</a></li>
 <li><a href="/pt-PT/docs/Learn/HTML/Tables/Avaliacao_Estruturar_os_dados_dos_planetas">Estruturar dados dos planetas</a></li>
</ul>
</div>

<div id="SL_balloon_obj" style="display: block;">
<div class="SL_ImTranslatorLogo" id="SL_button" style="background: rgba(0, 0, 0, 0)  repeat scroll 0% 0%; opacity: 1; display: none; left: -8px; top: -25px;"> </div>

<div id="SL_shadow_translation_result2" style="display: none;"> </div>

<div id="SL_shadow_translator" style="display: none;">
<div id="SL_planshet">
<div id="SL_arrow_up" style="background: rgba(0, 0, 0, 0)  repeat scroll 0% 0%;"> </div>

<div id="SL_Bproviders">
<div class="SL_BL_LABLE_ON" id="SL_P0" title="Google">G</div>

<div class="SL_BL_LABLE_ON" id="SL_P1" title="Microsoft">M</div>

<div class="SL_BL_LABLE_ON" id="SL_P2" title="Translator">T</div>
</div>

<div id="SL_alert_bbl" style="display: none;">
<div id="SLHKclose" style="background: rgba(0, 0, 0, 0)  repeat scroll 0% 0%;"> </div>

<div id="SL_alert_cont"> </div>
</div>

<div id="SL_TB">
<table id="SL_tables">
 <tbody>
  <tr>
   <td class="SL_td"><input></td>
   <td class="SL_td"><select><option value="auto">Detectar idioma</option><option value="af">Africâner</option><option value="sq">Albanês</option><option value="de">Alemão</option><option value="ar">Arabe</option><option value="hy">Armênio</option><option value="az">Azerbaijano</option><option value="eu">Basco</option><option value="bn">Bengali</option><option value="be">Bielo-russo</option><option value="my">Birmanês</option><option value="bs">Bósnio</option><option value="bg">Búlgaro</option><option value="ca">Catalão</option><option value="kk">Cazaque</option><option value="ceb">Cebuano</option><option value="ny">Chichewa</option><option value="zh-CN">Chinês (Simp)</option><option value="zh-TW">Chinês (Trad)</option><option value="si">Cingalês</option><option value="ko">Coreano</option><option value="ht">Crioulo haitiano</option><option value="hr">Croata</option><option value="da">Dinamarquês</option><option value="sk">Eslovaco</option><option value="sl">Esloveno</option><option value="es">Espanhol</option><option value="eo">Esperanto</option><option value="et">Estoniano</option><option value="fi">Finlandês</option><option value="fr">Francês</option><option value="gl">Galego</option><option value="cy">Galês</option><option value="ka">Georgiano</option><option value="el">Grego</option><option value="gu">Gujarati</option><option value="ha">Hauça</option><option value="iw">Hebraico</option><option value="hi">Hindi</option><option value="hmn">Hmong</option><option value="nl">Holandês</option><option value="hu">Húngaro</option><option value="ig">Igbo</option><option value="id">Indonésio</option><option value="en">Inglês</option><option value="yo">Ioruba</option><option value="ga">Irlandês</option><option value="is">Islandês</option><option value="it">Italiano</option><option value="ja">Japonês</option><option value="jw">Javanês</option><option value="kn">Kannada</option><option value="km">Khmer</option><option value="lo">Laosiano</option><option value="la">Latim</option><option value="lv">Letão</option><option value="lt">Lituano</option><option value="mk">Macedônico</option><option value="ml">Malaiala</option><option value="ms">Malaio</option><option value="mg">Malgaxe</option><option value="mt">Maltês</option><option value="mi">Maori</option><option value="mr">Marathi</option><option value="mn">Mongol</option><option value="ne">Nepalês</option><option value="no">Norueguês</option><option value="fa">Persa</option><option value="pl">Polonês</option><option value="pt">Português</option><option value="pa">Punjabi</option><option value="ro">Romeno</option><option value="ru">Russo</option><option value="sr">Sérvio</option><option value="st">Sesotho</option><option value="so">Somália</option><option value="sw">Suaíli</option><option value="su">Sudanês</option><option value="sv">Sueco</option><option value="tg">Tadjique</option><option value="tl">Tagalo</option><option value="th">Tailandês</option><option value="ta">Tâmil</option><option value="cs">Tcheco</option><option value="te">Telugo</option><option value="tr">Turco</option><option value="uk">Ucraniano</option><option value="ur">Urdu</option><option value="uz">Uzbeque</option><option value="vi">Vietnamita</option><option value="yi">Yiddish</option><option value="zu">Zulu</option></select></td>
   <td class="SL_td">
    <div id="SL_switch_b" style="background: rgba(0, 0, 0, 0)  repeat scroll 0% 0%;" title="Alternar Idiomas"> </div>
   </td>
   <td class="SL_td"><select><option value="af">Africâner</option><option value="sq">Albanês</option><option value="de">Alemão</option><option value="ar">Arabe</option><option value="hy">Armênio</option><option value="az">Azerbaijano</option><option value="eu">Basco</option><option value="bn">Bengali</option><option value="be">Bielo-russo</option><option value="my">Birmanês</option><option value="bs">Bósnio</option><option value="bg">Búlgaro</option><option value="ca">Catalão</option><option value="kk">Cazaque</option><option value="ceb">Cebuano</option><option value="ny">Chichewa</option><option value="zh-CN">Chinês (Simp)</option><option value="zh-TW">Chinês (Trad)</option><option value="si">Cingalês</option><option value="ko">Coreano</option><option value="ht">Crioulo haitiano</option><option value="hr">Croata</option><option value="da">Dinamarquês</option><option value="sk">Eslovaco</option><option value="sl">Esloveno</option><option value="es">Espanhol</option><option value="eo">Esperanto</option><option value="et">Estoniano</option><option value="fi">Finlandês</option><option value="fr">Francês</option><option value="gl">Galego</option><option value="cy">Galês</option><option value="ka">Georgiano</option><option value="el">Grego</option><option value="gu">Gujarati</option><option value="ha">Hauça</option><option value="iw">Hebraico</option><option value="hi">Hindi</option><option value="hmn">Hmong</option><option value="nl">Holandês</option><option value="hu">Húngaro</option><option value="ig">Igbo</option><option value="id">Indonésio</option><option selected value="en">Inglês</option><option value="yo">Ioruba</option><option value="ga">Irlandês</option><option value="is">Islandês</option><option value="it">Italiano</option><option value="ja">Japonês</option><option value="jw">Javanês</option><option value="kn">Kannada</option><option value="km">Khmer</option><option value="lo">Laosiano</option><option value="la">Latim</option><option value="lv">Letão</option><option value="lt">Lituano</option><option value="mk">Macedônico</option><option value="ml">Malaiala</option><option value="ms">Malaio</option><option value="mg">Malgaxe</option><option value="mt">Maltês</option><option value="mi">Maori</option><option value="mr">Marathi</option><option value="mn">Mongol</option><option value="ne">Nepalês</option><option value="no">Norueguês</option><option value="fa">Persa</option><option value="pl">Polonês</option><option value="pt">Português</option><option value="pa">Punjabi</option><option value="ro">Romeno</option><option value="ru">Russo</option><option value="sr">Sérvio</option><option value="st">Sesotho</option><option value="so">Somália</option><option value="sw">Suaíli</option><option value="su">Sudanês</option><option value="sv">Sueco</option><option value="tg">Tadjique</option><option value="tl">Tagalo</option><option value="th">Tailandês</option><option value="ta">Tâmil</option><option value="cs">Tcheco</option><option value="te">Telugo</option><option value="tr">Turco</option><option value="uk">Ucraniano</option><option value="ur">Urdu</option><option value="uz">Uzbeque</option><option value="vi">Vietnamita</option><option value="yi">Yiddish</option><option value="zu">Zulu</option></select></td>
   <td class="SL_td">
    <div id="SL_TTS_voice" style="background: rgba(0, 0, 0, 0)  repeat scroll 0% 0%;" title="Ouça"> </div>
   </td>
   <td class="SL_td">
    <div class="SL_copy" id="SL_copy" style="background: rgba(0, 0, 0, 0)  repeat scroll 0% 0%;" title="Copiar"> </div>
   </td>
   <td class="SL_td">
    <div id="SL_bbl_font_patch"> </div>

    <div class="SL_bbl_font" id="SL_bbl_font" style="background: rgba(0, 0, 0, 0)  repeat scroll 0% 0%;" title="Tamanho da fonte"> </div>
   </td>
   <td class="SL_td">
    <div id="SL_bbl_help" style="background: rgba(0, 0, 0, 0)  repeat scroll 0% 0%;" title="Ajuda"> </div>
   </td>
   <td class="SL_td">
    <div class="SL_pin_off" id="SL_pin" style="background: rgba(0, 0, 0, 0)  repeat scroll 0% 0%;" title="Fixar a janela de pop-up"> </div>
   </td>
  </tr>
 </tbody>
</table>
</div>
</div>

<div id="SL_shadow_translation_result"> </div>

<div class="SL_loading" id="SL_loading" style="background: rgba(0, 0, 0, 0)  repeat scroll 0% 0%;"> </div>

<div id="SL_player2"> </div>

<div id="SL_alert100">A função de fala é limitada a 200 caracteres</div>

<div id="SL_Balloon_options" style="background: rgb(255, 255, 255)  repeat scroll 0% 0%;">
<div id="SL_arrow_down" style="background: rgba(0, 0, 0, 0)  repeat scroll 0% 0%;"> </div>

<table id="SL_tbl_opt" style="width: 100%;">
 <tbody>
  <tr>
   <td><input></td>
   <td>
    <div id="SL_BBL_IMG" style="background: rgba(0, 0, 0, 0)  repeat scroll 0% 0%;" title="Mostrar o botão do ImTranslator 3 segundos"> </div>
   </td>
   <td><a class="SL_options" title="Mostrar opções">Opções</a> : <a class="SL_options" title="Histórico de tradução">Histórico</a> : <a class="SL_options" title="Comentários">Comentários</a> : <a class="SL_options" href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&amp;hosted_button_id=GD9D8CPW8HFA2" title="Faça sua contribuição">Donate</a></td>
   <td><span id="SL_Balloon_Close" title="Encerrar">Encerrar</span></td>
  </tr>
 </tbody>
</table>
</div>
</div>
</div>