CSS Background Image
The background-image
property in CSS is used to set an image as the background of an HTML element. You can apply it to any block-level or inline-level element. Here are some basics and examples to help illustrate how it works:
Basic Syntax
selector {
background-image: url('path/to/image.jpg');
}
Examples
1. Setting a Background Image on a Div
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Background Image Example</title>
<style>
.background-div {
width: 300px;
height: 200px;
background-image: url('https://via.placeholder.com/300x200');
background-size: cover; /* Makes sure the image covers the whole div */
background-position: center; /* Centers the image in the div */
background-repeat: no-repeat; /* Prevents the image from repeating */
}
</style>
</head>
<body>
<div class="background-div"></div>
</body>
</html>
2. Multiple Background Images
You can also use multiple images, separated by commas. The images will stack on top of each other.
.multiple-backgrounds {
width: 300px;
height: 200px;
background-image: url('https://via.placeholder.com/300x200'), url('https://via.placeholder.com/150x100');
background-position: center, top right;
background-size: cover, auto;
}
3. Using Gradient Backgrounds
CSS gradients can be used as backgrounds as well. They’re not images per se but function similarly in terms of background properties.
.gradient-background {
width: 300px;
height: 200px;
background-image: linear-gradient(to right, #ff7e5f, #feb47b); /* Creates a gradient from left to right */
}
4. Background Image with Overlay
Sometimes you might want to add an overlay to make text more readable on top of the background image.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Background Image with Overlay</title>
<style>
.overlay-container {
position: relative;
width: 300px;
height: 200px;
background-image: url('https://via.placeholder.com/300x200');
background-size: cover;
background-position: center;
}
.overlay-container::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5); /* Black overlay with 50% opacity */
z-index: 1;
}
.overlay-container .text {
position: relative;
z-index: 2;
color: white;
text-align: center;
padding: 20px;
}
</style>
</head>
<body>
<div class="overlay-container">
<div class="text">This is some overlay text</div>
</div>
</body>
</html>
Key Properties for Background Images
background-image
: Sets the image or images.background-position
: Defines the starting position of the background image.background-size
: Specifies the size of the background image (cover
,contain
, or specific sizes).background-repeat
: Determines if and how the image repeats (no-repeat
,repeat-x
,repeat-y
, orrepeat
).
Feel free to ask if you need more details or examples!
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.