Now that you know the shape of a selector, it’s finally time to add color. Color is the very first thing in CSS where you really feel “it changed!” Let’s put your favorite color onto that pure-white page.
How to write color
h1 {
color: white;
background-color: teal;
}It means “make the heading text white and the background teal.” Color names (red, blue, teal, …) can be written as they are. “Lining up settings inside { }” is already coming in handy.
| Property | What it changes |
|---|---|
color | The color of the text |
background-color | The color of the background |
These two are so alike that they easily get mixed up. Remember them as a set: “color is the text itself; add background- and it’s the background.”
Even for the same sentence, color and background-color put the color in completely different places.
color is the color of the text itself; background-color is the background color that spreads across the whole box. Choose the two as a pair for a readable color scheme.There are over 140 color names
Beyond basic colors like red and blue, there are over 140 colors you can call by name, such as tomato, skyblue, and gold. The best way to improve is to type in names you know and enjoy the changes.
p {
color: tomato;
}Color codes for finer control
When you want a subtle shade that has no name, use a color code like #ff0000 (red). The six digits after the # are the color’s true identity.
h1 {
color: #e8734e;
}The six digits split into three pairs, each representing the intensity of red, green, and blue (256 levels from 00 to ff). Named colors number around 140, but with codes you have your pick of over 16 million colors. It’s also handy that you can copy a color you found in a design tool and bring it straight over.
Background color applies to “the element’s whole box”
background-color applies not just around the text but to the element’s entire box. An element like h1 stretches the full width, so the background color becomes like a band stretched across.
h1 {
background-color: teal;
}If you want color on just the text part, wrap that part in a <span> and set background-color on the span only. Knowing that “background color applies to the box” saves you from being surprised by unexpected spreading.
h1 stretches the full width, so its background becomes a band, while a span-wrapped part gets a background only as wide as the text.When stuck on colors, it’s fine to lean on tools
Even once you know how to write color and background-color, “which color goes with which” is a separate worry. This is less about taste and more something you can solve with a tool. Even pros often pick from color-scheme sites.
Summary of this lesson
- Text color is
color, background color isbackground-color - You can write colors by name directly (over 140 of them)
- Think of text and background color as a pair (for readability)
