Colors in CSS — How to Choose Text and Background Colors Like a Pro

Colors in CSS are the foundation of any site's visual identity. They define text clarity, interface appeal, and the user's first impression.

If you are looking for CSS color basics or how to change text and background colors in CSS or the best way to choose website colors, this lesson gives you the right foundation.

What Are Colors in CSS?

In CSS we use different properties to control colors depending on what we want to style:

  • color for text color.
  • background-color for background color.
  • border-color or border for border color.
Simple definition: Colors in CSS are properties that control the appearance of text, backgrounds, and borders in HTML elements.

Change Text Color with color

h2 {
    color: red;
}

p {
    color: blue;
}

What does this code do? It makes all h2 headings red and all p paragraphs blue.

Line by line:

  • h2 { color: red; }: targets subheadings and colors them.
  • p { color: blue; }: targets paragraphs and colors them.

Expected result: a clear visual difference between heading and body text.

Common mistake: choosing colors that are too similar and reduce readability.

Result in the browser:

A red heading

A blue paragraph showing the color property.

Change Background Color with background-color

div {
    background-color: #fef08a;
    color: #111827;
}

What does this code do? It adds a soft yellow background to all div elements with dark text for contrast.

Line by line:

  • background-color: sets the element background color.
  • color: sets the text color inside the background.

Expected result: a clear, readable content box.

Common mistake: using light text on a light background so the content disappears.

Result in the browser:

This is a box with a light yellow background and dark text.

Change Border Color with border-color

h3 {
    border: 2px solid #16a34a;
}

What does this code do? It adds a 2px green border around every h3 heading.

Line by line:

  • 2px: border width.
  • solid: border style.
  • #16a34a: border color.

Expected result: the heading stands out visually.

Common mistake: forgetting the border style (solid), which makes the color appear to not work.

Result in the browser:

Heading with a green border

Full Practical Example: A Colored Note Card

HTML:

<div class="note">
    <h3>Important note</h3>
    <p>Good color choices improve design quality and content clarity.</p>
</div>

CSS:

.note {
    background-color: #eff6ff;
    border: 1px solid #93c5fd;
    color: #1e3a8a;
    padding: 16px;
    border-radius: 10px;
}

.note h3 {
    color: #1d4ed8;
}

What does this example do? It creates a card with a calm background, light border, and readable text with a stronger heading.

Expected result: a balanced, easy-to-read element.

Common mistake: using very strong background colors with very dark text, which can be hard on the eyes.

Result in the browser:

Important note

Good color choices improve design quality and content clarity.

CSS fact: there are more than 140 named colors (like red and tomato), but real projects usually use HEX, RGB, or HSL for more precision.

Pro Tips for Choosing CSS Colors

  • Keep strong contrast between text and background.
  • Choose a limited palette (2 to 4 main colors) for a consistent identity.
  • Use a primary color for key buttons and links.
  • Test colors on both mobile and desktop because lighting differs.

Common Mistakes When Using Colors in CSS

1) Low contrast:

Example: light gray text on a white background. This makes reading hard and hurts the user experience.

2) Too many colors:

Using many random colors makes the design look unprofessional. Stick to a consistent color system.

3) Relying only on color names:

Named colors are useful for quick tests, but HEX/RGB/HSL give better control and accuracy in real projects.

Frequently Asked Questions — FAQ

How do I change text color in CSS?

Use the color property: p { color: blue; }.

How do I change background color in CSS?

Use background-color like: body { background-color: #f8fafc; }.

What is the difference between color and background-color?

color changes text color, while background-color changes the background area behind the element.

What is the best color format: name, HEX, or RGB?

For beginners: color names are easiest. For real projects: HEX and RGB (and HSL) are better for precision and control.

Why is my color not showing in CSS?

Common reasons: typo in the property, incorrect selector, or a stronger rule (specificity) overriding it.

Try now: Create a simple box and change its text, background, and border colors, then test different combinations until you get comfortable contrast.
Excellent! You now mastered the basics of colors in CSS. In the next lesson we will learn color formats in CSS (HEX, RGB, and HSL) to access millions of colors precisely.
Smart Editor

Write code and see the result instantly

Try it free