How to return a javascript set style property to CSS default

I’m trying to work out how, after changing style properties with javascript, I can revert to the value in the stylesheet (including the units).

In the example below, I’d like the output to read 100px (the value in the CSS), rather than 10px, as getComputedStyle gives.

I’d also keep the dummy div at top:25px, so removing the style property won’t work.

The best I have is cloning the node and reading the height and storing in a property (, but this is not really getting the browser’s default css value (especially if this is set in ems).

   position: absolute;
   left:     100px;
   top:      200px;
   height:   100px;

<div id="elem-container">dummy</div>
<div id="output"></div>  

  function getTheStyle(){
    var elem = document.getElementById("elem-container"); = "10px"; = "25px";
    var theCSSprop = window.getComputedStyle(elem,null).getPropertyValue("left");
    document.getElementById("output").innerHTML = theCSSprop;

Just clear the inline style you wish to fallback to original stylesheet on. = null;

The style object has a built-in removeProperty() method, so you could do something like:'left');

As far as I know, this will have exactly the same effect as setting the property to null, as abaelter suggested. I just thought it might be worth including for the sake of completeness.

Combining abaelter’s answer and gives us the below function:

var getCssStyle = function(elementId, cssProperty) {
  var elem = document.getElementById(elementId);
  var inlineCssValue =[cssProperty];

  // If the inline style exists remove it, so we have access to the original CSS
  if (inlineCssValue !== "") {[cssProperty] = null;

  var cssValue = "";
  // For most browsers
  if (document.defaultView && document.defaultView.getComputedStyle) {
    cssValue = document.defaultView.getComputedStyle(elem, "").getPropertyValue(cssProperty);
  // For IE except 5
  else if (elem.currentStyle){
    cssProperty = cssProperty.replace(/\-(\w)/g, function (strMatch, p1) {
      return p1.toUpperCase();
    cssValue = elem.currentStyle[cssProperty];

  // Put the inline style back if it had one originally
  if (inlineCssValue !== "") {[cssProperty] = inlineCssValue;

  return cssValue;

Placing in your example code and testing:

console.log("getCssStyle: " + getCssStyle("elem-container", "left"));

Gives us getCssStyle: 100px allowing you to see the original CSS value. If you just want to revert the value then do as abaelter says and null the CSS value you want to revert.

Read More:   How to pass multiple checkboxes using jQuery ajax post

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts