CSS Backgrounds - Detailed Overview

Introduction to CSS Backgrounds

In CSS, the background property is used to set background styles for elements. You can apply backgrounds to elements using various methods, including color, images, gradients, and more.

Background Color

The background-color property sets the background color of an element. For example:

div {
    background-color: #ff6347;
}
Background color: Tomato

Background Image

The background-image property sets an image as the background of an element. For example:

div {
    background-image: url('https://www.shariqsp.com/image1.jpg');
}
Background image

Background Gradient

The background-image property can also be used to create gradients. For example:

div {
    background-image: linear-gradient(to right, #ff7e5f, #feb47b);
}
Background gradient

Background Position

The background-position property sets the position of a background image. For example:

div {
    background-image: url('https://www.shariqsp.com/image1.jpg');
    background-position: center;
}
Background position: Center

Background Repeat

The background-repeat property sets how a background image repeats. For example:

div {
    background-image: url('https://www.shariqsp.com/image1.jpg');
    background-repeat: no-repeat;
}
Background repeat: No-repeat

Background Size

The background-size property sets the size of a background image. For example:

div {
    background-image: url('https://www.shariqsp.com/image1.jpg');
    background-size: contain;
}
Background size: Contain

Background Attachment

The background-attachment property sets whether a background image scrolls with the page or is fixed. For example:

div {
    background-image: url('https://www.shariqsp.com/image1.jpg');
    background-attachment: fixed;
}
Background attachment: Fixed