HTML Linking External Stylesheets

Using CSS, you can transform a plain HTML page into something visually engaging. While it’s possible to include CSS directly within your HTML files, linking to an external stylesheet keeps your code organized. With external stylesheets, you can control the look and feel of multiple pages from a single file, making updates faster and more efficient.

How to Link an External Stylesheet

To link an external stylesheet to your HTML, use the <link> tag in the <head> section of your document. The <link> tag should include two main attributes:

  • rel – specifies the relationship between the current document and the linked file.
  • href – points to the URL or file path of the stylesheet.

HTML Linking External Stylesheets Syntax

HTML




Tutorials dojo strip

HTML Linking External Stylesheets Example Code

Explanation of Code:

  • The <link> tag in the <head> section of index.html tells the browser to use the styles defined in styles.css.
  • The CSS file then controls the appearance of the <body>, <h1>, and <p> elements.


Let’s say you have an HTML file (index.html) and a CSS file (styles.css). Here’s how to link them:

HTML

Here’s the CSS Sample File:

HTML




HTML Linking External Stylesheets File Organization Tips

When you start working on larger projects, keeping files organized is essential. Typically, you’ll store all your CSS files in a folder like this:

markdown

To link styles.css in this structure, update the href path:

HTML




HTML Linking MULTIPLE External Stylesheets

It’s possible to link more than one stylesheet. Just add another <link> tag for each additional file:

HTML

Styles are applied in the order they are linked. If two stylesheets contain conflicting rules, the styles in the last linked file will override earlier ones.




HTML Labs

Tutorials dojo strip