Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære HTML Tables | Working with Semantic Elements, Multimedia Elements and Graphics
HTML for Beginners

book
HTML Tables

Tables are made with the <table> tag.

html

index.html

copy
<table></table>

A table is just a concept for a collection of records. So we need to have records to create a table. Records are created with the <tr> (table row) tag. For a record to exist, you have to include data. You add table data with the <td> (table data) tag.

html

index.html

copy
<!DOCTYPE html>
<html>
<head> </head>
<body>
<table>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$200</td>
</tr>
</table>
</body>
</html>

So our table has 2 rows and 2 columns. These columns need headings so everyone can understand what these columns represent.

To create a column heading, you use the <th> (table heading) tag instead of the <td> tag.

html

index.html

copy
<!DOCTYPE html>
<html>
<head> </head>
<body>
<table>
<tr>
<th>Month</th>
<th>Expenditure</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$200</td>
</tr>
</table>
</body>
</html>

You can see the column headings are displayed in bold text. This is an effect of <th> tags.

However, this doesn't feel like a table without lines between table data. We add these lines using the border attribute of the <table> element.

Replace the first <table> with this line:

html
<table border="1">
  • <table border="1"> : It sets the border around the table cells.

  • <table border="0"> : It removes (not set) the border around the table cells.

As explained in a previous chapter, we add CSS styles using the style attribute.

html
<table border = '1' style = 'border-collapse:collapse'>
html

index.html

copy
<!DOCTYPE html>
<html>
<head> </head>
<body>
<table border="1" style="border-collapse: collapse">
<tr>
<th>Month</th>
<th>Expenditure</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$200</td>
</tr>
</table>
</body>
</html>

Colspan and rowspan attributes

2 unique attributes can be used with table cells. You can span table cells to 2 or more columns or rows.

html

index.html

copy
<!DOCTYPE html>
<html>
<head> </head>
<body>
<table border="1" style="border-collapse: collapse">
<tr>
<th>col 1</th>
<th>col 2</th>
<th>col 3</th>
<th>col 4</th>
</tr>
<tr>
<td>col 1 row 1</td>
<td colspan="2">col 2-3 row 1</td>
<td>col 4 row 1</td>
</tr>
<tr>
<td>col 1 row 2</td>
<td rowspan="2">col 2 row 2 -3</td>
<td>col 3 row 2</td>
<td>col 4 row 2</td>
</tr>
<tr>
<td>col 1 row 3</td>
<td colspan="2" rowspan="2">col 3 -4 row 3 -4</td>
</tr>
<tr>
<td>col 1 row 4</td>
<td>col 2 row 4</td>
</tr>
</table>
</body>
</html>

I'd appreciate it if you could carefully notice how we have used colspan and rowspan to increase the space of a cell.

question mark

Which tag is used to create data for table headings?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 4

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

We use cookies to make your experience better!
some-alt