Speed is essential in web development. Especially when prototyping.
If you're not a 150WPM typing ninja, adding a background can be time consuming. And let's be honest...
Who wants to type
background-*: value; 8 times?
background-attachment: scroll; background-origin: padding-box; background-clip: border-box; background-color: transparent; background-image: none; background-position: 0% 0%; background-size: auto; background-repeat: repeat;
That's a lot of typing, and a lot of space. We can use the background shorthand to turn all of that into a single line:
background: scroll padding-box border-box transparent none 0% 0%/auto repeat;
In this article I want to show you how this shorthand works, and the rules you need to follow for it to work.
If you're not familiar with all of the properties above, please take a minute to read my quick introduction to background properties before reading this article. It will help you understand what's going on.
You might be thinking to yourself now:
Okay, it's nice that they're on the same line. But do I still need to write all of these values?
No. Unlike the font shorthand, all properties in the background shorthand are optional.
Be careful though.
If you don’t include a value for one of the properties, its default value will be used. Even if you’ve defined that property earlier.
The order of properties doesn't matter. With a few small exceptions:
background-size can only come immediately after
background-position, and the two have to be separated with a slash (
This means you can't set
background-position using this shorthand.
There are two ways to work around that though:
- Use the default
background-positionvalue, which is:
0% 0%. For example
0% 0%/50%would set
background-sizeseparately after the background shorthand. Like this:
background: url("cat.png"); background-size: 50%;
IMPORTANT: If you try setting
background-sizein the shorthand using one of its keywords (
contain) without the
backgroundproperty will be treated as invalid and ignored.
Because the values of
background-clip heavily overlap...
text- This one's the exception. It's available only for
...setting them using the shorthand works a bit differently.
If you only include one of the box values, it will be used for both
If you include two, the first one will be used for
background-origin and the second one for
If you don't use any, both properties will keep their default values.
This is why I've labeled them as box in the images.
NOTE: As far as I know,
textvalue is not supported by the background shorthand. So if you want to create gradient text, you will have to set
A few of the background properties can accept more than one value. It's important to keep those values next to each other and in the correct order.
background-size - If two values are provided, the first sets the width of the image, and the second its height.
background-repeat - If two values are provided, the first sets the repeat behavior for the horizontal (x) axis, and the second for the vertical (y) axis.
background-position - If two values are provided, the first controls the image's horizontal position, and the second its vertical position.
The background shorthand allows you to set a color or image as a background. You can even set both.
Wait, why would I need both?
Setting a fallback
background-color can be useful in many cases:
- If the image URL is broken or the image takes a long time to load, the background color can be a good placeholder.
- If the image contains transparency the background color will be visible underneath.
- If the value of
background-repeatis other than
round, there are likely to be areas not covered by the image. Those areas will be filled with the background color. You can see this above, in the
Have you ever tried to set a gradient as the
background-color, only to be surprised that it doesn't work?
That's because gradients are images in CSS. This is where the background shorthand comes in handy.
Mateusz HadryśThe background shorthand is the best way to set anything as a background. Including:
You don't have to remember which to use: background-image or background-color. Plus, it's the shortest.15:57 PM - 21 Dec 2020
Just like all other
background-* properties, the
background shorthand can accept multiple comma-separated layers.
background: url(cat.png) no-repeat scroll, url(grass-texture.jpg) repeat local green;
There is just one thing you have to keep in mind. An element can only have one
background-color, and it has to be on the last (bottom) layer. Otherwise the entire property will be treated as invalid.
If you ever need a reminder, here’s a handy cheatsheet 😃
Thanks for reading. I hope you learned something useful. If you have any questions, ask in the comments. Follow me for more web development tips.