Hướng dẫn javascript load css

View Discussion

Show

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The CSS file is used to describe how HTML elements will be displayed. There are various ways to add CSS file in the HTML document. JavaScript can also be used to load a CSS file in the HTML document. 

    Approach:

    • Use document.getElementsByTagName() method to get HTML head element.
    • Create new link element using createElement(‘link’) method.
    • Initialize the attributes of link element.
    • Append link element to the head.

    Example 1: This example uses JavaScript to add CSS file in HTML document.

    • Create CSS file using name style.css: 

    html

    • Use JavaScript to add CSS file: 

    javascript

        </code></p><p><code>        </code><code>Load CSS file using JavaScript</code></p><p><code>    </code><code>

        

        

    GeeksForGeeks

                       

    Output:

     

    Hướng dẫn javascript load css
     

    Example 2: This example uses JavaScript to add CSS file in HTML document.

    • Create CSS file using name style.css: 

    html

    .GFG {

        font-size:24px;

        font-weight:bold;

        color:white;

        background-color:green;

        padding:10px;

        text-align:center;

    }

    • Use JavaScript to add CSS file: 

    javascript

        </code></p><p><code>        </code><code>Load CSS file using JavaScript</code></p><div style="width:100%; margin:20px auto; display:block"> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-4987931798153631" data-ad-slot="8587332220"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div></p><p><code>    </code><code>

        

        

    GeeksforGeeks

                       

    Output: 

    Hướng dẫn javascript load css

    CSS is the foundation of webpages, is used for webpage development by styling websites and web apps. JavaScript is best known for web page development but it is also used in a variety of non-browser environments.You can learn more about CSS and Javascript from the links given below:

    • CSS Tutorial and CSS Examples
    • JavaScript Tutorial and JavaScript Examples