- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Importing External Style Sheets in CSS
Import additional CSS files inside another CSS declaration. The @import rule is used for this purpose as it links a stylesheet in a document. This is generally used when one stylesheet is dependent upon another. It is specified at the top of the document after @charset declaration inside <head>. With that, the <link> element can also be used.
Syntax
The syntax of @import rule is as follows −
@import /*url or list-of-media-queries*/
The media queries can be compound statements which may specify the behaviour of the document in different media.
Import external stylesheets with @import rule
The following example implement the @import rule. Using the @import, we have included the style.css file −
@import url(style.css); body { background-color: honeydew; }
The @import rule is used for this purpose as it links a stylesheet in a document. The style file includes the styles for this HTML document. Let us see the example −
Example
<!DOCTYPE html> <html> <head> <style type="text/css"> @import url(style.css); body { background-color: honeydew; } </style> </head> <body> <p>This is demo paragraph one.</p> <p class="two">This is demo paragraph two.</p> <p>This is demo paragraph three</p> </body> </html>
style.css
p { color: navy; font-style: italic; } .two { color: darkgreen; font-size: 24px; }
Include external stylesheet with the element
Let us see another example to import external stylesheet using the <link> element −
HTML document
<!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div></div> </body> </html>
style.css
div { height: 50px; width: 100px; border-radius: 20%; border: 2px solid blueviolet; box-shadow: 22px 12px 3px 3px lightblue; position: absolute; left: 30%; top: 20px; }