HTML tables, colour schemes, images and links

table, tr, th, td, caption and border; finding the element where a row and a column meet and counting bold cells; colspan and rowspan; the body attributes bgcolor, text, link, vlink, alink and #RRGGBB colours; the img (src, alt, width, height, align, border) and a (href, #, mailto, target) tags.

  • Grade 10
  • 24 min
  • 12 questions
  • Lesson 45 of 59

In this lesson you will learn

  • From a code fragment, find how a table looks, the element where a given row and column meet and the number of bold cells
  • Read a table with cells merged by colspan and rowspan
  • Set a page’s colour scheme with body attributes and read a #RRGGBB code
  • Place images and create different kinds of links

A school website has a timetable, term marks, a photo of the school and links to other pages. All of them are built with the tags of this lesson: tables, colours, images and links.

Every entrance exam paper has one task on an HTML table: find how the table will look, a given column, the element where a row and a column meet or the number of cells in bold. Tags and formatting are explained in the lesson “Web programming: building a site, HTML tags and lists”.

Building a table: table, tr, th, td

TagMeaningHow it looks
<table>tablewithout border no frame is shown
<tr>row (table row)its cells line up in one row
<th>header cell (table header)bold and centred
<td>data cell (table data)plain text, aligned left
<caption>table captionabove the table, centred

Attributes of <table>: border — frame thickness (in pixels), width — width (pixels or %), cellpadding — space inside a cell, cellspacing — space between cells, align — alignment, bgcolor — background colour (also works on <tr> and <td>).

  • The number of rows equals the number of <tr> tags; the header row is a row too.
  • The number of columns is the number of cells in one row (counting merges).
  • In DİM tasks rows are counted from the top, including the header row, and columns from the left.
  • An empty <td></td> is still a cell: with a border it shows as an empty cell.
HTML
<table border="1" cellpadding="6">
  <caption>Class timetable</caption>
  <tr><th>Time</th><th>Monday</th><th>Tuesday</th></tr>
  <tr><td>09:00</td><td>Math</td><td rowspan="2">Chemistry lab</td></tr>
  <tr><td>10:00</td><td>History</td></tr>
  <tr><td>11:00</td><td colspan="2">Sports day</td></tr>
  <tr><td>12:00</td><td><b>Test</b></td><td></td></tr>
</table>
Preview
Header cells are bold and centred, “Chemistry lab” spans two rows, “Sports day” spans two columns, and the last cell is empty. Change border or add a new <tr>.

Example 1. Where a row and a column meet

Problem

According to the HTML code fragment, which element will be where the third row and the second column of the table meet?
<table border="1">
<tr><th>Day</th><th>Lesson 1</th><th>Lesson 2</th></tr>
<tr><td>Monday</td><td>Math</td><td>Physics</td></tr>
<tr><td>Tuesday</td><td>History</td><td>Chemistry</td></tr>
<tr><td>Wednesday</td><td>Informatics</td><td>Biology</td></tr>
</table>
A) Math B) Chemistry C) History D) Informatics E) Tuesday

Show solutionHide solution

Solution

1) Row 1 is the header row (Day, Lesson 1, Lesson 2), row 2 is Monday, row 3 is Tuesday.
2) The cells of row 3: Tuesday, History, Chemistry. Column 2 holds “History”.
Correct answer: C) History. (If you skip the header row, you wrongly get Informatics.)

Example 2. How many cells are bold?

Problem

According to the HTML code fragment, in how many cells of the table will the text be shown in bold?
<table border="1">
<tr><th>Subject</th><th>Term I</th><th>Term II</th></tr>
<tr><td>Math</td><td><b>5</b></td><td><em>4</em></td></tr>
<tr><td>Physics</td><td><strong><u>5</u></strong></td><td><i>3</i></td></tr>
<tr><td><tt>Chemistry</tt></td><td>4</td><td><b><i>5</i></b></td></tr>
</table>
A) 3 B) 4 C) 6 D) 5 E) 7

Show solutionHide solution

Solution

1) <th> cells are always bold: Subject, Term I, Term II — 3 cells.
2) Among the <td> cells: <b>5</b>, <strong><u>5</u></strong>, <b><i>5</i></b> — 3 cells. <em>, <i>, <tt> and plain text are not bold.
3) 3 + 3 = 6.
Correct answer: C) 6. (Forgetting the <th> cells gives 3.)

Sign up free to keep reading

The rest of the lesson, the quiz and your notes open with an Educora account. Signing up takes a minute, and your progress is saved on all your devices.

Merging cells: colspan and rowspan

colspan="k" stretches a cell across k columns — that row has k − 1 fewer cells written. rowspan="k" stretches a cell down k rows — the next k − 1 rows have no cell written for that column, and their other cells shift to the right.

Example 3. Merged cells

Problem

<table border="1">
<tr><td rowspan="2">A</td><td>B</td><td>C</td></tr>
<tr><td>D</td><td>E</td></tr>
<tr><td colspan="2">F</td><td>G</td></tr>
</table>
1) How many columns does the table have? 2) Which element is where row 2 and column 3 meet? 3) How many cells does F take up?

Show solutionHide solution

Solution

1) Row 1 has A, B, C — 3 columns.
2) A spans two rows (rowspan="2"), so column 1 of row 2 is taken by A; D moves to column 2 and E to column 3. Answer: E.
3) F with colspan="2" covers columns 1 and 2 — 2 cells; G is in column 3.

The colour scheme of a web page

The main colours of a page are set with attributes of the <body> tag, and the colour of single words with <font color="…">. A colour is written as an English name (red, blue) or as a #RRGGBB code: RR, GG, BB are the amounts of red, green and blue light, each from 00 to FF in hexadecimal (0–255).

AttributeWhat it colours
bgcolorthe page background
textordinary text
linklinks not yet visited
vlinklinks already visited — visited
alinka link at the moment it is clicked — active
N = 256 · 256 · 256 = 2²⁴ = 16,777,216

Where

#RRGGBB
colour code: three hexadecimal pairs — red, green, blue
256
number of values of one pair: 00…FF = 0…255
N
number of colours that can be written

Every colour is coded with 3 bytes = 24 bits — the same as True Color in raster graphics.

NameCodeR, G, B
black#0000000, 0, 0
white#FFFFFF255, 255, 255
red#FF0000255, 0, 0
lime — bright green#00FF000, 255, 0
green#0080000, 128, 0
blue#0000FF0, 0, 255
yellow#FFFF00255, 255, 0
gray#808080128, 128, 128
purple#800080128, 0, 128

Example 4. Reading a colour scheme

Problem

A page’s code contains <body bgcolor="#FFFF00" text="#0000FF" link="#FF0000" vlink="#008000" alink="#800080">. 1) What colour are the background, ordinary text, unvisited and visited links? 2) What colour does <font color="#FF8000"> give?

Show solutionHide solution

Solution

1) bgcolor #FFFF00 — red + green = a yellow background; text #0000FF — blue text; link #FF0000 — red; vlink #008000 — green; at the moment of clicking (alink) #800080 — purple.
2) FF = 15 · 16 + 15 = 255, 80 = 8 · 16 = 128, 00 = 0: full red + half green — orange.

Interactive

Loading simulation…
On a screen, colours are made by mixing light (RGB). Change the amounts of red, green and blue and watch the HEX code.
HTML
<body bgcolor="#FFFFE0" text="#003366" link="#CC0000" vlink="#800080" alink="#FF8000">
  <h2>Colour scheme</h2>
  <p>Ordinary text is dark blue on a light yellow background.</p>
  <p><a href="#top">This link</a> is red before you open it.</p>
  <p><font color="green">green</font>,
     <font color="#FF8000">orange #FF8000</font>,
     <font color="#808080">grey #808080</font></p>
</body>
Preview
Change the bgcolor, text and link values and look at the result.

Images and links

An image is placed with the single tag <img>. Its main attributes: src — the address of the graphic file (JPEG, PNG, GIF, SVG, WebP); alt — the text shown when the image cannot load and read out by screen readers; width, height — size in pixels or percent; align="left" / "right" — the image moves to the side and text wraps around it (top, middle, bottom set its vertical place in a line); border — frame thickness (pixels).

h₂ = h₁ · w₂ / w₁h₂ = h₁ · w₂ / w₁

Where

w₁, h₁
the image’s own width and height (pixels)
w₂
the width written in the width attribute
h₂
the height the browser shows

If only width (or only height) is given, the browser keeps the proportions.

Example 5. The size of an image

Problem

1) A 1200 × 800 pixel image is placed with <img src="tower.jpg" width="300">. At what height will the browser show it? 2) A 640 × 480 pixel image has height="120". What will its width be?

Show solutionHide solution

Solution

1) h₂ = 800 · 300 / 1200 = 200 pixels (the width became 4 times smaller, so did the height).
2) This time the height is given: w₂ = 640 · 120 / 480 = 160 pixels.

A link (hyperlink) is text or an image that leads to another page, a file or another place on the page: <a href="address">text</a>. href can hold an absolute address (https://… — another site), a relative one (contact.html — a page of your own site), #name (the place with id="name" on the same page) or mailto: (the mail program). target="_blank" opens the link in a new window (tab). Put an <img> inside <a> and the picture becomes a link.

HTML
<p id="top">
  <img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 100'%3E%3Crect width='160' height='60' fill='%23fdba74'/%3E%3Ccircle cx='80' cy='60' r='22' fill='%23f97316'/%3E%3Crect y='60' width='160' height='40' fill='%230ea5e9'/%3E%3C/svg%3E"
       width="160" align="right" border="2" alt="Sunset over the sea">
  The picture is on the right (align="right"), with a 2-pixel frame,
  and this text wraps around it on the left.
</p>
<p><a href="https://example.com" target="_blank">Opens in a new tab</a></p>
<p><a href="mailto:info@example.com">Write an e-mail</a></p>
<p><a href="#top">Back to the top of the page</a></p>
Preview
Set align to left and border to 0, change width — the height changes in proportion.

Example 6. Kinds of links

Problem

1) <a href="https://www.example.com">Site</a>
2) <a href="contact.html">Contact</a>
3) <a href="#prices">Prices</a>
4) <a href="mailto:info@example.com">Write to us</a>
5) <a href="book.pdf" target="_blank">Book</a>
Which link: a) leads to another site; b) jumps within the same page; c) opens the mail program; d) opens in a new tab; e) leads to another page of the same site?

Show solutionHide solution

Solution

a) 1 — an absolute address starting with https://. b) 3 — #prices jumps to the place with id="prices". c) 4 — mailto:. d) 5 — target="_blank". e) 2 — the relative address contact.html.

Remember

  • Table: <table> → <tr> rows → <th> (bold, centred) and <td> (plain) cells; border draws the frame, <caption> gives the title.
  • Rows are counted from the top including the header row, columns from the left; an empty <td> is still a cell.
  • Bold cells = all <th> + the <td> cells containing <b> or <strong>.
  • colspan stretches a cell to the right, rowspan downwards; cells of the rows below go to the right of the merged cell.
  • <body>: bgcolor — background, text — text, link — unvisited, vlink — visited, alink — clicked link; #RRGGBB writes 2²⁴ colours.
  • <img src alt width height align border> is a single tag; with only a width the proportions stay; <a href>: https://, relative address, #id, mailto:, target="_blank".
Spotted a mistake or something inaccurate in this lesson?

Check yourself

12 questions. Every correct answer earns XP.

Question 1 / 12