CSS CSS Tutorial CSS Advanced CSS Responsive Web Design(RWD) CSS Grid CSS Properties Sass Tutorial Sass Functions



@import

Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document written in HTML or XML. CSS allows developers to separate the presentation of a document from its content, making it easier to maintain and update the design of a website. One of the features of CSS is the @import rule, which allows developers to import one CSS file into another.

The @import rule is used to include one CSS file into another. This is useful when you want to use styles from one file in another file without having to copy and paste the styles. The @import rule is also useful when you want to organize your CSS files into smaller, more manageable files.

The syntax for the @import rule is as follows:


@import url("style.css");

The @import rule is placed at the top of the CSS file and is followed by the URL of the file you want to import. The URL can be either an absolute or relative path to the file. If the file is in the same directory as the CSS file, you can use a relative path. If the file is in a different directory, you will need to use an absolute path.

Here is an example of how to use the @import rule:


/* main.css */
@import url("header.css");
@import url("footer.css");

/* header.css */
header {
  background-color: #333;
  color: #fff;
}

/* footer.css */
footer {
  background-color: #333;
  color: #fff;
}

In this example, we have three CSS files: main.css, header.css, and footer.css. The main.css file imports the header.css and footer.css files using the @import rule. The header.css and footer.css files contain styles for the header and footer of the website, respectively.

When the main.css file is loaded, the @import rule will load the header.css and footer.css files, and the styles in those files will be applied to the website.

The @import rule can also be used to import styles from external CSS files. For example, if you want to use the Bootstrap CSS framework in your website, you can import the Bootstrap CSS file using the @import rule:


@import url("https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css");

This will import the Bootstrap CSS file from the CDN and apply the styles to your website.

It is important to note that the @import rule can have a negative impact on the performance of your website. When you use the @import rule, the browser must make an additional HTTP request to load the imported file. This can slow down the loading time of your website, especially if you have multiple @import rules.

To avoid this performance issue, it is recommended to use the <link> element to include CSS files in your HTML document. The <link> element allows you to include multiple CSS files in a single HTTP request, which can improve the performance of your website.

Here is an example of how to use the <link> element to include CSS files:


<head>
  <link rel="stylesheet" href="main.css">
  <link rel="stylesheet" href="header.css">
  <link rel="stylesheet" href="footer.css">
</head>

In this example, we have three CSS files: main.css, header.css, and footer.css. The <link> element is used to include all three files in the HTML document. This will result in a single HTTP request to load all three files, which can improve the performance of the website.

In conclusion, the @import rule is a useful feature of CSS that allows developers to import one CSS file into another. This can help to organize CSS files into smaller, more manageable files and make it easier to reuse styles across multiple files. However, it is important to be aware of the potential performance issues associated with the @import rule and to use the <link> element to include CSS files in your HTML document whenever possible.

References

Activity