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;
Neat, huh?
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.
All CSS Background Properties Explained in 5 Minutes
Mateusz Hadryś ・ Dec 16 '20
Required Properties
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.
Order of Properties
The order of properties doesn't matter. With a few small exceptions:
Position & Size
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-size
without background-position
using this shorthand.
There are two ways to work around that though:
- Use the default
background-position
value, which is:0% 0%
. For example0% 0%/50%
would setbackground-size
to50%
and keepbackground-position
unchanged. - Set
background-size
separately after the background shorthand. Like this:
background: url("cat.png");
background-size: 50%;
IMPORTANT: If you try setting
background-size
in the shorthand using one of its keywords (cover
orcontain
) without thebackground-position
the wholebackground
property will be treated as invalid and ignored.
📦 Origin & Clip
Because the values of background-origin
and background-clip
heavily overlap...
border-box
padding-box
content-box
-
text
- This one's the exception. It's available only forbackground-clip
.
...setting them using the shorthand works a bit differently.
If you only include one of the box values, it will be used for both background-origin
and background-clip
.
If you include two, the first one will be used for background-origin
and the second one for background-clip
.
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,
background-clip
'stext
value is not supported by the background shorthand. So if you want to create gradient text, you will have to setbackground-clip
separately.
Multi Value Syntax
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.
🎨 Color & Image
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-repeat
is other thanrepeat
orround
, 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 thebackground-repeat
example image.
Gradients
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.
Layers
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.
Cheatsheet
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.
Top comments (0)