Background colors, images, and gradients
Backgrounds can establish hierarchy or add atmosphere, but they should support rather than compete with the content placed on top.
✓ What you will learn in this topic
- Explain the key idea: Layer backgrounds intentionally, retain fallbacks, and preserve readable contrast.
- Apply the concept: A diagonal blue-to-navy gradient appears behind white text, with the solid color retained as a fallback.
- Avoid this common mistake:
Concept explanation
background-color paints a fallback color. background-image accepts images and gradients; gradients are generated by CSS and do not require a separate image file. Use background-size: cover when a photographic background should fill a region, and position the important subject intentionally.
Text over an image needs a dependable contrast treatment, such as a solid overlay or a carefully tested gradient. Decorative imagery should not contain information that visitors need to understand the page.
Syntax
A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.
.demo {
color: #fff;
background: linear-gradient(135deg, #1d4ed8, #0f172a);
padding: 1.5rem;
}Example 1: a small page element
Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.
<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>.demo {
color: #fff;
background: linear-gradient(135deg, #1d4ed8, #0f172a);
padding: 1.5rem;
}Expected result: A diagonal blue-to-navy gradient appears behind white text, with the solid color retained as a fallback.
Example 2: a practical interface card
The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.
<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>.card {
color: #fff;
background-color: #1e3a8a;
background-image: linear-gradient(110deg, rgb(30 58 138 / 96%), rgb(15 23 42 / 72%));
}Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.
Common mistake
A background image does not replace a text alternative for meaningful information, and low-contrast text can be difficult to read.
Best practice
Check foreground/background contrast, provide a fallback background color, and keep decorative images from carrying essential information.
Practice exercise
Create a promotional card with a solid fallback color, a subtle gradient, and text that remains readable over the background. Apply it to the example above and inspect the result at a narrow and a wide viewport.
Section summary
- Layer backgrounds intentionally, retain fallbacks, and preserve readable contrast.
- Check foreground/background contrast, provide a fallback background color, and keep decorative images from carrying essential information.
Knowledge check
Why declare a background color when a background image is also used?
Reveal the explanation
The fallback color prevents an unstyled or unreadable gap if the image cannot be displayed.
Finish the module lessons, then take the server-scored module quiz to check your understanding.
Practical example
css.demo {
color: #fff;
background: linear-gradient(135deg, #1d4ed8, #0f172a);
padding: 1.5rem;
}
Expected result:
A diagonal blue-to-navy gradient appears behind white text, with the solid color retained as a fallback.
Hands-on practice task
Create a promotional card with a solid fallback color, a subtle gradient, and text that remains readable over the background.
In this section
Other lessons in Colors and Backgrounds
Broaden your knowledge
Related learning topics
HTML
Learn to structure accessible, meaningful web pages with a complete HTML course from first document to professional application patterns.
JavaScript
Build strong JavaScript foundations with browser examples, focused practice, beginner projects, and module quizzes.