Styling HTML

Inline CSS

Inline styles are applied directly to HTML elements using the style attribute. They override all other types of styles and are useful for quick, one-off changes.

<p style="color: red;">This is red text.</p>

Internal CSS

Internal CSS is defined in a <style> tag inside the <head> section. It’s useful for styling a single HTML page.

<head>
  <style>
    body {
      background-color: lightblue;
    }
    h1 {
      color: navy;
    }
  </style>
</head>

External CSS

External CSS is written in a separate .css file and linked in the HTML document using a <link> tag. This is the preferred method for styling larger or multi-page websites.

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

The styles.css file should only contain CSS code and no HTML.

CSS File Extension

External stylesheets must end with a .css extension. This file should only include valid CSS rules:

/* styles.css */
body {
  font-family: Arial, sans-serif;
  background-color: #f5f5f5;
}

Order of Priority

When styles conflict, CSS follows this order of priority:

  • Inline CSS – Highest priority
  • Internal CSS – Medium priority
  • External CSS – Lowest by default

Specificity, order in the document, and !important can also affect the final applied style.

Practice Prompt

Create a single HTML page that uses:

  1. An inline style to color a paragraph red
  2. An internal style to set an h1 element to blue
  3. An external stylesheet that sets the page background to light gray
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS Example</title>

  <!-- Internal CSS -->
  <style>
    h1 {
      color: blue;
    }
  </style>

  <!-- External CSS -->
  <link rel="stylesheet" href="styles.css">
</head>
<body>

  <h1>This is a Blue Heading</h1>

  <!-- Inline CSS -->
  <p style="color: red;">This paragraph is red using inline CSS.</p>

  <p>This is a normal paragraph to see the background color effect.</p>

</body>
</html>
/* External CSS */
body {
  background-color: lightgray;
}

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic