10

I have problem with getting inline css style properties.

I'd tried doing that with:

 var inline_css = $(this).attr("style");

but...

it works only when element does not have any other css properties outside inline style... like:

.our_element {something: some;}

Any ideas how to get only inline CSS properties from element which has many other CSS properties?

5
  • How does the HTML look like? You may want to use this.style. Commented Oct 2, 2015 at 9:59
  • for example: <h1 style="font-size: 33px;">This is a test2</h1> Commented Oct 2, 2015 at 10:00
  • Possible duplicate of Get computed font size for DOM element in JS Commented Oct 2, 2015 at 10:01
  • I don't understand if you want only inline CSS property or all??? Commented Oct 2, 2015 at 10:04
  • 1
    @IonicăBizău: Pretty sure it's exactly not that question. Commented Oct 2, 2015 at 10:10

3 Answers 3

28

If you mean a style from the style attribute, you can access them directly on the element instance:

var color = this.style.color;

That will give you the color only if it's in the style attribute (not applied via stylesheet).

The names you use are camelCase if you use literal notation, e.g. this.style.fontSize, or you can also use the CSS dashed style using brackets notation, this.style["font-size"].

Just for completeness, if you want the information whether it comes from the style attribute or a stylesheet, jQuery's CSS function does just that:

var color = $(this).css("color");

From your comment:

thanks, but if I want all properties can I use this.style ??

If you want all of the inline-styles as text, either get the style attribute (as you're doing) or use this.style.cssText.

If you want all of the styles, regardless of whether they're inline or not, as an object, use getComputedStyle (or currentStyle on obsolete browsers like IE8):

var style = window.getComputedStyle ? getComputedStyle(this) : this.currentStyle;
if (style) { // This will be true on major browsers
    var color = style.color; // Or whatever
}

Examples:

var div = document.querySelector(".foo");
snippet.log("div.style.fontSize: " + div.style.fontSize);
snippet.log("div.style.color: " + div.style.color);
snippet.log("div.style.cssText: " + div.style.cssText);
snippet.log("$(div).attr('style'): " + $(div).attr('style'));
snippet.log("$(div).css('fontSize'): " + $(div).css('fontSize') + " (note that's in pixels, probably, not pt)");
snippet.log("$(div).css('color'): " + $(div).css('color'));
.foo {
  font-size: 14pt;
  color: green;
}
<div class="foo" style="font-size: 12pt">
  This has an inline <code>font-size: 12pt</code> and
  CSS (not inline) giving <code>font-size: 14pt</code> and <code>color: green</code>.
</div>
<hr>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Sign up to request clarification or add additional context in comments.

7 Comments

thanks, but if I want all properties can I use this.style ??
@luke9999: I don't understand the question. In your question, you said you wanted to get one of them.
sorry then, I mean all inline properties... ;/
@luke9999 So what's wrong with $(this).attr("style");??? Do you mean these properties could be overwritten by other rules or what? Can you provide concrete sample replicating your issue?
it works but its getting properties from file not inline style ;/
|
0

As old as this is, the actual question was on getting all computed styles on a given element.

What you were actually wanting to use it the Window.getComputedStyle() method.

With browser support going back, further than the original question.

Example:

var ourElement = document.querySelector(".ourElement");
var ourElementStyles = window.getComputedStyle(ourElement);

ourElement.textContent += ourElementStyles.color;
.ourElement {
  color: rgb(0, 255, 0) !important;
}
<div class="ourElement" style="color:rgb(255,0,0)">color: </div>

Comments

0

Even if it's an old post but this is the answer that can be helpful for others. You can try the below code -

HTML code like -

<p style="color: red; height: 300px">

jQuery code -

let height = $('p').get(0).style.height;
let color = $('p').get(0).style.color;

Output below -

let height = $('p').get(0).style.height;
let color = $('p').get(0).style.color;

$('#output').html( 'Color: ' + color + '<br> Height: ' + height );
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p style="color: red; height: 300px">
<div id="output"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.