Colors and Backgrounds in CSS

CSS without colors feels like a garden without flowers.
Colors and backgrounds give life, emotion, attraction, branding, and beauty to a webpage. A good designer does not only write code — he paints experiences.

Today we will learn how websites use colors and backgrounds to look modern and attractive.


1. What are Colors in CSS?

Colors are used to style:

  • Text
  • Backgrounds
  • Borders
  • Buttons
  • Shadows
  • Links
  • Cards
  • Sections

Example:

<p style="color:red;">This text is red.</p>

Output:

The text becomes red.


Why Colors Matter

Imagine two websites:

  • One has plain black and white only
  • Another has beautiful colors, contrast, gradients, and clean design

Which looks professional?

Obviously the second one.

Good colors:

  • Improve readability
  • Create emotions
  • Improve user experience
  • Make websites modern
  • Help branding

Example:

  • Blue → Trust (banks, tech companies)
  • Green → Nature, success
  • Red → Danger, warning
  • Black → Luxury, premium

2. Color Names in CSS

CSS already knows many color names.

Example:

color: red;
background-color: yellow;

Common Color Names

Color Name Output
red Red
blue Blue
green Green
black Black
white White
yellow Yellow
orange Orange
purple Purple
gray Gray
pink Pink

Example

<!DOCTYPE html>
<html>
<head>
<style>
h1{
    color: blue;
}

p{
    color: green;
}
</style>
</head>
<body>

<h1>Welcome</h1>
<p>This is green text.</p>

</body>
</html>

Logic Behind Color Names

Color names are easy for beginners.

But they have limitations:

  • Limited control
  • Not many shades
  • Not professional for advanced design

That is why developers mostly use:

  • HEX
  • RGB
  • RGBA

3. HEX Colors

HEX means Hexadecimal color code.

Format:

#RRGGBB
  • RR = Red
  • GG = Green
  • BB = Blue

Values go from:

  • 00 → lowest
  • FF → highest

Example

color: #ff0000;

This means:

  • Full Red
  • No Green
  • No Blue

Result → Red color


Common HEX Colors

Color HEX
White #ffffff
Black #000000
Red #ff0000
Green #00ff00
Blue #0000ff
Yellow #ffff00

Real-Life Logic of HEX

Think of mixing light colors:

  • Red light
  • Green light
  • Blue light

By changing their strength, millions of colors are created.

Example:

  • #000000 = complete darkness → black
  • #ffffff = full brightness → white

Example

<p style="color:#ff6600;">
Orange text using HEX color
</p>

4. RGB Colors

RGB stands for:

  • Red
  • Green
  • Blue

Format:

rgb(red, green, blue)

Values:

  • 0 to 255

Example

color: rgb(255,0,0);

Meaning:

  • Red = 255
  • Green = 0
  • Blue = 0

Result → Red


More Examples

RGB Code Color
rgb(255,0,0) Red
rgb(0,255,0) Green
rgb(0,0,255) Blue
rgb(255,255,255) White
rgb(0,0,0) Black

Why RGB is Useful

RGB gives precise control.

You can create custom shades.

Example:

  • Light blue
  • Dark blue
  • Sky blue
  • Navy blue

All possible with RGB.


Example

<h2 style="color:rgb(128,0,128);">
Purple Heading
</h2>

5. RGBA Colors

RGBA is same as RGB, but with:

A = Alpha (Transparency)

Format:

rgba(red, green, blue, alpha)

Alpha values:

  • 0 = fully transparent
  • 1 = fully visible

Example

background-color: rgba(255,0,0,0.5);

Meaning:

  • Red background
  • 50% transparent

Real-Life Logic of RGBA

Imagine colored glass:

  • You can partially see through it

RGBA works the same way.

Very useful for:

  • Overlay effects
  • Modern UI design
  • Transparent cards
  • Hero sections

Example

<div style="
background-color:rgba(0,0,255,0.3);
padding:20px;
">
Transparent Blue Box
</div>

6. Background Color

The background-color property changes element background.


Example

body{
    background-color: lightblue;
}

Entire page becomes light blue.


Another Example

<div style="
background-color:yellow;
padding:20px;
">
This div has yellow background.
</div>

Real-Life Use of Background Colors

Used for:

  • Sections
  • Cards
  • Buttons
  • Navigation bars
  • Footers

Example:

  • Dark navbar
  • White content area
  • Blue buttons

7. Background Images

CSS allows images as backgrounds.

Property:

background-image

Example

body{
    background-image: url("nature.jpg");
}

Logic Behind Background Images

Instead of placing image inside HTML:

<img src="image.jpg">

CSS places image behind content.

Useful for:

  • Hero sections
  • Website banners
  • Landing pages
  • Full-screen designs

8. Background Repeat

By default background image repeats.

Example:

background-repeat: repeat;

Stop Repeating

background-repeat: no-repeat;

9. Background Size

Controls image size.


Full Screen Background

background-size: cover;

Meaning:

  • Image covers entire area

Example

body{
    background-image:url("bg.jpg");
    background-size:cover;
    background-repeat:no-repeat;
}

10. Background Position

Controls image position.


Example

background-position:center;

Other values:

  • top
  • bottom
  • left
  • right

Full Background Example

<!DOCTYPE html>
<html>
<head>
<style>
body{
    background-image:url("nature.jpg");
    background-size:cover;
    background-repeat:no-repeat;
    background-position:center;
}
</style>
</head>
<body>

<h1>Beautiful Website</h1>

</body>
</html>

11. Gradient Backgrounds

Gradients are smooth color transitions.

Very modern and attractive.


Types of Gradients

  1. Linear Gradient
  2. Radial Gradient

12. Linear Gradient

Colors move in a straight direction.


Example

background: linear-gradient(red, yellow);

Left to Right Gradient

background: linear-gradient(to right, blue, purple);

Real-Life Logic

Imagine sunset sky:

  • Orange slowly changes into pink
  • Pink changes into purple

That smooth transition is gradient.


Example

<div style="
height:200px;
background:linear-gradient(to right, blue, purple);
">
</div>

13. Radial Gradient

Colors move from center outward.


Example

background: radial-gradient(red, yellow);

Real-Life Example

Like:

  • Sunlight
  • Lamp glow
  • Water ripple

Modern Website Example

<!DOCTYPE html>
<html>
<head>
<style>

body{
    height:100vh;
    background:linear-gradient(to right,#141e30,#243b55);
    color:white;
    text-align:center;
}

h1{
    padding-top:200px;
    font-size:50px;
}

</style>
</head>
<body>

<h1>Modern Gradient Website</h1>

</body>
</html>

Best Practices for Colors

1. Keep Good Contrast

Bad:

color: yellow;
background-color: white;

Hard to read.

Good:

color: white;
background-color: black;

2. Do Not Use Too Many Colors

Professional websites usually use:

  • 2 primary colors
  • 1 accent color

3. Use Soft Backgrounds

Very bright backgrounds hurt eyes.

Better:

  • Light gray
  • Soft blue
  • Light cream

4. Maintain Consistency

Use same color theme across website.

Example:

  • All buttons blue
  • All headings dark

Real Website Color Examples

Website Type Common Colors
Islamic Website Green, White, Gold
School Website Blue, White
Tech Website Black, Blue
Food Website Red, Orange
Luxury Brand Black, Gold

Common Beginner Mistakes

Mistake 1

Using too many bright colors.


Mistake 2

Low contrast text.


Mistake 3

Huge repeating background images.


Mistake 4

Ignoring mobile screen appearance.


Mini Practice Tasks

Task 1

Create:

  • Blue background
  • White text

Task 2

Create:

  • Gradient background
  • Center heading

Task 3

Use:

  • HEX color
  • RGB color
  • RGBA color

Interview Questions

Q1. Difference between RGB and RGBA?

RGBA includes transparency (alpha value).


Q2. What does HEX mean?

Hexadecimal color code.


Q3. Which property adds background image?

background-image

Q4. Which value prevents image repeat?

no-repeat

Learning Outcome Achieved

After completing Day 3, students can:

✅ Use color names
✅ Use HEX colors
✅ Use RGB and RGBA
✅ Add background colors
✅ Add background images
✅ Create gradient backgrounds
✅ Design attractive webpages
✅ Build professional color combinations


Final Revision Summary

Topic Purpose
Color Names Simple colors
HEX Professional color coding
RGB Custom color mixing
RGBA Transparent colors
Background Color Element/page background
Background Image Decorative backgrounds
Gradient Smooth color transition

Homework

Create a webpage with:

  • Gradient background
  • White heading
  • Background image section
  • Transparent card using RGBA
  • Professional color theme

Goal: Make it look like a modern landing page.

List of Modules