Skip to main content
CSS for Beginners
CHAPTER 03 Beginner

CSS Colors, Backgrounds, and Gradients

Updated: May 10, 2026
5 min read

# CSS Colors, Backgrounds, and Gradients

1. Introduction

Color brings the web to life! In this chapter, we will dive deep into how CSS handles colors, background images, and beautiful modern gradients to make your websites pop.

2. Learning Objectives

  • Master color formats: HEX, RGB, RGBA, and HSL.
  • Apply background colors and background images.
  • Control background repetition and positioning.
  • Create linear and radial gradients.

3. Detailed Explanations

Color Formats

CSS supports several ways to define a color.
  • Color Names: red, blue, transparent
  • HEX: #FF0000 (Red)
  • RGB / RGBA: rgba(255, 0, 0, 0.5) (Red with 50% opacity)
  • HSL: hsl(120, 100%, 50%) (Green based on Hue, Saturation, Lightness)
css
12345
.text-examples {
    color: #333333; /* Dark gray HEX */
    background-color: rgba(0, 0, 0, 0.1); /* Light transparent black */
    border-color: hsl(200, 100%, 50%); /* Bright blue HSL */
}

Background Images

You can set images as backgrounds using background-image.
css
123456
.hero-section {
    background-image: url('hero.jpg');
    background-size: cover; /* Covers the whole area */
    background-position: center; /* Centers the image */
    background-repeat: no-repeat; /* Prevents tiling */
}

Linear Gradients

Gradients smooth transition between two or more colors.
css
123456789
.gradient-box {
    /* Gradient from top to bottom */
    background: linear-gradient(to bottom, #ff7e5f, #feb47b);
}

.diagonal-gradient {
    /* 45 degree angle */
    background: linear-gradient(45deg, blue, purple);
}

Radial Gradients

Radial gradients emanate from a center point outward.
css
123
.circle-gradient {
    background: radial-gradient(circle, white, black);
}

4. Mini Project: Colorful Hero Section

html
1234
<header class="hero">
    <h1>Welcome to the Future</h1>
    <p>Discover amazing gradients.</p>
</header>
css
1234567891011121314151617
.hero {
    height: 400px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: white;
    text-align: center;
    /* Beautiful modern gradient */
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    box-shadow: 0 4px 15px rgba(0,0,0,0.2);
}

.hero h1 {
    font-size: 3rem;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}

> [!TIP] > Always provide a fallback background-color in case your background-image fails to load!

5. MCQs

Q1: Which CSS property is used to create a background that transitions from blue to green? A) background-color B) background-blend C) linear-gradient D) color-transition *Answer: C*

6. Summary

You now know how to paint the web! Combining RGBA transparency, background images, and modern linear gradients allows you to create enterprise-level aesthetic designs.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·