CSS Backgrounds
Hey friends 👋 welcome back. In the last lesson, we learned about borders, margins, and padding — the CSS box model. Today, we are going to make our pages more exciting with CSS backgrounds.
With CSS, you can style the background of any element using colors, images, gradients, and patterns. Let’s dive in.
Background Color
The simplest background is just a solid color. You can set it using the background-color property.
Background Image
You can also add an image as a background using background-image.
By default, the image repeats both horizontally and vertically.
Controlling Backgrounds
You can control how the background behaves using:
-
background-repeat→ values:repeat,repeat-x,repeat-y,no-repeat. -
background-position→ values:left,right,center,top,bottom. -
background-size→ values:auto,cover,contain, or exact sizes like100px 200px. -
background-attachment→ values:scrollorfixed.
Example:
This code makes the background image cover the whole page, stay centered, and remain fixed even when scrolling.
Background Shorthand
Instead of writing many properties, you can combine them in one line:
This is called shorthand, and it makes your CSS cleaner.
Try It Yourself
Create a new file called backgrounds.html. Use CSS to:
-
Give your page a solid color background.
-
Add a heading with a different background color.
-
Experiment with a background image — try
cover,contain, andfixed.
See how each property changes the design.
Great job 🎉 You just learned about CSS backgrounds. In the next tutorial, we will explore CSS Lists and Text Decoration, where we’ll style bullet points, numbered lists, and make our text even more creative. 🚀
0 Comments