Background Image
CSS allows you to add an image as the background of an HTML element using the background-image property.
You can also control the background image using properties such as background-size, background-position, and background-repeat.
Background Image Flow
Example 1: CIIT Training Institute Logo
The CIIT Training Institute logo is used as a CSS background image.
CSS Code
div {
background-image: url("https://ciitinstitute.com/assets/mainlogo.png");
background-size: contain;
background-position: center;
background-repeat: no-repeat;
background-color: #ffffff;
min-height: 280px;
}
Output
Here, the CIIT logo is displayed as the background image.
Example 2: Background Size
The background-size property controls how the background image fits inside the element.
CSS Code
div {
background-image: url("https://images.unsplash.com/photo-1498050108023-c5249f4df085?auto=format&fit=crop&w=1200&q=80");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
min-height: 280px;
}
Output
cover makes the image cover the complete element.
Example 3: Background Position
The background-position property controls where the background image is placed.
CSS Code
div {
background-image: url("https://images.unsplash.com/photo-1516321318423-f06f85e504b3?auto=format&fit=crop&w=1200&q=80");
background-size: cover;
background-position: right center;
background-repeat: no-repeat;
min-height: 280px;
}
Output
right center places the background image toward the right side and vertically centers it.
Common Background Image Properties
| Property | Purpose | Example |
|---|---|---|
background-image |
Adds an image as the background | url("image.jpg") |
background-size |
Controls image size | cover |
background-position |
Controls image position | center |
background-repeat |
Controls image repetition | no-repeat |
Simple Explanation
background-image → Adds an image to the background.
background-size → Controls how large the image appears.
background-position → Controls where the image appears.
background-repeat → Controls whether the image repeats.
Summary
-
Use
background-imageto add a background image. -
Use
background-sizeto control image size. -
Use
background-positionto control image position. -
Use
background-repeatto control image repetition.