Open In App

What is the best way to include CSS file? Why use @import?

CSS property can be include in the HTML page in a number of different ways. HTML documents are formatted according to the information in the style sheet which is to be included.
There are many ways to include CSS file which are listed below:  

<link rel="stylesheet" href="style.css">
<style>
@import url(style.css);
</style>
<style>
element {
    // CSS property
}
</style>
<h1 style="style property">Geeksforgeeks</h1>

Best Approach: The External Style Sheet (using HTML <link> Tag) is the best method which is used to link the element. Maintaining and re-using the CSS file across different pages is easy and efficient. The <link> tag is placed in the HTML <head> element. To specify a media type=”text/css” for a Cascading Style Sheet <type> attribute which is used to ignore style sheet types that are not supported in a browser.
Example 1: The file given below contains CSS property. This file save with .css extension. For Ex: geeks.css 



body {
    background-color:powderblue;
}
.main {
    text-align:center;   
}
.GFG {
    color:#009900;
    font-size:50px;
    font-weight:bold;
}
#geeks {
    font-style:bold;
    font-size:20px;
} 




<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="geeks.css"/>
    </head>
    <body>
        <div class = "main">
        <div class ="GFG">GeeksForGeeks</div>
        <div id ="geeks">A computer science portal for geeks</p>
  
        </div>
    </body>
</html>    

Output:  



Example 2: This example describes the Internal or Embedded CSS.  




<!DOCTYPE html>
<html>
    <head>
        <title>Internal CSS</title>
        <style>
            .main {
                text-align:center;
            }
            .GFG {
                color:#009900;
                font-size:50px;
                font-weight:bold;
            }
            .geeks {
                font-style:bold;
                font-size:20px;
            }
        </style>
    </head>
    <body>
        <div class = "main">
        <div class ="GFG">GeeksForGeeks</div>
        <div class ="geeks">A computer science portal for geeks</p>
  
        </div>
    </body>
</html>                    

Output: 
 

Example 3: This example describes the inline CSS. 
 




<!DOCTYPE html>
<html>
    <head>
        <title>Inline CSS</title>
    </head>
     
    <body>
        <p style = "color:#009900;
                    font-size:50px;
                    font-style:italic;
                    text-align:center;">
        GeeksForGeeks</p>
  
    </body>
</html>                    

Output: 
 

@import rule: The @import rule is used to import one style sheet into another style sheet. This rule also support media queries so that the user can import the media-dependent style sheet. The @import rule must be declared at the top of the document after any @charset declaration. 
Characteristics of @import: 
 

Syntax: 
 

@import url|string list-of-mediaqueries;

Property values: 
 

Example 1: Consider the two CSS files as shown below. 
 

@import url("i1css.css");
h1 {
    color: #00ff00;
}
h1 {
   text-decoration: underline;
   font-size:60px;
}

p {
   padding-left: 20px;
   font-size: 60px;
}

 




<!DOCTYPE html>
<html>
<head>
    <title>WebPage</title>
    <link href="icss.css" rel="stylesheet">
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
     
<p>A computer science portal for geeks</p>
  
</body>
</html>                    

Output: 
 

Example 2: 
 




<!DOCTYPE html>
<html>
     
<head>
    <title>@import property</title>
     
    <style type="text/css">
        @import url(
    </style>
</head>
 
<body>
    <div id = "Geeks">
         
        <h1>GeeksforGeeks</h1>
         
        <h2>External style sheet (Using @import At-rule)</h2>
    </div>
</body>
 
</html>                   

Output: 
 

Supported Browser:


Article Tags :