</>HTML & CSS
BeginnerLesson 4 of 15

Tables & Definition Lists – HTML & CSS

Mark up tabular data correctly and style it for readability.

Tables are only meant for tabular data, not for page layout (Flexbox and Grid handle that). A good table has <caption>, <thead>, <tbody>, <tfoot> and header cells <th scope="col|row">. You merge cells with colspan and rowspan.

<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
table{border-collapse:collapse;width:100%;max-width:420px}
caption{text-align:left;font-weight:700;padding:6px 0}
th,td{border:1px solid #ccc;padding:6px 10px;text-align:left}
thead th{background:#222;color:#fff}
tbody tr:nth-child(even){background:#f3f3f3}
tfoot td{font-weight:700}
td:last-child,th:last-child{text-align:right}
</style>
<table>
  <caption>Order No. 6</caption>
  <thead><tr><th scope="col">Product</th><th scope="col">Qty</th><th scope="col">Price</th></tr></thead>
  <tbody>
    <tr><th scope="row">Laptop</th><td>1</td><td>999 €</td></tr>
    <tr><th scope="row">SQL Book</th><td>3</td><td>90 €</td></tr>
  </tbody>
  <tfoot><tr><td colspan="2">Total</td><td>1089 €</td></tr></tfoot>
</table>

For term-explanation pairs there is the definition list <dl> with <dt> (term) and <dd> (description). It suits glossaries, metadata or property lists (name: value). You create nested lists by placing a <ul>/<ol> inside an <li>.

<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
dl{display:grid;grid-template-columns:max-content 1fr;gap:6px 16px}
dt{font-weight:700}
dd{margin:0}
</style>
<dl>
  <dt>HTML</dt><dd>Structure of a web page</dd>
  <dt>CSS</dt><dd>Styling and layout</dd>
  <dt>DOM</dt><dd>Object tree the browser builds from HTML</dd>
</dl>
<ol>
  <li>Plan
    <ul><li>Goals</li><li>Audience</li></ul>
  </li>
  <li>Build</li>
</ol>
WarningOn narrow screens wide tables overflow. Wrap them in a container with overflow-x: auto so that only the table scrolls and not the whole page.
TipWithout border-collapse: collapse, tables have double borders between the cells. With <th scope> the screen reader knows which column or row a cell belongs to.