Advertisement
  1. Web Design
  2. CSS
Webdesign

Revisiting the CSS Background Property

by
Difficulty:IntermediateLength:MediumLanguages:

Featured in: Learn CSS: The Complete Guide.

In this tutorial we’re going to look into one of the older, more familiar CSS properties: background. background is one of several CSS properties whose features are often overlooked. In fact, it saw an upgrade with CSS3, receiving new capabilities beyond just adding a background image or color. Let’s look into some of them!

1. Background Position Offset

Positioning a background graphic is straightforward and something you’re likely familiar with already. If we want to position the background to the bottom right of the element, we apply bottom right to background-position. For example:

Or, with the shorthand, background, after the url definition:

Since the advent of CSS3 we’ve been able to specify the position offset; the precise distances to the positions set. Taking our example of bottom right, we include bottom 20px right 30px to position our background at 20px from the bottom and 30px from the left.

The positions (bottom, top, right, left) can be defined in any order, but the offset length must be defined after each background position.

Credit goes to Metin Kazan for the illustrations.

2. Multiple Background Images

The background property also allows us to add multiple background images. Therefore, let's extend our previous example with more stuff and gadgets.

We add these images to a single background or background-image declaration by separating each one with a comma. We use the background-position property, which also accepts multiple values, to control each of those background images.

We can use fixed units (such as pixels), flexible units (such as percentages), or a combination of the two.

Each pair of values represents coordinates from the top left of the container element, to the top left of the image. For example, the top left of the camera image is 280px from the top of the container, then 7% of the available width across from the left.

Note: The available width is the total width of the container element, minus the width of the background image. Therefore a background image positioned 50% along the x axis appears exactly centered!

Motion

Furthermore, since the background-position is an animatable property, we can create a more lively, compelling background:

Here we’ve setup a number of keyframes along a timeline. At each keyframe we’ve altered the background-position-x and background-position-y of each background image. The animation is admittedly rudimentary, so please kindly fork the CodePen and improve it!

Note: Click Rerun at the bottom right of the pen to see the animation

Learn More About CSS Animation

A Couple of Noteworthy Points

The backgrounds we used are ordered sequentially; the very first listed appears at the top of the stack, while the last one listed will appear at the bottom of the background stack.

All background sub-properties (background-repeat, background-size, background-position etc.) may be defined multiple times, except background-color. If we apply multiple backgrounds using the shorthand background property, define the background color as the latest value to take effect. For example:

Alternatively, add a separate background-color, subsequent to the short-hand property:

Both of these codes do the same, but I find the latter to be more intuitive and readable.

3. Blend Background Image

The background-blend-mode does the same as you’ll find in graphics applications like Photoshop or Gimp; it blends background images to one another, as well as to whatever’s underneath.

The background-blend-mode takes familiar values such as overlay and multiply among a few others which Jonathan Cutrell does a fantastic job of explaining in his tutorial on the subject. I highly recommend you to read it through so we can jump into some practical examples.

There are several ways of using CSS Blend Mode to create striking designs, such as blending a gradient with an image, which Ian Yates points out in his inspiration roundup:

To create this effect, we add a background image and a gradient, and apply the overlay blend mode.

The overlay impacts both of the backgrounds listed here, so you might need to be cautious if you don’t want everything to blend all together. If we want to avoid everything blending with the background color, set the second value to normal which will apply to our second background image.

4. Background Clipping

The background-clip property is a utility which controls how the background color and the image span within the CSS content box model. Similar to the box-sizing property, the background-clip property takes three valid values, namely:

  • border-box is the default value which spans the background image or color all the way to the outer edge of the element.
  • padding-box will span the background up to the outer edge of the padding, or in other words; the inner edge of the border.
  • content-box will preserve the background within the element content as shown below:

One practical example where I’ve found background-clip to be handy is when I have to create a button with an icon, using a single element. In the following demo, our image spans from the left to right edge of the element, even if we add padding on each side, since it still applies border-box.

If we want to surround the icon with a bit of whitespace we would traditionally have to wrap it with another element and apply padding on that extra element. Using the background-clip property, we are able to do it elegantly by setting it to content-box, and substitute the padding with borders of the same color as the background color.

Wrapping Up

The background property is one we frequently use in our projects. Hopefully this article has reminded you of its wide and varied uses, and I look forward to hearing more ideas in the comments.

One final note: browser support for these properties (with the exception of background-blend-mode) are great. According to CanIUse, multiple backgrounds is supported from Internet Explorer 9 onwards, with just a couple of trivial issues. Background image options, such as the background-clip property, is almost as good.

Blending Modes, at the time of writing, work best in Chrome, Opera, are partially applicable in Safari due to a few bugs, but sadly seem to have made no sign of progress with Microsoft Edge.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.