Be able to create classes including background properties (colour, images), font properties, table, table row and table data properties (size, background colour, horizontal and vertical alignment, spacing, padding, borders: including collapsed, colou

Published by Patrick Mutisya · 8 days ago

ICT 0417 – Website Authoring Notes

21 Website Authoring

Learning Objective

By the end of this lesson you will be able to create CSS classes that control:

  • Background colour and background images
  • Font colour, size, style and family
  • Table, table row and table data properties – size, background colour, horizontal and vertical alignment, spacing, padding, borders (collapsed, colour, thickness, visible/invisible)

1. Background Properties

Background properties are applied to any block‑level element (e.g., <div>, <body>, <td>) using CSS class definitions.

  • background‑color – sets a solid colour behind the element.
  • background‑image – places an image behind the element. The image can be repeated, positioned, or fixed.
  • background‑repeat – controls whether the image repeats horizontally, vertically, both or not at all.
  • background‑position – defines the starting point of the image (e.g., center top).
  • background‑size – scales the image (e.g., cover or contain).

Suggested diagram: Visual comparison of background‑color vs. background‑image with repeat and position settings.

2. Font Properties

Font properties affect the appearance of text inside an element.

  • color – colour of the text.
  • font‑family – typeface (e.g., Arial, sans-serif).
  • font‑size – size of the text (e.g., 16px, 1.2em).
  • font‑weight – thickness of the characters (normal, bold, numeric values).
  • font‑style – normal, italic, or oblique.
  • text‑align – horizontal alignment inside the block (left, center, right, justify).
  • line‑height – vertical spacing between lines (often expressed as a unitless multiplier, e.g., \$1.5\$).

3. Table‑Related Properties

Tables are built from three core elements:

  • <table> – the container.
  • <tr> – a table row.
  • <td> – a table data cell (or <th> for header cells).

3.1 Size and Spacing

  • width and height – set explicit dimensions for the table or cells.
  • border‑spacing – distance between adjacent cells when border‑collapse: separate is used.
  • cellpadding (HTML attribute) – internal space between cell content and its border (equivalent to CSS padding).
  • cellspacing (HTML attribute) – external space between cells (equivalent to CSS border‑spacing).

3.2 Alignment

  • text‑align – horizontal alignment of text inside a cell (left, center, right).
  • vertical‑align – vertical alignment of content within a cell (top, middle, bottom, baseline).

3.3 Borders

  • border – shorthand for width, style and colour (e.g., 2px solid #000).
  • border‑collapsecollapse merges adjacent borders into a single line; separate keeps them distinct.
  • border‑stylenone, solid, dashed, dotted, etc.
  • border‑color – colour of the border.
  • border‑width – thickness (e.g., 1px, 0.5em).

3.4 \cdot isibility

To make a border invisible set border‑style: none or border‑width: 0. The cell still occupies space unless display: none is used, which removes it from the layout.

4. Example Class Definitions (Illustrative Only)

The following pseudo‑code shows how the properties can be combined into CSS classes. In an exam you would write the actual CSS in a <style> block or external stylesheet.

/* Background class */

.bg‑blue {

background‑color: #cce5ff;

}

/* Background image class */

.bg‑pattern {

background‑image: url('pattern.png');

background‑repeat: repeat;

background‑position: center;

}

/* Font class */

.text‑large‑bold {

color: #333333;

font‑family: Arial, sans-serif;

font‑size: 1.2em;

font‑weight: bold;

}

/* Table class with collapsed borders */

.table‑collapsed {

border‑collapse: collapse;

width: 100%;

}

/* Table cell class with padding and centre alignment */

.td‑center {

padding: 8px;

text‑align: center;

vertical‑align: middle;

border: 1px solid #999999;

}

5. Summary Table of Key Properties

ElementPropertyPossible \cdot aluesEffect
Any block elementbackground‑colorcolour name, hex, rgb, rgbaSets solid background colour
Any block elementbackground‑imageurl('path'), nonePlaces an image behind the element
Any text elementfont‑sizepx, em, rem, %Controls size of the text
Any text elementfont‑weightnormal, bold, 100‑900Controls thickness of characters
<table>border‑collapsecollapse, separateDetermines how cell borders are rendered
<td> / <th>paddinglength values (e.g., 5px, 0.5em)Space between cell content and its border
<td> / <th>text‑alignleft, center, right, justifyHorizontal alignment of text
<td> / <th>vertical‑aligntop, middle, bottom, baselineVertical alignment of content
<td> / <th>borderwidth style colour (e.g., 2px solid #000)Defines cell border appearance

6. Practical Exercise

  1. Create an HTML page with a <table> that has three columns and two rows.
  2. Define a class .header‑row that gives the first row a dark background colour, white text, and a 2 px solid border.
  3. Define a class .data‑cell that centres the text horizontally and vertically, adds 10 px padding, and uses a light‑grey background.
  4. Apply border‑collapse: collapse to the table so that adjacent borders merge.
  5. Validate the page with the W3C validator to ensure correct HTML5 syntax.

7. Quick Reference Formula

When calculating the total width of a table with collapsed borders, use:

\$\text{Total Width} = \sum{i=1}^{n} \text{cell‑width}i + (n-1)\times\text{border‑width}\$

where \$n\$ is the number of columns.

8. Checklist Before Submission

  • All CSS classes are defined and correctly linked to the HTML elements.
  • Background colours and images display as intended on different browsers.
  • Font properties are consistent with the specification.
  • Table borders are either collapsed or separate as required, and spacing/padding are correct.
  • HTML validates without errors or warnings.