Lesson 09 · 14 steps
table.html turns the site's numbers into tables: a squad list, a match score with merged cells, a scrolling results list. It also builds a page layout out of a table, the way it was done in 1998 — and shows why that one is a mistake.
<!-- 2) проста таблиця із заголовком і малюнком у комірці -->
<h2> Склад Natus Vincere </h2>
<table border="2" cellpadding="5" cellspacing="0">
<caption> Турнірні результати сезону </caption>
<tr>
<th> № </th> <th> Гравець </th> <th> Роль </th> <th> Рейтинг </th>
</tr>
<tr>
<td> 1 <img src="images/6.jpg" width="40"> </td>
<td> s1mple </td> <td> AWPer </td> <td> 1.30 </td>
</tr>
<tr>
<td> 2 </td> <td> electroNic </td> <td> entry fragger </td> <td> 1.15 </td>
</tr>
<tr>
<td> 3 </td> <td> b1t </td> <td> rifler </td> <td> 1.12 </td>
</tr>
</table>
Three tags do the work: table wraps it, tr is a row, td a cell — and th a heading cell, which browsers draw bold and centred. caption is the table's own title.
A cell can hold anything a page can hold, pictures included.
border, cellpadding and cellspacing are attributes from the first version of HTML. CSS does all three now: border, padding, and border-collapse for the gap between cells.
<!-- 4) об'єднані комірки -->
<h2> Об'єднані комірки </h2>
<table border="4" bordercolor="#f0a500" cellspacing="0" cellpadding="5">
<tr align="center"><th colspan="3"> Підсумок матчу на Mirage </th></tr>
<tr align="center">
<td rowspan="2"> Natus Vincere </td>
<td> 1-ша половина </td> <td> 8 </td>
</tr>
<tr align="center"><td> 2-га половина </td> <td> 5 </td></tr>
<tr align="center">
<td rowspan="2"> Team Spirit </td>
<td> 1-ша половина </td> <td> 4 </td>
</tr>
<tr align="center"><td> 2-га половина </td> <td> 7 </td></tr>
</table>
colspan stretches a cell across columns, rowspan down rows. The catch is counting: a row that lends a cell to the row below must have one cell fewer, or the table grows a column nobody asked for.
<!-- 5) макет сторінки, побудований таблицею -->
<h2> Макет сторінки на таблиці </h2>
<table border="1" width="420">
<tr id="header"><td colspan="2"> header (шапка сайту) </td></tr>
<tr id="box">
<td id="column1"> column1 (меню) </td>
<td id="column2"> column2 (вміст) </td>
</tr>
<tr id="footer"><td colspan="2"> footer (підвал) </td></tr>
</table>
This is how pages were built before CSS could lay anything out: a table with a header row, a row of two columns, and a footer row.
It is also the clearest example of markup lying about content. A screen reader announces «table with three rows» and starts reading cells, when there is no table of anything — only a layout. Today that is header, nav, main, footer with grid, and tables are left for actual data.
<style type="text/css">
/* 6) таблиця з прокруткою */
table.scroll {table-layout:fixed;border-collapse:collapse;border:solid 2px #f0a500;width:420px}
table.scroll td {border-right:solid 2px #f0a500;border-bottom:solid 1px #888}
table.scroll th {border-right:solid 2px #f0a500;border-bottom:solid 3px #f0a500;width:80px}
table.scroll tbody {height:120px;overflow:auto;display:block}
table.scroll thead tr {display:block}
<!-- 6) таблиця з прокруткою -->
<h2> Таблиця з прокруткою </h2>
<table class="scroll">
<thead>
<tr><th> Карта </th><th> Раундів </th><th> Рахунок </th><th> Результат </th></tr>
</thead>
<tbody>
<tr><td> Mirage </td><td> 24 </td><td> 13:11 </td><td> перемога </td></tr>
<tr><td> Inferno </td><td> 19 </td><td> 6:13 </td><td> поразка </td></tr>
<tr><td> Nuke </td><td> 30 </td><td> 16:14 </td><td> перемога </td></tr>
<tr><td> Ancient </td><td> 21 </td><td> 13:8 </td><td> перемога </td></tr>
<tr><td> Anubis </td><td> 26 </td><td> 11:13 </td><td> поразка </td></tr>
<tr><td> Overpass </td><td> 22 </td><td> 13:9 </td><td> перемога </td></tr>
</tbody>
</table>
The trick: tbody is made a block with a fixed height and overflow:auto, so the body scrolls while the heading row stays put.
It works, and it is fragile — the columns in the head and the body are measured separately, so their widths have to be pinned by hand or they drift apart.
Modern CSS has position:sticky for exactly this: the heading row stays visible while the page scrolls, and nothing has to be turned into a block.
/* 7) оформлення колонок тегом col */
table.cols {border-collapse:collapse;border:solid 2px #8bd450}
table.cols td, table.cols th {border:solid 1px #888;padding:4px}
.fon {background:#2b2b2b}
.f5 {background:#3a2a1a}
<!-- 7) оформлення колонок тегом col -->
<h2> Оформлення колонок </h2>
<table class="cols">
<col class="fon"><col class="f5"><col class="fon"><col class="f5">
<tr>
<th> № </th><th> Команда </th><th> Країна </th><th> Очки </th>
</tr>
<tr>
<td> 1 </td><td> Natus Vincere </td><td> Україна </td><td> 1000 </td>
</tr>
<tr>
<td> 2 </td><td> B8 </td><td> Україна </td><td> 620 </td>
</tr>
</table>
col describes a column before any row exists. It is the only way to reach a whole column at once — cells belong to rows, so a column is not a thing you can select.
/* 12) реагування комірок на наведення мишки */
table.hovertable {font-family:verdana,arial,sans-serif;font-size:13px;color:#e6e6e6;border-collapse:collapse}
table.hovertable th {background:#3a2a1a;border:solid 2px #f0a500;padding:8px}
table.hovertable td {border:solid 1px #888;padding:8px}
table.hovertable tr:hover {background:#f0a500;color:#141414}
<!-- 12) реагування комірок на наведення мишки --> <h2> Наведіть мишку на рядок </h2> <table class="hovertable"> <tr><th> Зброя </th><th> Сторона </th><th> Ціна </th></tr> <tr><td> AK-47 </td><td> терористи </td><td> 2700 </td></tr> <tr><td> M4A1-S </td><td> контртерористи </td><td> 2900 </td></tr> <tr><td> AWP </td><td> обидві </td><td> 4750 </td></tr> <tr><td> Desert Eagle </td><td> обидві </td><td> 700 </td></tr> </table>
tr:hover highlights the row under the pointer — one line of CSS that makes a long table far easier to read across.
/* 14) таблиця, оформлена класами */
.myTable {width:420px;background:#1b1b1b;border-collapse:collapse}
.myTable th {background:#3a2a1a;color:#f0a500;width:50%}
.myTable td, .myTable th {padding:5px;border:1px solid #f0a500}
</style>
<!-- 14) таблиця, оформлена класами --> <h2> Таблиця, оформлена класами </h2> <table class="myTable"> <tr><th> Турнір </th><th> Рік </th></tr> <tr><td> PGL Major Stockholm </td><td> 2021 </td></tr> <tr><td> IEM Katowice </td><td> щороку </td></tr> </table>
Nothing new in the table itself — the difference is that every rule lives in one class, and the markup carries no formatting at all. That is the direction the whole practicum has been moving in since lesson 2.