CSS Tutorial
CSS Colors
Set color with names, hex, RGB, HSL, or currentColor. Pick contrast you can actually read.
Color on text and boxes
Two properties do most of the work. color paints the text (and, by default, the text of children). background-color paints the box behind that text. Set both when you change the background, or the words may vanish into the page.
The same hue can be a name, a hex code, rgb(), or hsl(). Pick one style per project and stay with it. Harbor studio examples in this tutorial use sky #0284c7 andteal.
Named colors
Browsers know a list of names such as teal, navy, white, andblack. Names are easy to type. They are also coarse: you cannot nudge teal a little darker without switching notation.
Example
<style>
h1 {
color: teal;
}
p {
color: #475569;
}
.panel {
background-color: teal;
color: white;
padding: 0.75rem 1rem;
}
</style>
<h1>Harbor studio</h1>
<p>Open 8–16.</p>
<p class="panel">White text on a teal panel.</p>Hex codes
A hex color starts with # and six hexadecimal digits: two for red, two for green, two for blue. Each pair runs from 00 (none) to FF (full). #0284c7 is the sky used on these pages. #ffffff is white. #000000 is black.
You can shorten a value when both digits in a pair match: #336699 becomes #369. Prefer the six-digit form until that shortcut feels natural. An eight-digit hex adds alpha:#0284c780 is sky at half opacity.
Example
<style>
h1 {
color: #0284c7;
}
p {
color: #475569;
}
.panel {
background-color: #0c4a6e;
color: #f0f9ff;
padding: 0.75rem 1rem;
}
</style>
<h1>Harbor studio</h1>
<p>Open 8–16.</p>
<p class="panel">Sky ink on deep harbor navy.</p>rgb() and hsl()
rgb(red, green, blue) uses numbers from 0 to 255. rgb(2, 132, 199) is the same sky as #0284c7. Modern CSS also accepts rgb(2 132 199 / 0.2) for a transparent wash.
hsl(hue, saturation, lightness) is often easier to tune. Hue is 0 to 360 around the color wheel. Saturation and lightness are percentages. Raise lightness to fade a color; drop it to darken. Sky sits near hue 199.
| Notation | Example | Use when |
|---|---|---|
| Name | teal | You want a quick, coarse color |
| Hex | #0284c7 | You copy a brand token |
| RGB | rgb(2, 132, 199) | You already think in channels |
| HSL | hsl(199, 98%, 39%) | You want to lighten or shift hue |
Example
<style>
h1 {
color: rgb(2, 132, 199);
}
p {
color: hsl(215, 16%, 37%);
}
.wash {
background-color: hsl(199, 98%, 92%);
color: hsl(199, 98%, 24%);
padding: 0.75rem 1rem;
}
</style>
<h1>Harbor studio</h1>
<p>Open 8–16.</p>
<p class="wash">Same hue, high lightness: a pale wash with dark text.</p>currentColor
The keyword currentColor means “whatever color is on this element.” Borders, outlines, and some shadows can reuse the text color so they stay in sync when you restyle the type.
Example
<style>
h1 {
color: teal;
}
.hours {
color: #0284c7;
border: 2px solid currentColor;
padding: 0.5rem 0.75rem;
}
p {
color: #475569;
}
</style>
<h1>Harbor studio</h1>
<p class="hours">Open 8–16.</p>
<p>The hours box border uses currentColor, so it matches the sky text.</p>Click Try it in CSS and change .hours { color } to teal. The border follows. That live preview is /css/try, not the HTML editor.
Contrast you can read
Light gray on white, or yellow on white, fails many readers. If you cannot read the line at a glance, do not ship it. Dark text on a light panel, or light text on a dark panel, is the usual pair. Test a pale wash with dark ink, not pale ink on pale paper.
Color is not the only signal. Do not rely on red versus green alone for hours or errors. Pair color with words, weight, or an icon so the meaning survives grayscale and color vision differences.
Next you will paint the area behind the content — background color, then images, position, and size.
Worked examples
The short listings above show one property. These sheets paint documents you would actually ship: a lab dashboard, a clinic form, a marks table, a nav bar.
CSS does not invent meaning. HTML already said what the pieces are. Cascade, specificity, and the box model decide how they look. The numbers are classroom values — current, pH, pulse, 3-4-5 — so the style has something real to sit on.
Preview them in the CSS editor at /css/try. Change one property and watch the page paint. The tags stay the same.
Chemistry
Hex versus RGB on an indicator
teal, #166534, rgb(22, 101, 52), and hsl(142, 64%, 24%) can be the same green. Hex is common in design tokens. RGB matches how screens mix light.
The pH number must stay in the text. Colour is a legend, not the only signal.
pH = −log₁₀[H⁺]
Example
<style>
.acid { color: #b91c1c; }
.neutral { color: rgb(22, 101, 52); }
.alkali { color: hsl(217, 91%, 45%); }
</style>
<p class="acid">pH 1</p>
<p class="neutral">pH 7</p>
<p class="alkali">pH 10</p>Physics
currentColor on a unit
currentColor means “use this element’s text colour”. Borders and icons can track the type. Change the paragraph colour once; the mark follows.
Example
<style>
p { color: #0f766e; border-left: 4px solid currentColor; padding-left: 0.5rem; }
</style>
<p>I = 0.45 A</p>