Table of contents
1.
Introduction
2.
External CSS File to HTML
2.1.
Syntax
2.2.
Example
2.3.
Benefits of External CSS
3.
Internal CSS
3.1.
Syntax
3.2.
Example
3.3.
Benefits of Internal CSS
4.
Inline CSS
4.1.
Syntax
4.2.
Example
4.3.
Benefits of Inline CSS
5.
Attributes of the Link Tag  
5.1.
1. `rel` Attribute  
5.2.
2. `href` Attribute  
5.3.
3. `type` Attribute  
5.4.
Folder Structure
6.
Comparison Table: Methods to Link CSS with HTML
7.
Frequently Asked Questions
7.1.
What is the best way to apply CSS to an HTML file?
7.2.
Can I use all three types of CSS in the same document?
7.3.
Which CSS method has the highest priority?
8.
Conclusion
Last Updated: Feb 9, 2025
Medium

How to Link a CSS File to HTML

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Linking a CSS file to an HTML document is an essential step in web development that allows developers to apply styles to their webpages. This technique helps in separating content from design, making the code more organized and easier to manage. By linking an external CSS file, you can apply consistent styling across multiple web pages without writing repetitive code. The most common way to link a CSS file to HTML is by using the <link> tag inside the <head> section of the HTML document. This method ensures that the styles are loaded before the page content is displayed.

How to Link a CSS File to HTML

In this article, we will discuss how to link a CSS file to HTML, different ways to apply CSS, and the benefits of each method with examples.

External CSS File to HTML

To link an external CSS file to an HTML document, use the <link> tag inside the <head> section.

Syntax

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

 

  • rel="stylesheet": Specifies that this link is for a CSS file.
     
  • type="text/css": Defines the type of document (CSS).
     
  • href="styles.css": Points to the external CSS file.

Example

styles.css

body {
    background-color: lightblue;
    font-family: Arial, sans-serif;
}
h1 {
    color: darkblue;
    text-align: center;
}

 

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>External CSS Example</title>
    <link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
    <h1>Welcome to My Website</h1>
</body>
</html>

 

Output: 

Output

The background of the page will be light blue, and the heading will be dark blue and centered.

Benefits of External CSS

  • Reusability: One CSS file can style multiple HTML pages.
     
  • Easier Maintenance: Changes made in the CSS file apply to all linked pages.
     
  • Better Page Load Speed: The CSS file is cached, reducing load times.

Internal CSS

Internal CSS is written inside the <style> tag within the <head> section of an HTML file.

Syntax

<head>
    <style>
        body {
            background-color: lightgrey;
            font-family: Arial, sans-serif;
        }
        h1 {
            color: green;
            text-align: center;
        }
    </style>
</head>

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Internal CSS Example</title>
    <style>
        body {
            background-color: lightgrey;
            font-family: Arial, sans-serif;
        }
        h1 {
            color: green;
            text-align: center;
        }
    </style>
</head>
<body>
    <h1>Welcome to My Website</h1>
</body>
</html>

 

Output: 

The background will be light grey, and the heading will appear in green and centered.

Benefits of Internal CSS

  • Useful for Single Pages: Ideal for applying styles to a single page without an extra file.
     
  • No Additional HTTP Requests: Unlike external CSS, no extra request is needed to fetch styles.

Inline CSS

Inline CSS applies styles directly within an HTML element using the style attribute.

Syntax

<h1 style="color: red; text-align: center;">Welcome</h1>

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Inline CSS Example</title>
</head>
<body>
    <h1 style="color: red; text-align: center;">Welcome to My Website</h1>
</body>
</html>

 

Output: 

Output

The heading will appear in red and centered.

Benefits of Inline CSS

  • Quick and Simple: Useful for styling single elements quickly.
     
  • Overrides External and Internal CSS: Has the highest specificity, meaning it takes priority.

Attributes of the Link Tag  

The `<link>` tag is used to connect an external CSS file to an HTML document. It is placed inside the `<head>` section of the HTML file. This tag has several attributes, but the most important ones are `rel`, `href`, & `type`. Let’s understand this one by one.  

1. `rel` Attribute  

The `rel` attribute specifies the relationship between the HTML file & the linked file. For CSS files, the value is always `"stylesheet"`. This tells the browser that the linked file is a stylesheet.  

2. `href` Attribute  

The `href` attribute specifies the path to the CSS file. This can be a relative path (if the CSS file is in the same folder or a subfolder) or an absolute path (if the CSS file is hosted online).  

3. `type` Attribute  

The `type` attribute defines the type of the linked file. For CSS files, the value is `"text/css"`. However, this attribute is optional in HTML5 because browsers automatically assume the file is CSS if the `rel` attribute is `"stylesheet"`.  


Let’s take an example of how to use the `<link>` tag with all its attributes:  

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Linking CSS to HTML</title>
    <link rel="stylesheet" href="styles.css" type="text/css">
</head>
<body>
    <h1>Welcome to My Website</h1>
    <p>This is a paragraph styled with external CSS.</p>
</body>
</html>


Output

Output

In this Code:  

1. The `<link>` tag is placed inside the `<head>` section.  
 

2. The `rel="stylesheet"` attribute tells the browser that the linked file is a stylesheet.  
 

3. The `href="styles.css"` attribute points to the CSS file named `styles.css` in the same directory as the HTML file.  
 

4. The `type="text/css"` attribute is optional but can be included for clarity.  

Folder Structure

To make this work, ensure your project folder looks like this:  

project-folder/  
│  
├── index.html  
└── styles.css  


 Example of `styles.css`:  

/ styles.css /
body {
    font-family: Arial, sans-serif;
    background-color: f4f4f4;
    color: 333;
}

h1 {
    color: 007BFF;
}

p {
    font-size: 18px;
    line-height: 1.6;
}


This CSS file will style the HTML elements as follows:  

  • The `body` will have a light gray background & dark gray text.  
     
  • The `h1` heading will be blue.  
     
  • The `p` paragraph will have a font size of 18px & a line height of 1.6.  

Comparison Table: Methods to Link CSS with HTML

MethodHow to ImplementWhy Use ItLimitations
External CSSCreate a separate .css file & link it using <link rel="stylesheet" href="styles.css"> in the HTML <head> section.Helps keep styling rules separate from HTML, making the code more organized & easier to update. Allows reuse across multiple pages for consistent styling.Requires an additional HTTP request to load the CSS file, which might slightly increase the initial loading time.
Internal CSSWrite CSS directly in the HTML file using <style> tags inside the <head> section.Useful for applying styles to a single page without needing an extra file. No need to manage multiple files.Makes the HTML file larger & harder to read. Styles cannot be reused across multiple pages without duplication.
Inline CSSAdd CSS directly to individual HTML elements using the style attribute, e.g., <p style="color: red;">.Useful for styling a single element quickly or overriding existing styles. Styles are applied immediately.Mixes content with styling, making HTML messy & harder to maintain. Leads to redundant code when styling similar elements.

Frequently Asked Questions

What is the best way to apply CSS to an HTML file?

The best method depends on the project. External CSS is preferred for larger projects as it promotes reusability and clean code.

Can I use all three types of CSS in the same document?

Yes, you can use External, Internal, and Inline CSS together, but it is recommended to maintain consistency for better readability.

Which CSS method has the highest priority?

Inline CSS > Internal CSS > External CSS. Inline CSS has the highest priority and overrides other styles.

Conclusion

Linking a CSS file to HTML can be done using External, Internal, or Inline CSS. External CSS is best for large projects, Internal CSS is useful for single pages, and Inline CSS is great for quick fixes. Understanding these methods will help you create well-structured and visually appealing web pages.

Live masterclass