Published by Patrick Mutisya · 8 days ago
By the end of this lesson you should be able to:
An external style sheet is a separate file (normally style.css) that is linked to an HTML document with the <link> element. The file contains a list of CSS rules that can be applied to any number of pages.
Example of a basic external style rule:
/* style.css */
body {
background-color: #f0f0f0;
font-family: Arial, Helvetica, sans-serif;
}
Inline styles are written directly inside an HTML tag using the style attribute. They override external and internal styles for that element only.
<p style="color:#003366; font-size:14px; background-image:url('bg.jpg');">Sample text</p>
The following background properties are commonly used:
background-color – sets a solid colour.background-image – sets an image as the background.background-repeat – controls tiling (repeat, no-repeat, repeat-x, repeat-y).background-position – positions the image (e.g., center top).background-size – scales the image (e.g., cover, contain).Key font‑related CSS properties:
font-family – typeface(s) to use.font-size – size of the text (px, em, %).font-weight – normal, bold, or numeric values (100‑900).font-style – normal, italic, oblique.color – text colour.text-align – left, right, centre, justify.Tables can be styled using CSS properties applied to the table element itself, rows (tr), header cells (th) and data cells (td).
border – shorthand for width, style and colour.border-collapse – collapse or separate.border-spacing – space between cells when border-collapse: separate.padding – space inside a cell.text-align – horizontal alignment (left, centre, right).vertical-align – vertical alignment (top, middle, bottom, baseline).background-color – cell background colour.width and height – dimensions of table or cells./* External style sheet */
table {
width: 80%;
border-collapse: collapse; /* removes double borders */
margin: 20px auto;
}
th, td {
border: 2px solid #333333; /* visible border, 2 px thick */
padding: 8px;
text-align: centre;
vertical-align: middle;
}
th {
background-colour: #006699;
colour: #ffffff;
font-weight: bold;
}
tr:nth-child(even) {
background-colour: #e6f2ff; /* zebra striping */
}
| Property | Applied To | Example \cdot alue | Effect |
|---|---|---|---|
| border | table, th, td | 2px solid #333333 | Visible 2 px dark border around cells |
| border‑collapse | table | collapse | Single line between adjoining cells |
| border‑spacing | table | 10px 5px | Horizontal 10 px, vertical 5 px space (only when not collapsed) |
| padding | th, td | 8px | Space inside each cell |
| background‑colour | th | #006699 | Dark blue header background |
| colour | th | #ffffff | White text in header cells |
| text‑align | th, td | centre | Horizontal centre alignment |
| vertical‑align | td | middle | Vertical centre alignment |
| width / height | table, th, td | 80% (table), 120px (th) | Overall table width and header height |
| border‑style: none | td | none | Invisible border – useful for spacing only |
Sometimes a single table needs a unique look. Inline styles can be used for quick adjustments.
<table style="border: 3px dashed #990000; border-collapse: separate; border-spacing: 12px;">
<tr>
<th style="background-colour:#990000; colour:#ffffff; padding:10px;">Item</th>
<th style="background-colour:#990000; colour:#ffffff; padding:10px;">Price</th>
</tr>
<tr>
<td style="border: 1px solid #cccccc; padding:8px; text-align:left;">Notebook</td>
<td style="border: 1px solid #cccccc; padding:8px; text-align:right;">$2.50</td>
</tr>
</table>
border-collapse: collapse or separate is required.text-align (horizontal) and vertical-align (vertical).padding for space inside cells and border-spacing for space between cells when not collapsed.border-style: none makes a border invisible while still occupying space.