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.

NotationExampleUse when
NametealYou want a quick, coarse color
Hex#0284c7You copy a brand token
RGBrgb(2, 132, 199)You already think in channels
HSLhsl(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>

FAQ: CSS Colors

Common questions about this page.

What is the StudyGrid CSS tutorial?

The StudyGrid CSS tutorial is a full beginner track: selectors, the box model, text, layout, flex, grid, and responsive design. Each chapter has copy-and-preview examples.

Should I run css colors examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn css colors in this CSS CSS lesson (CSS Colors).

Is the CSS editor the same as Try HTML?

No. Try CSS is a stylesheet preview at /css/try. Try HTML stays at /html/try. CSS lessons never open the HTML-only editor or the Python editor.

Do I need to install anything to learn CSS?

No. Open a chapter, click Try it in CSS, and the page preview updates in the browser.

Where should I start the CSS tutorial?

Start at CSS Intro, then Syntax, Selectors, and How To. After colors and the box model, continue to flex, grid, and media queries. Use Next at the bottom of each chapter.

Is the CSS tutorial free?

Yes. The CSS studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live preview editor.