Using External CSS Files in Web Development


An external stylesheet is a standalone . css file that is linked from a web page. The advantage of external stylesheets is that it can be created once and the rules applied to multiple web pages.

The external method in CSS refers to a way of defining the styles for an HTML document in a separate CSS file. Instead of embedding the CSS rules directly within the HTML file, you create a separate file with a .css extension that contains all the CSS code.

The external CSS file is then linked to the HTML document using the <link> tag in the <head> section of the HTML file. This allows the browser to load and apply the styles defined in the external CSS file to the HTML elements.

The advantages of using the external method include:

  1. Separation of Concerns: It helps to separate the structure (HTML), presentation (CSS), and behavior (JavaScript) of a web page. This improves code organization, readability, and maintainability.
  2. Code Reusability: By externalizing CSS code into a separate file, you can reuse the same stylesheet across multiple HTML pages, promoting consistency and saving development time.
  3. Caching: External CSS files can be cached by the browser, meaning that once a user visits a page that references the external stylesheet, subsequent page visits can load faster as the CSS file is already stored in the cache.

Here's an example of how to use the external method in CSS:

External style sheet (style.css)

h1{
    color:blue;
}
p{
    color:red;
}
To download raw file Click Here

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Tutor Joes</title>
    <link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
    <h1>External Style</h1>
    <p>
      Lorem ipsum dolor sit, amet consectetur adipisicing elit. Qui, 
      placeat eius! Error deserunt eaque sed non, laboriosam quaerat veritatis
      veniam possimus? Facilis corporis quae at alias esse optio sint 
      dignissimos.
    </p>
    
</body>
</html>

Output


CSS External Method Live Preview

In the HTML document, the <link> tag is used to link the HTML document with the external CSS file. The rel attribute is set to "stylesheet" to indicate that it's a stylesheets, and the href attribute specifies the path to the external CSS file ("style.css" in this example).

By using the external method, you can keep your HTML and CSS code separate, making it easier to manage and update styles across multiple pages. It also allows for better collaboration, as designers and developers can work independently on their respective files.


CSS Tutorial


Properties Reference


Cascading Style Sheet


Flexbox Tutorial


CSS Grid Tutorial


Transitions Properties


CSS Properties with Examples


CSS Selectors


CSS Pseudo Elements


CSS Attribute Selectors


Input Pseudo Classes


CSS Examples


CSS Animation Projects