DEV Community

Cover image for CSS Multiple Backgrounds
joan?
joan?

Posted on

CSS Multiple Backgrounds

One of the lesser-known but incredibly useful feature of CSS is the ability to apply multiple backgrounds to an element. This powerful feature allows you to create complex and visually appealing web pages with ease. In this article, we will dive deep into CSS multiple backgrounds, exploring how to add them, use multiple background colors, combine background images and control image repetition.

How to Add Multiple Backgrounds in CSS?

To add multiple backgrounds to an HTML element using CSS, you can use the background property. This property can accept multiple values, each separated by a comma. Each value corresponds to a distinct background layer. Let's take a look at a simple example:

.element {
  background: 
    url('background-image1.jpg') no-repeat center center, /* Layer 1 */
    url('background-image2.jpg') no-repeat top left,     /* Layer 2 */
    #f2f2f2;                                             /* Layer 3 */
}
Enter fullscreen mode Exit fullscreen mode

In the code above, we have applied three background layers to the .element class. Here's a breakdown of each layer:

  1. Layer 1: This layer uses an image background-image1.jpg centered both horizontally and vertically. The no-repeat property ensures that the image does not repeat.

  2. Layer 2: This layer uses another image background-image2.jpg, positioned at the top left corner. Again, no-repeat prevents repetition.

  3. Layer 3: This layer is a solid background color #f2f2f2.

By using this approach, you can combine background images and colors to create visually rich elements.

How Do I Get Multiple Background Colors in CSS?

Adding multiple background colors in CSS is straightforward. You can define each color as a separate background layer using the background property. Here's an example:

.element {
  background:
    #ff5733, /* Layer 1 - Red */
    #44b7b8; /* Layer 2 - Blue */
}
Enter fullscreen mode Exit fullscreen mode

In this example, we have applied two background layers to the .element class, each with a distinct color. Layer 1 is red, and Layer 2 is blue. You can adjust the colors and their order as needed to achieve the desired visual effect.

How Do I Add Multiple Background Images in HTML CSS?

To add multiple background images in HTML and CSS, you can use the background property with image URLs, just like we did in the previous example. Here's a more detailed example:

.element {
  background:
    url('background-image1.jpg') no-repeat center center, /* Layer 1 */
    url('background-image2.jpg') no-repeat top left;     /* Layer 2 */
}
Enter fullscreen mode Exit fullscreen mode

In this code, we have applied two background images to the .element class. Layer 1 uses background-image1.jpg, centered both horizontally and vertically with no repetition. Layer 2 uses background-image2.jpg, positioned at the top left corner with no repetition.

You can add as many background images as needed, and CSS will render them in the specified order, creating a layered effect.

Can Background Images Be Repeated in CSS?

Yes, background images can be repeated in CSS, but you can control the repetition behavior using the background-repeat property. By default, background images repeat both vertically and horizontally. However, you can change this behavior to create various effects.

Here are the possible values for the background-repeat property:

  • repeat: This is the default behavior and causes the background image to repeat both vertically and horizontally.
  • repeat-x: The background image repeats only horizontally.
  • repeat-y: The background image repeats only vertically.
  • no-repeat: The background image does not repeat in either direction.

Let's illustrate this with an example:

.element {
  background:
    url('background-image.jpg') repeat,     /* Layer 1 - Repeats both ways */
    url('pattern-image.jpg') repeat-x;     /* Layer 2 - Repeats horizontally only */
}
Enter fullscreen mode Exit fullscreen mode

In this code, Layer 1 uses background-image.jpg with repeat, causing it to repeat both vertically and horizontally. Layer 2 uses pattern-image.jpg with repeat-x, making it repeat horizontally but not vertically.

By controlling background image repetition, you can achieve precise design results.

Combining Multiple Backgrounds for Creative Design

One of the most exciting aspects of CSS multiple backgrounds is the ability to combine them creatively to achieve unique design effects. By adjusting the order, positioning, and repetition of background layers, you can create visually stunning elements.

Let's explore a few creative examples:

Example 1: Overlay Text on an Image

.element {
  background:
    url('background-image.jpg'),   /* Layer 1 - Background image */
    rgba(0, 0, 0, 0.5);            /* Layer 2 - Semi-transparent overlay */
  color: #fff;                     /* Text color */
  padding: 20px;
}
Enter fullscreen mode Exit fullscreen mode

In this example, Layer 1 is a background image, and Layer 2 is a semi-transparent overlay. This combination allows you to display text on top of the image while maintaining readability.

Example 2: Creating a Complex Pattern

.element {
  background:
    url('pattern-image.jpg') repeat,   /* Layer 1 - Repeating pattern */
    url('border-image.png') no-repeat center center; /* Layer 2 - Centered border */
  border: 5px solid #333;             /* Border style */
  padding: 20px;
}
Enter fullscreen mode Exit fullscreen mode

Here, Layer 1 uses a repeating pattern, creating a textured background. Layer 2 is a centered border image that adds a decorative element to the design.

Example 3: Parallax Scrolling Effect

.element {
  background:
    url('background-image.jpg') no-repeat center center fixed, /* Layer 1 - Fixed background */
    linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.8));    /* Layer 2 - Gradient overlay */
  color: #fff;                      /* Text color */
  padding: 100px;
  text-align: center;
}
Enter fullscreen mode Exit fullscreen mode

In this example, Layer 1 uses a fixed background image to create a parallax scrolling effect. Layer 2 is a gradient overlay that adds depth and enhances readability.

Conclusion

CSS multiple backgrounds are a powerful tool for web designers and developers. By combining background colors and images, controlling repetition, and experimenting with layering, you can create visually appealing and dynamic web elements. Understanding how to use multiple backgrounds effectively can take your web design skills to the next level, allowing you to create stunning and engaging user experiences.

Connect with me on twitter

Top comments (1)

Collapse
 
diomed profile image
May Kittens Devour Your Soul

with 0 real life examples. on how it looks. wow

like you told us about The Son of man but you never showed the painting.