Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if a particular CSS property is inherited with jQuery?

Tags:

jquery

css

This is a very simple question, so I'll keep it really brief:

How can I tell if a particular DOM element's CSS property is inherited?

Reason why I'm asking is because with jQuery's css method it will return the computed style, which inherits the parent object's CSS properties. Is there a way to retrieve the properties set on the object itself?

An example might explain what I'm getting at a bit better:

CSS:

div#container {
  color:#fff;
}

HTML:

<div id="container">
  Something that should be interesting
  <div class="black">
    Some other thing that should be interesting
  </div>
</div>

So, in the instance of div.black, which inherits color, how can I tell if it is inherited?

$('div.black:eq(0)').css('color') will obviously give me #fff, but I want to retrieve the style of the element itself, not its parents.

like image 656
Jacob Relkin Avatar asked Feb 15 '11 05:02

Jacob Relkin


People also ask

How can check CSS property value in jQuery?

Get a CSS Property Value You can get the computed value of an element's CSS property by simply passing the property name as a parameter to the css() method. Here's the basic syntax: $(selector). css("propertyName");

What CSS properties are not inherited?

CSS properties such as height , width , border , margin , padding , etc. are not inherited.


3 Answers

To actually determine whether a css style was inherited or set on the element itself, you would have to implement the exact rules that the browsers apply to determine the used value for a particular style on an element.

You would have to implement this spec that specifies how the computed and used values are calculated.

CSS selectors may not always follow a parent-child relationship which could have simplified matters. Consider this CSS.

body {     color: red; }  div + div {     color: red; } 

and the following HTML:

<body>     <div>first</div>     <div>second</div> </body> 

Both first and second wil show up in red, however, the first div is red because it inherits it from the parent. The second div is red because the div + div CSS rule applies to it, which is more specific. Looking at the parent, we would see it has the same color, but that's not where the second div is getting it from.

Browsers don't expose any of the internal calculations, except the getComputedStyle interface.

A simple, but flawed solution would be to run through each selector from the stylesheets, and check if a given element satisfies the selector. If yes, then assume that style was applied directly on the element. Say you wanted to go through each style in the first stylesheet,

var myElement = $('..'); var rules = document.styleSheets[0].cssRules; for(var i = 0; i < rules.length; i++) {     if (myElement.is(rules[i].selectorText)) {         console.log('style was directly applied to the element');     } } 
like image 78
Anurag Avatar answered Sep 19 '22 06:09

Anurag


I don't think you can tell if the given style is inherited, I think the best you can do is to set the given CSS property to "inherit", capture its computed value, and compare it to the original value. If they are different, the style is definitely not inherited.

var el = $('div.black:eq(0)');
var prop = el.css("color");
el.css("color", "inherit");
var prop2 = el.css("color");
el.css("color", prop);
if(prop != prop2)
  alert("Color is not inherited.");

Demo on jsFiddle

The point is this: If you set div.black to #fff in the CSS or via inline style, this method will consider that to be inherited. Not ideal, in my opinion, but it may suit your needs. I'm afraid a perfect solution requires traversal of the entire stylesheet.

like image 24
Josh Stodola Avatar answered Sep 21 '22 06:09

Josh Stodola


http://jsfiddle.net/k7Dw6/

var $black = $('div.black:eq(0)');
alert($('<div>').attr('class', $black.attr('class')).css('line-height') === $black.css('line-height'));

You could create a new element with the same class (and ID I guess) and check if the CSS property is the same or not.

like image 22
mpen Avatar answered Sep 22 '22 06:09

mpen