Get Css properties

Asked

Viewed 81 times

0

Someone knows how to identify, using javascript, only the css properties of a class, for example:

.in-text{
   color: red;
   font-weight: 500;
}

html

<label class="in-text">Digite um texto</label>

I tried this method, getComputedStyle(document.querySelector('.in-text'))however I get many properties and not only from the in-text class

  • This is complex, it’s easier to know which styles a given element has. You can better explain what you want to do or what you need to know about that class?

  • 2

    See if that’s what you need, http://jsfiddle.net/HP326/6/, reply https://stackoverflow.com/questions/2952667/find-all-css-rules-that-apply-to-an-element

  • @Sergio was studying the possibility of applying the rules of a css class inline in a style tag. Good to know that this is very complex.

  • 1

    @abfurlan vlw by link, is a good basis for my searches.

1 answer

0

You can use getComputedStyle() which returns an Object with all attributes that have been declared for the last element. And using getPropertyValue() you pass the name of the css attribute you want to access

var elemento = document.getElementById("algo");
window.getComputedStyle(elemento, null).getPropertyValue("margin-top")

If you want to know more about how this works, you can see the documentation on the link: https://developer.mozilla.org/en/docs/Web/API/Window/getComputedStyle

  • I was looking to find out only the rules of a CSS class, in this case I can’t use getPropertyValue, because I don’t have the properties.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.