Tech Junkie Blog - Real World Tutorials, Happy Coding!: CSS: Linking to An External Stylesheet With the link Tag and The @import Directive

Thursday, January 10, 2019

CSS: Linking to An External Stylesheet With the link Tag and The @import Directive

One of the first thing you would do in web development is to link an external Cascading Stylesheet to an HTML document.  As a matter of fact bootstrap would not work if you didn't link the bootstrap.css file in your HTML document.  Linking an external document is easy enough you often see the link like this

    <link href="Content/bootstrap.css" media="all" rel="stylesheet" type="text/css"></link>
    <script src="Scripts/bootstrap.js"></script>

Now let's break it down link attributes, there are four possible attributes for the link tag.

They are the following:
  • rel (required) - Relations, relationship to this page.  For CSS stylesheets its "stylesheet"
  • type (optional) - is the type of the link, "text/css" is the type for CSS stylesheets
  • href (required) - is the location of the resource, it could a relative path or an actual URL like a CDN
  • media (optional) - with this attribute you can specify which media the stylesheet is meant for.  For example the media attribute can have the "projector" value.

Another way you can link to an external stylesheet is with the @import directive, which you don't see very often.  It is used a little differently.  You have to put it on the first line or before the CSS rules inside the <style> tag like so 

        @import url(CSS/styles.css);

The cool thing about the @import directive is that you can have one stylesheet imported in another.  To demonstrate this let's create two stylesheets and have one stylesheet import the other.

First create the nav.css file with the following markup

    font-size: 5px;

Now create the styles.css file with the following markup


body {

    font-size: 50px;

The next step is to create an index.html file to import the styles.css file. Even though you only imported the styles.css file with the @import directive, you also have access to the nav.css style rules because the styles.css file already imported the nav.css file.

The html markup looks like the following:

<!DOCTYPE html>
    <link href="Content/bootstrap.css" type="text/css" rel="stylesheet" media="all" />
    <link href="Content/bootstrap.css" rel="stylesheet" />
    <script src="Scripts/bootstrap.js"></script>
    <meta charset="utf-8" />
        @import url(CSS/styles.css);
    <p class="styles-really-big-fonts">This is from styles stylesheet</p>
    <p class="nav-really-small-font">This is from nav stylesheet</p>

The output of the HTML file should look like this


  1. Thanks for such a great article here. I was searching for something like this for quite a long time and at last, I’ve found it on your blog. It was definitely interesting for me to read about their market situation nowadays.angularjs best training center in chennai | angularjs training in velachery | angularjs training in omr | angularjs training in chennai

  2. This comment has been removed by the author.

  3. Thanks for posting this information
    Sanjary Kids is one of the best play school and preschool in Hyderabad,India. The motto of the Sanjary kids is to provide good atmosphere to the kids.Sanjary kids provides programs like Play group,Nursery,Junior KG,Serior KG,and provides Teacher Training Program.We have the both indoor and outdoor activities for your children.We build a strong value foundation for your child on Psychology and Personality development.
    Preschool in hyderabad

  4. Percuma anda menggunakan mantra jika ternyata anda tak paham cara main dengan aturan dan hal detail lainnya. Pemahaman cara main, itu akan sangat bagus dan penting sekali diperhatikan.
    bandar ceme
    freebet tanpa deposit
    paito warna terlengkap
    syair sgp