Why is my CSS file not linking to my HTML?

It is considered a best practice to have your CSS stylesheets in an external file. So how can you link that CSS to your HTML file?

Linking to an external CSS file is an important part of any HTML page boilerplate. And in this article, we'll learn how to do it.

You can link your CSS file to your HTML file by adding a link element inside the head element of your HTML file, like so:

<!DOCTYPE html>
  <html>
    <head>
      <link rel="stylesheet" href="style.css">
    </head>
    <body>
    
    </body>
</html>

The link element has many uses, and it is important to specify the right attributes so that you can use it to import an external CSS stylesheet. We'll look at some important attributes now.

The rel attribute

The first of the two indispensable attributes is the rel attribute. You will use this attribute to tell the browser what the relationship is with the imported file.

You'll write rel="stylesheet" to tell the browser that you are importing a stylesheet.

The project --- index.html css ---------- style.css0 attribute

The second indispensable attribute is the

project --- index.html
            css ---------- style.css
0 attribute, which specifies the file to import.

A common situation is that the CSS file and the HTML file are in the same folder. In such a case you can write

project --- index.html
            css ---------- style.css
2.

If the CSS file and the HTML file are in different folders, you need to write the correct path that needs to go from the HTML file to the CSS file.

For example, a common situation is that the CSS file is in a folder that is a sibling to the HTML file, like so:

project --- index.html
            css ---------- style.css

In this case you would need to write a path like

project --- index.html
            css ---------- style.css
3.

The project --- index.html css ---------- style.css4 attribute

<link rel="stylesheet" href="style.css" type="text/css">

You use the

project --- index.html
            css ---------- style.css
4 attribute to define the type of the content you're linking to. For a stylesheet, this would be
project --- index.html
            css ---------- style.css
6. But since
project --- index.html
            css ---------- style.css
7 is the only stylesheet language used on the web, it is not only optional, but it is even a best practice not to include it.

The project --- index.html css ---------- style.css8 attribute

<link rel="stylesheet" href="style.css" media="screen and (max-width: 600px)">

The media attribute is not visible in the example. It's an optional attribute that you can use to specify when to import a certain stylesheet. Its value must be a media type / media query.

This can be useful in case you want to separate the styles for different devices and screen sizes in different files. You would need to import each CSS file with its own link element.

You can check out these articles (or other sources) on media queries to learn more about what you can write as an attribute value:

  • How to Use CSS Media Queries to Create Responsive Websites
  • How to Set Width Ranges for Your CSS Media Queries
  • Media Query CSS Tutorial – Standard Resolutions, CSS Breakpoints, and Target Phone Sizes
Conclusion

In this article, you learned how to add an external style sheet to your web page using the link element and the

project --- index.html
            css ---------- style.css
0 and
<link rel="stylesheet" href="style.css" type="text/css">
2 attributes.

You also learned that you can import multiple stylesheets and use the

project --- index.html
            css ---------- style.css
8 attribute to determine when each one should be applied.

Have fun creating web pages!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT


Why is my CSS file not linking to my HTML?
Ilenia Magoni

Moderator and staff author for freeCodeCamp.


If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

Why isn't my CSS linking to HTML?

Make sure that your CSS file really has the file name “ mystyle. css “ and is located in the same folder as the HTML document. Also, you should add a closing </html> tag.
CSS can be added to HTML documents in 3 ways:.
Inline - by using the style attribute inside HTML elements..
Internal - by using a <style> element in the <head> section..
External - by using a <link> element to link to an external CSS file..