Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get CSS class property in Javascript?

.test {     width:80px;     height:50px;     background-color:#808080;     margin:20px; } 

HTML -

<div class="test">Click Here</div> 

In JavaScript i want to get margin:20px

like image 636
Chinmay235 Avatar asked Dec 04 '13 14:12

Chinmay235


People also ask

Can we use CSS class in JavaScript?

In this article, we will see how to dynamically create a CSS class and apply to the element dynamically using JavaScript. To do that, first we create a class and assign it to HTML elements on which we want to apply CSS property. We can use className and classList property in JavaScript.

How do I retrieve the properties of a CSS element?

You can get the computed value of an element's CSS property by simply passing the property name as a parameter to the css() method. Here's the basic syntax: $(selector). css("propertyName");

Can we change CSS property using JavaScript?

With JavaScript, we are able to set CSS styles for one or multiple elements in the DOM, modify them, remove them or even change the whole stylesheet for all your page.


2 Answers

For modern browsers you can use getComputedStyle:

var elem,     style; elem = document.querySelector('.test'); style = getComputedStyle(elem); style.marginTop; //`20px` style.marginRight; //`20px` style.marginBottom; //`20px` style.marginLeft; //`20px` 

margin is a composite style, and not reliable cross-browser. Each of -top -right, -bottom, and -left should be accessed individually.

fiddle

like image 157
zzzzBov Avatar answered Sep 22 '22 06:09

zzzzBov


The accepted answer is the best way to get the computed values. I personally needed the pre-computed value. Say for instance 'height' being set to a 'calc()' value. I wrote the following jQuery function to access the value from the style sheet. This script handles nested 'media' and 'supports' queries, CORS errors, and should provide the final cascaded precomputed value for accessible properties.

$.fn.cssStyle = function() {  		var sheets = document.styleSheets, ret = [];  		var el = this.get(0);  		var q = function(rules){  			for (var r in rules) {  				var rule = rules[r];  				if(rule instanceof CSSMediaRule && window.matchMedia(rule.conditionText).matches){  					ret.concat(q(rule.rules || rule.cssRules));  				} else if(rule instanceof CSSSupportsRule){  					try{  						if(CSS.supports(rule.conditionText)){  							ret.concat(q(rule.rules || rule.cssRules));  						}  					} catch (e) {  						console.error(e);  					}  				} else if(rule instanceof CSSStyleRule){  					try{  						if(el.matches(rule.selectorText)){  							ret.push(rule.style);  						}  					} catch(e){  						console.error(e);  					}  				}  			}  		};  		for (var i in sheets) {  			try{  				q(sheets[i].rules || sheets[i].cssRules);  			} catch(e){  				console.error(e);  			}  		}  		return ret.pop();  	};        // Your element    console.log($('body').cssStyle().height);
like image 34
Bernesto Avatar answered Sep 24 '22 06:09

Bernesto