About 2803 letters

About 14 minutes

#CSS Background

In CSS, the background property is used to set the background of an element. It's very powerful and can control background color, background image, position, size, repeat behavior, and more.

You can use the shorthand background property or set the following sub-properties individually:

PropertyDescriptionExample
background-colorBackground colorbackground-color: #f0f0f0;
background-imageBackground imagebackground-image: url('bg.jpg');
background-repeatWhether to repeatbackground-repeat: no-repeat;
background-positionPosition of the imagebackground-position: center top;
background-sizeSize of the backgroundbackground-size: cover;
background-attachmentScroll behaviorbackground-attachment: fixed;
background-originArea where background is positionedbackground-origin: padding-box;
background-clipArea where background is paintedbackground-clip: content-box;

#Example:

<!-- Background image --> <div style="background-image: url(https://xplanc.org/primers/icon.svg); height:240px;"></div>

Background image

<!-- Background image centered --> <div style="background-position: center; background-image: url(https://xplanc.org/primers/icon.svg); height:240px;"></div>

Centered

<!-- Background image centered, no repeat --> <div style="background-repeat: no-repeat; background-position: center; background-image: url(https://xplanc.org/primers/icon.svg); height:240px;"></div>

No repeat

<!-- Background image centered, no repeat, stretched cover --> <div style="background-size: cover; background-repeat: no-repeat; background-position: center; background-image: url(https://xplanc.org/primers/icon.svg); height:240px;"></div>

stretched cover

Created in 5/30/2025

Updated in 5/30/2025