Skip to content

Instantly share code, notes, and snippets.

@JARVIS-AI
Forked from tzi/important.css
Created March 15, 2021 09:48
Show Gist options
  • Save JARVIS-AI/b04bd7da23fe0ec1f0f69520d3e682c3 to your computer and use it in GitHub Desktop.
Save JARVIS-AI/b04bd7da23fe0ec1f0f69520d3e682c3 to your computer and use it in GitHub Desktop.

Revisions

  1. @tzi tzi revised this gist Jun 19, 2012. 2 changed files with 7 additions and 2 deletions.
    4 changes: 4 additions & 0 deletions important.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    .test {
    height: 139px;
    width: 96px
    }
    5 changes: 3 additions & 2 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,12 @@
    <!DOCTYPE html>
    <html>
    <head>
    <title>How to set an !important css property in javascript</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>How to set an !important css property in javascript</title>
    <link href="important.css" media="screen" rel="stylesheet" type="text/css" />
    </head>
    <body>
    <div>Miaaou</div>
    <div class="test">Miaaou</div>
    <script type="text/javascript" src="important.js"></script>
    <body>
    </html>
  2. @tzi tzi revised this gist Jun 19, 2012. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    If you want to try it, go [here](http://bl.ocks.org/2953155)

    To set a CSS rule as an !important one in javascript, you have to use the element.setAttribute() method.
    To set a CSS inline style as an !important one in javascript, you have to use the element.setAttribute() method.

    But you can't use this one in old IE to set style. There is a specific syntax ;)
  3. @tzi tzi revised this gist Jun 19, 2012. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    If you want to try it, go [here](http://bl.ocks.org/2953155)

    To set a CSS rule as an !important one in javascript, you have to use the element.setAttribute() method.
    But you can't use this one in old IE to set style. There is a specific syntax ;)
  4. @tzi tzi revised this gist Jun 19, 2012. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion important.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,13 @@
    (function(){
    var elements = document.getElementsByTagName( 'div' );
    element = elements[ 0 ];
    element.setAttribute( 'style', 'background-image: url( "http://placekitten.com.s3.amazonaws.com/homepage-samples/96/139.jpg" )' );

    // Specific old IE
    if ( document.all ) {
    element.style.setAttribute( 'cssText', 'background-image: url( "http://placekitten.com.s3.amazonaws.com/homepage-samples/96/139.jpg" ) !important' );

    // Modern browser
    } else {
    element.setAttribute( 'style', 'background-image: url( "http://placekitten.com.s3.amazonaws.com/homepage-samples/96/139.jpg" ) !important' );
    }
    })();
  5. @tzi tzi revised this gist Jun 19, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion important.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    (function(){
    var elements = document.getElementsByTagName( 'div' );
    element = elements[ 0 ];
    element.setAttribute( 'style', 'background-image: url( "http://placekitten.com.s3.amazonaws.com/homepage-samples/96/139.jpg" ) !important' );
    element.setAttribute( 'style', 'background-image: url( "http://placekitten.com.s3.amazonaws.com/homepage-samples/96/139.jpg" )' );
    })();
  6. @tzi tzi revised this gist Jun 19, 2012. 2 changed files with 4 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions important.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    (function(){
    var target = createElement( '<a class="target" href="javascript:;" title="Share this at Diaspora*">Miaaou</a>' );
    target.setAttribute( 'style', 'background-image: url( "http://placekitten.com.s3.amazonaws.com/homepage-samples/96/139.jpg" ) !important' );
    var elements = document.getElementsByTagName( 'div' );
    element = elements[ 0 ];
    element.setAttribute( 'style', 'background-image: url( "http://placekitten.com.s3.amazonaws.com/homepage-samples/96/139.jpg" ) !important' );
    })();
    1 change: 1 addition & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
    <div>Miaaou</div>
    <script type="text/javascript" src="important.js"></script>
    <body>
    </html>
  7. @tzi tzi revised this gist Jun 19, 2012. 2 changed files with 3 additions and 3 deletions.
    4 changes: 2 additions & 2 deletions important.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    (function(){
    var target = createElement( '<a class="target" href="javascript:;" title="Share this at Diaspora*">How to set a background image (important) in javascript</a>' );
    var target = createElement( '<a class="target" href="javascript:;" title="Share this at Diaspora*">Miaaou</a>' );
    target.setAttribute( 'style', 'background-image: url( "http://placekitten.com.s3.amazonaws.com/homepage-samples/96/139.jpg" ) !important' );
    }
    })();
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <!DOCTYPE html>
    <html>
    <head>
    <title>Test navigator</title>
    <title>How to set an !important css property in javascript</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
  8. @tzi tzi revised this gist Jun 19, 2012. 3 changed files with 5 additions and 6 deletions.
    5 changes: 0 additions & 5 deletions bg-image.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +0,0 @@
    (function()
    var target = createElement( '<a class="target" href="javascript:;" title="Share this at Diaspora*">Diaspora Share Button</a>' );;
    if (noDataURI == 1) {
    target.setAttribute( 'background-image', 'url( ' + imagesPath + 'diaspora-share-button.png ) !important' );
    }
    4 changes: 4 additions & 0 deletions important.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    (function(){
    var target = createElement( '<a class="target" href="javascript:;" title="Share this at Diaspora*">How to set a background image (important) in javascript</a>' );
    target.setAttribute( 'style', 'background-image: url( "http://placekitten.com.s3.amazonaws.com/homepage-samples/96/139.jpg" ) !important' );
    }
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,6 @@
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
    <script type="text/javascript" src="bg-image.js"></script>
    <script type="text/javascript" src="important.js"></script>
    <body>
    </html>
  9. @tzi tzi created this gist Jun 19, 2012.
    5 changes: 5 additions & 0 deletions bg-image.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    (function()
    var target = createElement( '<a class="target" href="javascript:;" title="Share this at Diaspora*">Diaspora Share Button</a>' );;
    if (noDataURI == 1) {
    target.setAttribute( 'background-image', 'url( ' + imagesPath + 'diaspora-share-button.png ) !important' );
    }
    10 changes: 10 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    <!DOCTYPE html>
    <html>
    <head>
    <title>Test navigator</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
    <script type="text/javascript" src="bg-image.js"></script>
    <body>
    </html>