Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to append a CSS3 background with additional classes / styles?

Tags:

css

background

Today I learned CSS3 supports multiple backgrounds, which is awesome. What I'd really like is the ability to combine multiple backgrounds on the fly, EG:

.Watermarked{
  background: url('text/logo.png') bottom right no-repeat;
}

HTML:

<div class="Watermarked" 
     style="background:url('photos/very_pretty_sunset.jpg') 0 0 no-repeat;">
...

Somehow produces the computed style:

 background: url('text/logo.png') bottom right no-repeat,
             url('photos/very_pretty_sunset.jpg') 0 0 no-repeat;    

Of course I can hard code the extra background style or add it using jquery. I am seeking that sweet, pure, CSS-only solution.

Answered

Accepted thirtydot for the pure CSS answer- "You can't".

It's worth highlighting that if you're working with SASS (Rails 3.1, etc), the hard-coded styles are more palatable through variable use:

$watermarkImage: url('text/logo.png') left top no-repeat;
...
#very_pretty_sunset{
  background: $watermarkImage, url('photos/very_pretty_sunset.png') right bottom no-repeat;
}
like image 244
RSG Avatar asked Jun 21 '11 22:06

RSG


People also ask

Does CSS3 allows you to use several background images for an element?

CSS allows you to add multiple background images for an element, through the background-image property. The different background images are separated by commas, and the images are stacked on top of each other, where the first image is closest to the viewer.

What are the new background added in CSS3?

In CSS3, we can also use three entirely new background properties: background-origin , background-clip and background-size .

How do you overlap a background image in CSS?

In short, CSS overlay effects are achieved by using the following: background-image and background CSS properties to add image and linear-gradient overlay effect. position:absolute , top , bottom , right , left CSS properties to control the position of overlay image or text.


1 Answers

You can't do that with CSS.

Whichever background is declared with higher specificity will completely override the other one.

like image 148
thirtydot Avatar answered Sep 27 '22 17:09

thirtydot