CHAPTER 7
CSS Backgrounds
CSS background properties control the background color and background images of elements.
01. What is CSS Backgrounds?
background-color sets a background color.
background-image adds an image or gradient.
background-repeat controls whether a background repeats.
background-position controls the position of a background.
background-size controls the size of a background.
02. Syntax
body {
background-color: #f5f7fb;
}
.hero {
background-image: url("image.jpg");
background-size: cover;
background-position: center;
}
03. Example
CSS / HTML Example
AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
}
.hero {
min-height: 300px;
display: flex;
align-items: center;
justify-content: center;
color: white;
background:
linear-gradient(
135deg,
#2563eb,
#7c3aed
);
}
</style>
</head>
<body>
<section class="hero">
<h1>
AIFA CSS Backgrounds
</h1>
</section>
</body>
</html>
04. Important Points
✓
background-color is useful for simple backgrounds.
✓
background-image can display images and gradients.
✓
background-size: cover is commonly used for hero sections.
✓
background-position can keep important image content visible.
05. Common Mistakes
⚠
Using an incorrect image path.
⚠
Using a background image without considering readability.
⚠
Forgetting to set an appropriate background size.
06. Real World Use
Backgrounds are commonly used in hero sections, cards, banners, landing pages and website sections.
Ready to Practice? 🚀
Open the AIFA CSS Editor and practice this chapter live.
Try It Yourself →
Practice Exercise
Create a Hero Background
Create a hero section with a gradient background, centered heading and readable text.