Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Additive properties in CSS

Tags:

css

I know in vanilla CSS, there is no way to create "additive" properties. What I mean by that is:

.shade {
    background: rgba(0,0,0,.1);
}

<div class="shade">I have .1 black background</div>
<div class="shade shade shade">I also have .1 black background, but wouldn't it be cool if i had .3?</div>

What I hope to do is to avoid having to generate styles in a loop in order to multiply the opacity of a background shade and without having to specify a class for each one, because i don't know how many there are.

I suspect this isnt possible because it kind of defeats the purpose of the "C" in "CSS", and that's fine - but figured I'd ask in case somebody smarter than me knew of a way.

And I'd rather not do:

<div class="shade">
   <div class="shade">
     <div class="shade">
       No please
     </div>
   </div>
</div> 
like image 228
oooyaya Avatar asked Jan 08 '14 15:01

oooyaya


People also ask

What are CSS properties?

A CSS property is a characteristic (like color) whose associated value defines one aspect of how the browser should display the element.

How many CSS properties are there?

W3Schools lists 228 of them.

What is all unset in CSS?

The unset CSS keyword resets a property to its inherited value if the property naturally inherits from its parent, and to its initial value if not.

What is the use of property in a CSS value?

The used value of a CSS property is its value after all calculations have been performed on the computed value. After the user agent has finished its calculations, every CSS property has a used value. The used values of dimensions (e.g., width , line-height ) are in pixels.


1 Answers

You can do it with JQuery Working example

$("div").each(function(index){
    var classList = $(this).attr('class').split(/\s+/);
    var num = 0;
    $.each( classList, function(index, item){
        if (item === 'shade') {
            num = num + 0.1;
        }
    })
    $(this).css("background", "rgba(0,0,0," + num + ")");
 });

HTML:

<div class="shade">I have .1 black background</div>
<div class="shade shade other shade">I also have .1 black background, but wouldn't it be cool if i had .3?</div>
<div class="shade shade shade shade shade">0.5 opacity</div>
like image 133
Itay Gal Avatar answered Oct 13 '22 20:10

Itay Gal