Get inline-style value even if element has the value also set in CSS?

If you have the following element:

<span></span> 

and the css is:

span {width:50px!important;} 

Is there a way I can get the inline-style value and ignore the css value?

Thanks

2

2 Answers

use element.style.width

sample:

$(function(){ alert($("span")[0].style.width); }); 
2

To find the applied style:

var span = document.querySelector('span[style]'), actualWidth = window.getComputedStyle(span, null).width; 

Note that this gets the style which won, which in this case will be the one with the !important modifier. If you must instead try and retrieve the in-line style, regardless of whether it was applied or not, you can simply use the style object of the element node:

inlineWidth = span.style.width; 

JS Fiddle demo.

Do remember that, for width to be applied to a span element, it must be either display: block or display: inline-block.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like