How do I include CSS in an HTML document?

Including CSS (Cascading Style Sheets) in an HTML document is essential for controlling the presentation and visual appearance of web pages. CSS allows web developers to define styles, such as colours, fonts, layout, and spacing, separate from the HTML content. This separation of concerns enhances code organisation, promotes maintainability, and provides a more consistent user experience across the entire website. In this article, we will explore various methods of including CSS in an HTML document.

1. Inline CSS

Inline CSS is the most straightforward method of applying styles to HTML elements. It involves directly adding CSS rules within the style attribute of an HTML tag. While this approach works, it is generally not recommended for large-scale projects due to its limited maintainability.

Here’s an example of inline CSS:

<p style="color: blue; font-size: 16px;">This paragraph is styled with inline CSS.</p>

2. Internal CSS

Internal CSS, also known as embedded CSS, allows developers to include CSS rules within the <style> element in the <head> section of an HTML document. The styles defined within the <style> element apply to all elements on the page, providing a more organised way to manage styles compared to inline CSS.

Here’s an example of internal CSS:

<!DOCTYPE html>
<html>
<head>
  <title>Internal CSS Example</title>
  <style>
    p {
      color: blue;
      font-size: 16px;
    }
  </style>
</head>
<body>
  <p>This paragraph is styled with internal CSS.</p>
</body>
</html>

3. External CSS

External CSS is the most recommended method for including CSS in an HTML document. It involves creating a separate CSS file with .css extension and linking it to the HTML document using the <link> element. This approach promotes code reusability and makes maintenance more manageable as styles are centralised in a separate file.

Here’s an example of external CSS:

styles.css:

/* styles.css */
p {
  color: blue;
  font-size: 16px;
}

index.html:

<!DOCTYPE html>
<html>
<head>
  <title>External CSS Example</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <p>This paragraph is styled with external CSS.</p>
</body>
</html>

4. CSS Frameworks

CSS frameworks are pre-prepared libraries that offer a set of predefined CSS styles and components. These frameworks simplify the process of building responsive and visually appealing websites. Popular CSS frameworks include Bootstrap, Foundation, and Bulma. To use a CSS framework, you typically include its CSS file or link to a hosted version.

<!-- Example using Bootstrap -->
<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap Example</title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
  <div class="container">
    <h1>Hello, Bootstrap!</h1>
    <p>This paragraph is styled using the Bootstrap CSS framework.</p>
  </div>
</body>
</html>

Conclusion

Incorporating CSS into an HTML document is crucial for controlling the presentation of web pages. While inline and internal CSS offer basic styling capabilities, they are less scalable and maintainable for larger projects. External CSS, on the other hand, is the preferred method as it promotes the separation of concerns and allows for code reusability.

Furthermore, CSS frameworks provide even more robust and standardised styling options, making them valuable tools for building modern, responsive, and visually appealing websites. By understanding these different methods of including CSS in HTML documents, developers can effectively style web pages and create engageing user experiences across the web.

Scroll to Top