What is CSS Import?
- CSS (Cascading Style Sheets) are the design backbone for the majority of webpages. It specifies how the items should be shown on the screen. This is where the CSS @import at-rule comes in.
- The @import rule directs the CSS engine to import an external style sheet into another style sheet. This enables the addition of style rules from one style sheet to another. This rule can be combined with media queries to import a style sheet dependent on the type of device.
- The @import style rule is useful since it can be used to avoid duplicating style rules and even import specific styles dependent on the device that is accessed. The @import rule must be placed at the very top of the style sheet.
- The issue is not the complexity of the language or the difficulty of coding. The issue is the large volume of information required to develop even a small website. When you consider the amount of HTML required to develop even simple design features and then extend that out to several pages with designs and content on each page, the effort might seem intimidating.
- This large amount of information can also be difficult to keep track of. If you wish to copy and paste specific rules, you must first locate them. Even if your CSS is well-organized, the sheer size of the website and the amount of content can be extremely confusing. The @import rule is an excellent method for organizing a large number of style rules.
Syntax of CSS import
Parameter | Description |
---|---|
url|string | The location of the resource to be imported is represented by a URL or a string. The URL might be either relative or absolute. |
list-of-mediaqueries | The list of media queries defines how the CSS rules given in the linked URL are applied. |
The positives of CSS @import
The most advantageous aspect of CSS @import is that it saves your time and frustration. If you don't use @import, you're either using old CSS to copy and paste the information you need or handling all style rules in a single style sheet. As your website increases in size and the code becomes more difficult to find, this can be time-consuming, frustrating, and even difficult. Even worse, having redundant styles results in a larger style sheet, which might reduce website performance.
CSS @import not only saves time but also delivers excellent accuracy. Rather than potentially establishing bad style rules or copying and pasting incorrect information, simply import the existing rules. This is straightforward and precise, especially when compared to the alternatives. It can also be used to segregate your style rules into different style sheets, for example:
The negatives of CSS @import
The only negative of using the @import declaration is that it increases page load time if it is used during the build process. It reads the imports and then applies them as it proceeds. Although the time difference is minor, it might have an impact on your search results because bots use page loading time to calculate ranking.
The Alternatives to CSS @import
a) Links: Using a tag to connect your CSS files to the header of your HTML allows CSS files to be downloaded rather than invoked. This means that the browser does not need to navigate to another style sheet to import it; instead, it can be downloaded right away. Linking has the advantage of not increasing loading times, but it also has certain disadvantages. The most important is that if you want to link to your CSS file, you must know where it is. If you don't, and your website is huge and complex, it could take a long time to find that link.
b) Rewriting the same style rules: This is also one option for merging, but it is not a good alternative. Rewriting the same content on different pages would be too much to handle and will be inefficient. While it is possible and could be done effectively, the risk of error and the huge amount of time required make this meaningless.
c) Copy and paste: Another method for merging data is to just copy and paste. If you've ever developed a website, you'll know that there's a lot of copying and even more pasting. However, when style sheets, especially long ones, are being copied and pasted, things can get complicated. For one thing, if you misspell a letter or add too many spaces, you can easily mess up the existing code. You must also know where to go on an old CSS for finding the information you want to copy.
The Difference Between Relative URL and Absolute URL
Absolute and relative URLs differ in six ways for SEO, web security, web page loading performance, and user privacy. The primary differences between Absolute and Relative URLs are discussed below.
Parameters | Relative URL | Absolute URL |
---|---|---|
URL Length | Relative URLs are shorter than Absolute URLs. | They are longer in comparison to Relative URLs. |
Informativeness | As relative URLs are shorter they provide less information. | They provide more information in comparison to relative URLs. |
URL Resolution speed | Absolute and relative URLs resolve at varying rates. Relative URLs are easier to resolve than Absolute URLs, thus better for the page speed. | Absolute URLs take a long time to resolve because they are longer. |
HTML Size | Each character is 8 KB in size. Relative URLs are shorter and lighter, thus easier to read while coding and during web development. | They are longer and heavier than relative URLs. |
Web Security | Protocol-relative URLs are bad for web security and user privacy, thus it's recommended to use absolute URLs or avoid using protocol-relative URLs entirely. | They are more secure than protocol-relative URLs. |
Technical SEO | Only a subset of technical SEO tags can use relative URLs. | All technical SEO tags use absolute URLs. |
Relative URL
- Relative URLs contain only the relatively related part of the URL. There are two sorts of relative URLs: "protocol-relative URLs" and "root-relative URLs."
- HTTP Protocol type information is not included in protocol-relative URLs. The domain name, subdomain name, and extension are not included in root-relative URLs.
- Relative URLs are derived from the current, and open URLs in the web browser. An HTTPS URL in the web browser allows HTTPS navigation for protocol-relative URLs.
- If the opened URL is from that subdomain, a root-relative URL with a subdomain offers navigation from that subdomain. Thus, root-relative URLs should only be used for the name of the currently opened website and domain.
- When compared to Absolute URLs, Relative URLs are more difficult to maintain internal link format consistency.
Example of Relative URL:
Absolute URL
- Absolute URLs include all URL components such as the protocol, domain name, domain extension, subdomain information, path, query string, URL fragment, and port information.
- Absolute URLs are longer and contain more information than relative URLs. An Absolute URL includes "HTTP" or "HTTPS", "WWW" or "WWW" information while explaining the protocol or subdomain of a given website.
- For better Technical SEO implementation, absolute URLs should be utilized within the "hreflang" and "canonical" values. Using Absolute URLs makes it easier to maintain internal link consistency in comparison to relative URLs.
Example of Absolute URL:
Examples of CSS import
a) Importing CSS rules
Example: In this example, we are going to @import CSS rules from the "navigation.css" style sheet into the current style sheet.
b) Importing CSS rules conditionally
Example: In this example, we are going to @import CSS rules with media-dependent conditions that must be true before the linked CSS rules are applied.
The final @import rule will load the flex.css stylesheet if the user agent supports display: flex. The media query further specifies that the style rules will only be applied to screen devices with a maximum viewport width of 500px by using the "and" operator.
c) Importing CSS rules into a cascade layer
Example 1: In this example, we will create a cascade layer called utilities, and it will apply rules from the imported stylesheet theme.
Example 2: In this example, the rules in headers.css and links.css stylesheets cascade within the same layer as the audio[controls] rule.
Example 3: In this example, we are going to create two independent anonymous cascade layers with linked rules imported into each one separately.
Note: An anonymous cascade layer is one that is declared without a name. When anonymous cascade layers are created, they are finalized: they do not allow for rearranging or adding styles, and they cannot be referenced from outside.
Media Queries are Supported by the @import rule
The @import style sheet rule is used to import one style sheet into another. This rule also supports media queries, allowing the user to import the media-specific style sheet. After any @charset declaration, the @import rule must be specified at the top of the page.
There are four basic categories of media, and each can be coded differently.
-
-Print Print media allows you to customize the look of the print. This implies that when you print your pages, the styling changes, but the display remains the same. If you simply want to read the main content of a webpage and don't want to see the sidebar, navigation, or pagination bar as they are irrelevant in print, print media can help you do so.
-
-Screen The mostly used media is screen media. Here, you create a new CSS file, add all styles based on the screen, and import it into your main CSS file. When you access your webpage on that screen, you will see a different layout, although the original layout remains the same. You can style any screen, including a television, laptop, smartphone, or tablet.
-
-Speech This type of media is used by any text recognition software in which the machine reads the content. You can style and configure your website so that visitors only hear the main content of the page.
-
-All This includes all media types that are supported when importing CSS files. You can add print, screen, and speech styling to one CSS file and import it into another.
Is CSS @import worth using?
The quick answer is yes. The positives simply outnumber the negatives, and by a significant margin. If your organization has the resources to hire someone full-time to focus on developing, constructing, and maintaining your website, you may be fine without the @import command. However, whether you are a web designer or the owner of a small business creating your own website, the benefits cannot be ignored.
CSS @import may increase the loading time of your website. The trick is that it will not increase the loading time to your home page. This is important since everyone who visits your home page is there for a reason. They are interested in your idea or product if they visit subsequent pages. This type of visitor to your site is likely willing to wait the extra .074 seconds (this is an approximate figure, but you get the idea) for your page to load due to the CSS @import. This extra time can even be eliminated entirely if @import is utilized throughout the build process.
Pros of using CSS @import
- It saves time even when compared to the copy and paste or adding links methods.
- It helps the organization while creating a medium to large website. Because there is so much information and coding involved, any assistance is greatly appreciated.
- You can import as many CSS files as you wish. You might have a main file and then extra files such as typography or images.
- It is a free, time-saving, easy-to-use web development tool.
Cons of using CSS @import
It can increase page loading time if used during the build process. When a page containing the @import command loads, it must read and import the style sheet given in the rule before applying it to the current page. This takes very little time, and even the largest websites that use the @import command will see no difference in loading time. However, for bots that use loading time to calculate your search rating, this could shift you up or down the list of desired search engines.
Browser Compatibility of CSS import rule
The following browsers support the @import rule:
Browser | Google Chrome | Internet Explorer | Microsoft Edge | Firefox | Opera | Safari |
---|---|---|---|---|---|---|
Version Support | Yes | Yes | Yes | Yes | Yes | Yes |
Download | 1.0+ | 5.5+ | 12.0+ | 1.0+ | 3.5+ | 1.0+ |
Conclusion
- The import rule is used to link one CSS file to another. Make sure to define the important rule at the top of the file you wish to import before importing it.
- You can import a CSS file by providing its path or by specifying its URL. You can also import a CSS file with various styles and settings for a certain media.
- The import rule allows you to import files with four different types of media settings: print, screen, speech, and all.
- By using an import rule, you may also insert a CSS file into an HTML document.
- The import rule allows you to style items in a separate CSS file before importing them into an HTML document.