CSS Backgrounds
Dive into the realm of CSS backgrounds and discover how to create captivating and immersive visual experiences on your web pages
The background is a color or image that may go behind the main content but plays a vital role in the visual presentation and accessibility of a page. When chosen carefully and has a proper contrast, the background sets the mood, creates the intended impression, and maintains readability.
Where do designers usually apply background? Think of hero images, article covers, image carousels, and different marketing and advertising materials. Explore how to define CSS background styles and apply properties of color, position, and size effectively.
The background-color
<p>
, <h1>
, <div>
, etc.
To specify color values in CSS, we use the following formats:
• A yellow
• A HEX value, like #F8DB46
• An RGB value, like rgb(248, 219, 70)
The background-image
Pro Tip: Even if you specify the image for a background, set a background-color
property as a back-up in case the image won't load.
We need the background-attachment
fixed
value, the image will always be in sight, while with the scroll
value, the image will scroll along with the
The background-clip
border-box
, padding-box
, or content-box
only.
Consider the following:
- The
border-box
value extends the background to the element's borders - The
padding-box
value extends it to the inner side of borders - The
content-box
value defines the background so that it doesn't go beyond itscontent area
Essentially, this property has no effect if the background color or background image aren't specified.
The background-origin
Consider the following:
- If you want it to start from the upper left corner of the border, you should use the
border-box
value - The
padding-box
value starts the image from the upper left corner of the padding edge - With the
content-box
value, the image extends to its full size from the upper left corner of thecontent container
Pro Tip: When the background-attachment
property has a fixed value, the background origin
property has no effect.
The background-position
top
, bottom
, left
, or right
— specifying the edge of the element's box to which you want to place the background. When we only use one value, the browser assumes the other value sets to center.
In the two-value notation, the first value represents the horizontal position, and the second represents the vertical. For example, the declaration background-position: bottom left;
means the object will be placed at bottom-left corner.
You can also use length (px
, pt
, cm
) or percentage values for defining the background position and combine them to further specify the coordinates.[1]
By default, the browser repeats the background
The background-repeat
property allows you to:
- Set a background image to repeat vertically along the y-axis, using the value
repeat-y
- Set the background image to repeat horizontally along the x-axis, using the value
repeat-x
- Set the background image to repeat in both directions, using the value
repeat
If you don't want any repetition, set the value to no-repeat
.
With the background-size
The contain
value scales the image to expand into the container without distorting proportions. If the container is bigger, the browser tiles the image.
In turn, the cover
value stretches the image, scaling it up to fill the container. If the image's proportions don't coincide with the element's, it gets cropped either vertically or horizontally so that no blank gaps are left.
You can use length (px
, pt
, cm
) or percentage values to set precise dimensions for an image.
The number of background properties is overwhelming. That's why the background
shorthand
The background
property allows us to set the background's
If you miss any property's value, the browser will set it to default.
For setting background-image
property. Its url value specifies the image location in an internal file system (relative link) or provides an external address (absolute link). Its standard syntax requires parentheses (( )
), quotation marks (" "
), and a semicolon (;
) at the end of the declaration.
The background-repeat
repeat-y
. What happens when we don't specify this property? The browser repeats the image both vertically and horizontally by default.
The background-position
top
, bottom
, left
, right
). Alternatively, for more precise coordinates, you can use pixels, points, centimeters, or percent.