I'm wondering of there is any way to find out where an inline CSS style came from. As you can see in the picture below, I have an element with an inline style that was generated using JavaScript. Sometimes my code seems to break and put the width to 0px, rendering the div invisible.
I've looked through all the JS files, but can't seem to find the error.
Is there a way to find the right file and line, just like dev tools does for css files?
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.
Inline styles are applied directly to the specific HTML element using the style attribute. In JavaScript the style property is used to get or set the inline style of an element. The following example will set the color and font properties of an element with id="intro" .
Inline Style Syntax The style attribute is just like any other HTML attribute. It goes inside the element's beginning tag, right after the tag name.
There are two other ways to set style using JavaScript. The first is to use the setAttribute() method. The second is by adding a class using the add() method of the classList property. The class you add can then be handled using external CSS styling.
Like most other attributes, you can get and set the inline style of an element in the DOM with the style property. However, unlike most other attributes, the style property returns an object containing a list of CSS style properties. You need to sign up for Treehouse in order to download course files.
To get all styles applied to an element, you should use the window.getComputedStyle () method. Use the properties of element.style object to set the inline CSS properties for the HTML element. Was this tutorial helpful ?
Or you can use the setAttribute () method: Once setting the inline style, you can modify one or more CSS properties: If you do not want to completely overwrite the existing CSS properties, you can concatenate the new CSS property to the cssText as follows: In this case, the += operator appends the new style string to the existing one.
The style property returns the read-only CSSStyleDeclaration object that contains a list of CSS properties. For example, to set the color of an element to red, you use the following code: If the CSS property contains hyphens ( -) for example -webkit-text-stroke you can use the array-like notation ( []) to access the property:
Since you are using Chrome:
When the inline style of the element is modified with JS, the debugger will trigger as if it had hit a breakpoint.
This will show you the relevant line of JS and give you a stack so you can figure out where that line was called from.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With