Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can one detect if an element's style is just the browser default vs. is set by a stylesheet or inline styles?

My javascript is included in X's site, but I don't have any other control over her site or where she includes it. If she's styled #element, I want to leave it alone, but if she hasn't, I've got a stylesheet I'll inject in <head>. Is there any way to detect whether or not she's styled it?

Checking it's height is 0 or 1 fails because it's got some content in it and the browser makes default decisions.

Any way for javascript/jquery/other framework to tell the CSS specificity of a style would answer this and be incredible.

like image 734
John Baber-Lucero Avatar asked Apr 09 '13 00:04

John Baber-Lucero


People also ask

How do you know where an element style is coming from?

In Chrome, if you right click on an element and "inspect," then view the styles in the "Computed" tab then you should see what styles are affecting the element.

Does inline style sheet affect all elements of the page?

This rule will affect every p on the page, whereas the inline style will affect only the <p> it's written in. Cascading works the same way, however, so the <strong> element inside the <p> will be blue with big text regardless of whether you decide to use inline styling or CSS rules.

How does an element get its default styles CSS?

CSS Inheritance This behavior means that a style applied to an element is inherited by its descendent elements. By default, the primary properties that have this behavior are text ones, such as font-family , font-size , color , text-align , and other typography properties.

Is it possible to set different styles for the same type of HTML element?

It is possible to specify several alternate styles using multiple Link headers, and then use the rel attribute to determine the default style. In the following example, "compact" is applied by default since it omits the "alternate" keyword for the rel attribute.


1 Answers

For inline style, it's quite easy: you can just access to element.style object and check the property you want to – or use element.hasAttribute('style') to check if the attribute is defined in the HTML.

For CSS rules applied by stylesheet, I would suggest to you to take a look at "Is it possible to check if certain CSS properties are defined inside the style tag with Javascript?" that it's quite similar from what are you asking.

Using the approach described there, is also quite easy filtered the rules that have only the #element selector in, if you want to filter out any generic rule (like tag ones).

like image 191
ZER0 Avatar answered Oct 20 '22 01:10

ZER0