The background property in CSS is used to add the background effect(s) for any element – as a matter of fact – what is underneath the content of that element. It’s a shorthand property, so you can write properties in one single line instead of listing multiple properties.
.container {
background:
url("my-dropdown-arrow-icon.jpg"); /*image */
99% 50% /* position */
8% /* size */
no-repeat /* repeat */
fixed /* attachment */
padding-box /* origin */
content-box /* clip */
#9decb7; /* color */
}
Background is a shorthand property
So, shorthand background property is made of eight other properties, and you can use any combination of these properties, but make sure you use the order as listed:
- background-image
- background-position
- background-size
- background-repeat
- background-attachment
- background-origin
- background-clip
- background-color
We will take a look at each property in more detail below.
Follow the order of shorthand property
There is one thing that could make you wonder and lose a few minutes because of it. The order of properties. If you don’t specify a specific property in the background property, CSS will automatically set it to default. Or for example, in the case below, the background will be transparent, instead of gray, just because we didn’t follow the correct order. However, that is easy to fix. You need to apply the order of shorthand background property, so define the background-color after the background, or simply use the shorthand, following the order of properties.
.container {
background-color: gray;
background: url(my-arrow.jpg);
}
1. Everything you need to know about background-image property
With a background-image property you can use:
It is super simple to use an image on a background – the url() value enables you to provide a file path to any image.
.container {
background: url(my-arrow.jpg);
}
If you want to reduce HTTP requests, you can also use URI, but be careful with that – there are a few downsides. When economizing with HTTP requests, you can use background-image to sprite images, which is great for icons and similar. There are a few neat sites that help you create them and automatically apply their positioning.
And applying gradients, whether linear or radial, is not much more complicated either. Basically, gradients are just another form of a background image, but the difference is that the browser makes the image for you (additional load to consider). An example for a linear gradient goes like this:
.container {
background: linear-gradient(yellow, gray);
}
And for radial gradient, just replace the linear keyword with radial and add a keyword circle:
.container {
background: radial-gradient(circle, yellow, gray);
}
Remember, you can also set a fallback color if your background image does not load or the browser does not support gradient background. We’re saying below – create a radial gradient from yellow color to gray and if the browser does not support gradients, display gray:
.container {
background: radial-gradient(circle, yellow, gray) gray;
}
One another cool thing to be aware of when working with backgrounds – you can apply multiple backgrounds with layering – meaning multiple backgrounds layer over the top of each other. Basically, you can apply any property related to backgrounds, just make sure you create a comma-separated list, where each value in the comma-separated list corresponds to a layer – the comma separates the first image and its values from the second image and its values. The stacking order goes like this: list the image that should be at the front first (the first value is the top layer), and the image that should be at the back last (the second value is the second layer), and so on. Remember, the background color is always the last layer. You can set as many background images as you want, there’s no limit, and you can even animate your background images if you want.
.container {
background:
url(my-logo.png) bottom center no-repeat,
url(my-background-pattern.png) repeat-y;
}
2. Everything you need to know about background-position property
The default value for background-position is 0 0, which places your background image at the top-left of the container. However, you can move a background image in the container with background-position property, and you can use 3 types of values:
- length values (for example 10em 4em), where the first value is the horizontal position and second value is the vertical position, so the image will move 10em to the right and 4em down. You can use any of the lenght values.
- percentages (90% 4%), where the first value of 90% will move a background image by 90%, which means it will align the 90% point in the image to the 4% point in the container. 50% will align the middle of the image with the middle of the container, and 100% means it will align the last pixel of the image with the last pixel of the container.
- keywords (top right), where keywords are shortcuts for percentages; for example top right stands for 0 100% or top for 0% vertically and so on.
All modern browsers support up to four values for background-position when declaring values:
- Specifying one value defines horizontal offset and the browser sets the vertical offset to center.
- Specifying two values defines the first value which is the horizontal and the second value with the vertical offset.
- Specifying three or four values combines keywords and length or even percentage units, and the keyword precedes length or percentage unit. When specifying three or four values you cannot use “center”. With three values declared the “missing” fourth value is interpreted as 0 by the browser and our example positions the background image 4em from the left and 0em from the top of the container.
.position-with-three-values { background-position: left 4em top; }
- With four values declared, the example below positions background image 30% from the left and 10% from the top of the container.
.position-with-four-values { background-position: left 30% top 20%; }
3. Everything you need to know about background-size property
Background-size property accepts 4 different syntaxes, and we will go through all four with examples explained:
- the keyword,
- one-value,
- two-values,
- multiple background syntax.
The keyword syntax for background-size property
There are three keyword values available for the background-size property:
- Auto is the default value which tells the browser to automatically calculate the size based on the actual size of the image and the aspect ratio.
- Cover tells the browser that the image should always cover the entire container, even if it stretches or cuts it off from one of its edges
- Contain keyword tells the browser to show the whole image, even if that leaves a little space to the bottom or sides of the container.
The one-value syntax for background-size property
When you declare only one value, whatever unit it is – px, em, % etc. – that value counts for the width, and the browser will set the height to auto.
The two-values syntax for background-size property
When you declare two values, the first value counts for the width, and the second value counts for the background image’s height.
The multiple background syntax for background-size property
When working with multiple images, you can combine any of the above methods, but you have to keep the background image stacking order in mind and add commas between each syntax.
html {
background: url(my-icon.png), url(my-image.png);
background-size: 500px 200px, auto;
/* the my-icon.png is 500x200px, and the my-image.png is set to auto */
}
5. Everything you need to know about background-repeat property
As the name of the property says, background-repeat specifies if and how your background image will repeat. It’s like tiling – covering a background with a repeating pattern in a specific direction. The possible values for the background-repeat values with explanations are:
- repeat is the default value and will repeat the image in both directions – horizontally and vertically, and will cut if off if needed.
- repeat-x value will repeat the image in a horizontal direction (x-axis)
- repeat-y value will repeat the image in a vertical direction (y-axis)
- no-repeat value says don’t repeat, but show the image once
- space value will repeat the image both vertically and horizontally, but will never crop the image (unless the first showing of an image is too big to fit into the container). However, if the container can fit more than one image, it will distribute images evenly so the images are always touching the edges.
- round value will also repeat the image in both horizontal and vertical direction, and will also never crop the image unless a single image is too big to fit. However, it will stretch or squeeze them, if the container can fit multiple images with leftover space. It will squeeze the images if it’s less than half of one image width left or stretch them if it’s more than half of one image width left them to fill the space.
The example below shows the my-icon.jpg will repeat horizontally in the container, without stretching or squeezing:
.container {
background-image: url(my-icon.jpg);
background-repeat: repeat-x;
}
If you’re working with multiple backgrounds, simply add multiple values, but separate them with commas, as it’s the convention for multiple backgrounds.
6. Everything you need to know about background-attachment property
This property defines how to move the background relative to the viewport. It has three values:
- scroll value is the default and scrolls with the browser window (aka main view) but stays fixed inside the local view. Scroll value actually acts as a fixed background
- fixed value stays fixed no matter what. The best way to explain it is to imagine standing in front of the window – moving in front of the window will change your perspective, but won’t change where things are located outside of the window.
- local value was invented because the default scroll value acts as a fixed background, so the local view scrolls both with the main view and the local view.
7. Everything you need to know about background-origin property
The background-origin property specifies where exactly the background should be applied. With the background-origin property you can apply three values:
- border-box applies the background inside the element’s border
- padding-box applies the background inside its padding
- content-box applies the background across the entire element
However, when the background-attachment is fixed, CSS will ignore the background-origin, whatever value you apply to it.
8. Everything you need to know about background-clip property
background-clip is similar to background-origin, except it clips the background instead of resizing it as the background-origin does. background-clip helps you control how far a background image will extend beyond an element’s padding or content. In addition to the inherit, initial and the unset, background-clip property has three values:
- border-box is the default value and extends the background to the outside edge of the element’s border.
- padding-box will clip the background at the outside edge of the element’s padding and will not let it extend into the element’s border.
- content-box will clip the background at the edge of the content box.
9. Everything you need to know about background-color property
The background-color value is listed as the last one in the shorthand background property and specifies the background color of any HTML element. We talked already about colors and gradients at the beginning of the article when we talked about background-image property and mentioned the importance of order when listing properties. In the example below, we defined the h3 elements to have a whitesmoke background, while all divs will have a yellow background color.
In CSS, you can specify a color with a valid color name, which CSS can offer for you – in our example we used whitesmoke and yellow, but you can also go for a HEX value (“#F5F5F5”) or an RGB value – in our case that would be rgb(245, 245, 245). The background color is set like this:
h3 {
background-color: whitesmoke;
}
div {
background-color: yellow;
}
Background-color opacity vs transparency using RGBA
Here, it makes sense to bring out that you can additionally specify the opacity of an element – whether and how much you want the color of your element to be transparent. Values go from 0.0 (completely transparent) to 1.0 (no transparency). However, be careful – if you add transparency to the background of let’s say <div> element, all of div’s child elements will inherit that transparency. So, if you have text in that div, the text will become transparent too, which will make it harder to read.
You can avoid adding transparency to child elements (if HTML structure allows) by using RGBA color values RGBA stands for red, green, blue and alpha, where the alpha parameter specifies a level of transparency with a number between 0.0 (completely transparent) and 1.0 (fully opaque or no transparency).
div {
background: rgba(245, 245, 245, 0.4) /* white smokebackground with 40% opacity */
}
/* or like this */
div {
background-color: whitesmoke;
opacity: 0.4
}