Table of contents
1.
Introduction
2.
Internal CSS
2.1.
Syntax
2.2.
Key Features of Internal CSS
3.
External CSS
3.1.
Syntax
3.2.
Key Features of External CSS
4.
Inline, Internal, and external CSS Use cases
4.1.
Inline CSS Use Cases:
4.2.
Internal CSS Use Cases:
4.3.
External CSS Use Cases:
4.4.
Example Combining All Three CSS Methods
5.
Frequently Asked Questions
5.1.
What is the main difference between internal and external CSS?
5.2.
When should I use external CSS instead of internal CSS?
5.3.
Can I use internal and external CSS together?
6.
Conclusion
Last Updated: Dec 20, 2024
Easy

Difference Between Internal and External CSS

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

Introduction

CSS (Cascading Style Sheets) is a styling language used to describe the presentation of a document written in HTML. It allows you to control the layout, colors, fonts & other visual aspects of web pages. CSS provides different ways to apply styles to HTML elements, including inline styles, internal stylesheets & external stylesheets. Each method has its own advantages & use cases. 

Difference Between Internal and External CSS

In this article, we will discuss the difference between internal and external CSS, their features, and use cases. 

Internal CSS

Internal CSS is defined within the <style> tag inside the <head> section of an HTML document. This type of CSS applies styles to a single web page only. It is useful when you need to style a single page without affecting other pages.

Syntax

<!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>
        h1 {
            color: blue;
            font-size: 24px;
            text-align: center;
        }
        p {
            color: green;
            font-size: 16px;
        }
    </style>
</head>
<body>
    <h1>This is a heading</h1>
    <p>This is a paragraph styled using internal CSS.</p>
</body>
</html>


Output

Output

Key Features of Internal CSS

  1. Internal CSS applies styles to a single HTML document.
     
  2. It is written in the <head> section of the HTML file.
     
  3. It does not require a separate CSS file.
     
  4. Useful for small or single-page projects.

External CSS

External CSS is written in a separate .css file and linked to the HTML document using the <link> tag. It is the most commonly used method for large projects as it allows you to use the same CSS file for multiple HTML pages.

Syntax

Step 1: Create an external CSS file (e.g., styles.css):

h1 {
    color: red;
    font-size: 28px;
    text-align: center;
}

p {
    color: purple;
    font-size: 18px;
}


Step 2: Link the CSS file to your HTML document using the <link> tag:

<!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" href="styles.css">
</head>
<body>
    <h1>This is a heading</h1>
    <p>This is a paragraph styled using external CSS.</p>
</body>
</html>


Output

Output

When you run the above code:

  • The heading text will appear in red, centered, and with a font size of 28px.
     
  • The paragraph text will appear in purple and with a font size of 18px.

Key Features of External CSS

  1. Styles are defined in a separate file (e.g., styles.css).
     
  2. It allows reusability, as the same CSS file can be linked to multiple HTML pages.
     
  3. The HTML file remains clean and easy to read.
     
  4. Ideal for large projects.
     

Inline, Internal, and external CSS Use cases

Inline CSS Use Cases:

1. Quick and specific styling: Inline CSS is useful when you need to apply styles to a single element quickly without creating a separate stylesheet.

2. Email templates: When designing HTML email templates, inline CSS is often used because some email clients may not support external or internal stylesheets.

3. Dynamic styling: Inline CSS can be used in conjunction with JavaScript to dynamically change the styles of elements based on user interactions or other events.

Internal CSS Use Cases:

1. Single-page websites: If you have a small website with only one or a few pages, using internal CSS can be convenient as it keeps the styles within the same HTML file.

2. Prototyping and testing: When experimenting with different styles or creating quick prototypes, internal CSS allows you to see the changes immediately without creating separate files.

3. Embedded styles: Internal CSS can be used to embed specific styles within an HTML document, such as within an iframe or a custom web component.

External CSS Use Cases:

1. Larger websites: For websites with multiple pages, external CSS is the preferred method as it allows you to maintain a consistent look and feel across all pages by linking them to a single stylesheet.

2. Separation of concerns: External CSS promotes a clear separation between the structure (HTML) and the presentation (CSS), making the code more modular, reusable, and easier to maintain.

3. Caching and performance: When using external CSS, browsers can cache the stylesheet file, reducing the amount of data that needs to be downloaded on subsequent page visits, resulting in faster page load times.

4. Collaboration and version control: External CSS files can be easily shared among developers, and changes can be tracked using version control systems like Git.

Example Combining All Three CSS Methods

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Use Cases</title>
    <link rel="stylesheet" href="styles.css"> <!-- External CSS -->
    <style>
        h2 {
            color: green; /* Internal CSS */
        }
    </style>
</head>
<body>
    <h1 style="color: blue;">This is Inline CSS</h1>
    <h2>This is Internal CSS</h2>
    <p>This is External CSS.</p>
</body>
</html>


Output

Output

Explanation

  1. The <h1> tag uses inline CSS, so its text appears blue
    .
  2. The <h2> tag uses internal CSS, so its text appears green.
     
  3. The <p> tag uses external CSS (from styles.css), and its style depends on the external file.

Frequently Asked Questions

What is the main difference between internal and external CSS?

Internal CSS is written inside the <style> tag in the HTML file, while external CSS is stored in a separate file and linked using the <link> tag.

When should I use external CSS instead of internal CSS?

Use external CSS for large projects or when the same styles need to be applied to multiple HTML pages.

Can I use internal and external CSS together?

Yes, you can use both together. However, external CSS is preferred for maintainability.

Conclusion

In this article, we discussed the difference between internal and external CSS. Internal CSS is useful for styling single pages, while external CSS is ideal for larger projects as it promotes reusability and cleaner code. We also discussed a comparison between inline, internal, and external CSS with practical examples.

Live masterclass