Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherit/Extend CSS properties onto another element

What's the most efficient way in jQuery to extend/inherit CSS properties from one element to another?

Desired effect

$('#blah').extendCSSTo('#me') // Is there a plugin or a good way to do this?
$('#me').css({ background: 'blue' });

#me will now have all the CSS of #blah and also background

CSS:

#blah {
    padding: 5px;
    width: 200px;
    height: 20px;
    border: 1px solid #333;
    font-family: "Verdana";
}

#me {
    position: absolute;
    left: 5px;
    top: 5px;
}

<div id="blah"></div>
<div id="me">test</div>

Edit: This will be used in a plugin so using classes and simply doing .addClass is not an option

I'm looking for the same effect as setting default value for plugin options, but instead for CSS:

$.extend([css object], [#blah css object], [#me css object])
like image 707
Gary Green Avatar asked Jun 08 '11 10:06

Gary Green


People also ask

How do you inherit property in CSS?

The inherit CSS keyword causes the element to take the computed value of the property from its parent element. It can be applied to any CSS property, including the CSS shorthand property all . For inherited properties, this reinforces the default behavior, and is only needed to override another rule.

How do I extend a style in CSS?

In order to @extend the styling rules of a css class into another, you use the syntax @extend . classname; . This should be added to . element-b and .

Can we use @extend in CSS?

The @extend directive lets you share a set of CSS properties from one selector to another. The @extend directive is useful if you have almost identically styled elements that only differ in some small details.

Are all CSS properties inherited?

Only Certain Properties are Inherited The same is true in CSS; not every CSS property is inherited by default by child elements. In fact, if all properties were inherited, the effect would be similar to having no inheritance at all and you would have to write a lot of CSS to override this behavior.


1 Answers

$('#me').addClass('class');

This works, not sure if you can use the css relative to an ID though.

like image 200
José Valente Avatar answered Oct 07 '22 00:10

José Valente