Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Backgrounds – Images & Gradi | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 15 of 50

·css

Backgrounds – Images & Gradients

10 mins

Course progress30%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • `background-size:cover` fills the container while preserving aspect ratio.
  • Multiple backgrounds are comma‑separated and are layered from left to right.
  • CSS gradients are resolution‑independent and lightweight.

Backgrounds are a layered paint system. You can stack multiple images, gradients, and color fills on a single element.

Core properties

  • background-image – URL, linear-gradient(), radial-gradient(), conic-gradient(), or a combination.
  • background-size – cover, contain, or explicit dimensions (200px 100%).
  • background-position – center, top left, percentages, or exact coordinates.
  • background-repeat – no-repeat, repeat-x, repeat-y, space, round.
  • background-attachment – scroll (default) or fixed (parallax effect).
  • background-blend-mode – blend multiple layers (multiply, screen, overlay).

Example – Gradient Overlay on a Hero Image

.hero {
  background-image:
    linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)),
    url('hero.jpg');
  background-size: cover;
  background-position: center;
  color:#fff;
  padding:4rem 2rem;
  text-align:center;
}

The first background (the gradient) sits on top of the image, providing a dark overlay that improves text readability.


Performance Tip – Use compressed images and WebP/AVIF where supported. Pair with image-set() to supply fallback formats:

background-image: image-set(
  url('hero.avif') type('image/avif') 1x,
  url('hero.webp') type('image/webp') 1x,
  url('hero.jpg') type('image/jpeg') 1x
);

Accessibility Note

Always supply a meaningful alt attribute on the <img> element if the image conveys information. Pure CSS backgrounds are decorative; they don’t need alt text.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonVisibility & OpacityContinue to lesson 16CSS Gradients Deep DiveBack to CSS Basics to Advanced curriculum