DEV Community

Cover image for 2. Three Ways to Link CSS to HTML
WEBDEVTALES
WEBDEVTALES

Posted on

2. Three Ways to Link CSS to HTML

When building a website, one of the first things you’ll need to do is style it with CSS (Cascading Style Sheets). But how do you link CSS to your HTML document? This is one of the core skills in web development and a crucial step in creating beautiful, responsive, and professional-looking websites.

Visit my website to get in-depth explanation on this topic.

In this guide, we'll explore different ways to link CSS to HTML, explain when to use each method, and offer an easy approach for beginners. Let’s dive right in!

Three Ways to Link CSS to HTML

There are three primary ways to apply CSS to your HTML document: Inline CSS, Internal CSS, and External CSS. Each method has its pros and cons, and knowing when to use them is key to mastering web design.

1. Inline CSS: Quick Styling for Individual Elements
Inline CSS applies styles directly within the HTML elements using the style attribute. This method is often used for quick, one-off changes to a single element.

Example:

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

Enter fullscreen mode Exit fullscreen mode

When to Use:

  • Quick fixes or temporary styling.
  • Small projects with minimal styling needs.

Drawbacks:

  • It mixes CSS with HTML, making the code harder to maintain.
  • Inline CSS cannot be reused across multiple elements.

2. Internal CSS: Embedding CSS Within the HTML Document
With internal CSS, you place all your CSS rules inside the <style> tag in the <head> section of the HTML document. This method is useful when you want to apply styles across the entire page but don’t plan to reuse them across multiple pages.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    body {
      background-color: lightgray;
    }
    h1 {
      color: navy;
    }
  </style>
</head>
<body>
  <h1>Internal CSS Example</h1>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

When to Use:

  • Styling a single page with unique requirements.

Drawbacks:

  • Can’t be reused across different HTML pages.
  • As your project grows, managing internal CSS can become cumbersome.

3. External CSS: The Best Practice for Reusability and Clean Code
External CSS is considered the best practice for most websites. With external CSS, you create a separate .css file that contains all your styles and then link it to your HTML document using the <link> tag.

Example:

Create a file called styles.css with your CSS rules:

body {
  background-color: lightblue;
}

h1 {
  color: darkblue;
  text-align: center;
}

Enter fullscreen mode Exit fullscreen mode
  • Link the external CSS file in your HTML:
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="styles.css">
  <title>External CSS Example</title>
</head>
<body>
  <h1>Welcome to My Styled Web Page!</h1>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

Top comments (0)