How To Add CSS

Apply CSS with external, internal, and inline methods.

Syntax<link rel="stylesheet" href="styles.css">

There are three ways to insert CSS:

  1. External — a separate .css file linked with <link>. Best for whole sites.
  2. Internal — a <style> block in the page <head>. Good for a single page.
  3. Inline — a style attribute on one element. Use sparingly.

If several rules target the same element, the one with the highest specificity and the one that appears last wins. This is the cascade.

Example

Try it yourself
Loading editor…
Press Run to see the result.

When to use it

  • A developer adds an external stylesheet to a 50-page site so a single file change rebrands every page at once.
  • A designer adds an internal style block to a one-off landing page to avoid creating a separate file for a minor tweak.
  • A CMS plugin injects an inline style on a dynamically generated banner to set a background color fetched from the database.

More examples

Link to external stylesheet

The preferred method — links a separate file so styles apply to the whole site and the file can be browser-cached.

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

Internal style block in head

Places CSS directly in the page head, useful for single-page styling when no external file is warranted.

Example · html
<head>
  <style>
    body { font-family: Arial, sans-serif; }
    h1   { color: #2965f1; }
  </style>
</head>

Inline style on one element

Applies style with the style attribute directly on an element — highest specificity but hardest to maintain at scale.

Example · html
<p style="color: #e05c00; font-weight: bold;">
  This paragraph has an inline style.
</p>

Discussion

  • Be the first to comment on this lesson.
How To Add CSS — CSS | SoundsCode