Skip to content
Tutorials Center
My learning

Topics

Lesson 59 of 122 ยท CSS and JavaScript in HTML

Adding CSS to HTML

Compare the three ways to add CSS to HTML (inline, the style element and external stylesheets), see how the cascade picks a winner and load CSS per media type.

You have already written CSS with the style attribute. That is one of three ways to connect CSS to an HTML page. Choosing the right one makes the difference between a site you can restyle in minutes and one where every change means hunting through dozens of files.

In this lesson you will compare inline styles, the <style> element and external stylesheets loaded with <link>. Then you will see how the cascade decides which rule wins when several of them disagree, and how the media attribute loads CSS only for certain screens or for printing.

Three ways, at a glance

Method Where the CSS lives Best for
Inline style attribute On one element Experiments, emails, values set by scripts.
<style> element In the page's <head> Single-page demos, page-specific or critical CSS.
External stylesheet via <link> A separate .css file Almost everything on a real site.

1. Inline styles

You know these already:

HTML
<p style="color: teal;">Inline style</p>

They affect one element only and can't use selectors, :hover or media queries. Use them sparingly.

2. The <style> element

A <style> element holds a full stylesheet inside the HTML document. It normally goes in the <head>:

HTML
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Seed Library</title>
  <style>
    body { font-family: system-ui, sans-serif; }
    h1   { color: #2d6a4f; }
  </style>
</head>
<body>
  <h1>Seed Library</h1>
</body>
</html>

Inside <style> you write rules. A rule has a selector (which elements to style) followed by declarations in curly braces:

CSS
selector {
  property: value;
  property: value;
}

The selectors you will use most are element names (p), classes (.notice) and IDs (#signup). Every TryIt editor on this site is a full page, so a <style> element works anywhere in the code:

A style element with selectors

One rule now styles every <h1> on the page. That is already far better than repeating inline styles, but the CSS still only applies to this page.

On a real site, CSS lives in its own file, for example styles/main.css, and each page links to it from the <head>:

HTML
<head>
  <meta charset="utf-8">
  <title>Seed Library</title>
  <link rel="stylesheet" href="/styles/main.css">
</head>

And main.css contains only CSS โ€” no <style> tags:

CSS
body {
  font-family: system-ui, sans-serif;
  line-height: 1.5;
}

h1 {
  color: #2d6a4f;
}
  • rel="stylesheet" says what the linked file is.
  • href is the path to the file โ€” relative, root-relative or absolute (see File Paths and URLs).
  • <link> is a void element: no closing tag.

Why this is the standard approach:

  • One file, every page. Change a color once and the whole site updates.
  • Caching. The browser downloads main.css once and reuses it on every page, so later pages load faster.
  • Separation of concerns. HTML files hold content and structure; CSS files hold presentation. Each is easier to read and maintain.

You can link several stylesheets. They apply in the order they appear, which matters for the cascade below.

Linked CSS is render-blocking. The browser waits for stylesheets in the <head> before it shows the page, so visitors never see an unstyled flash. That is usually what you want, but it is also why huge stylesheets slow down the first paint. See Loading Resources Efficiently.

Avoid @import for loading files. CSS can load another stylesheet with @import url("other.css");, but the browser only discovers that file after downloading the first one, which delays rendering. Prefer separate <link> elements.

The cascade: which rule wins?

Several rules often target the same element with conflicting values. The browser settles each conflict with the cascade. For everyday work, three questions decide the winner, in this order:

  1. Origin and importance. Your styles beat the browser's built-in defaults. Declarations marked !important jump ahead of normal ones (use it rarely โ€” it makes CSS hard to override later).
  2. Specificity. A more specific selector wins. Roughly: inline style > ID selector > class, attribute and pseudo-class selectors > element selectors.
  3. Order. If everything else is equal, the rule that comes last wins.
The cascade in action

The cascade works per property. If one rule sets the color and another sets the font size, both apply; only the conflicting property is decided by the cascade.

Some properties โ€” such as color and font-family โ€” are also inherited: set them on <body> and every element inside gets them unless something overrides them. That is why the body rule in the earlier examples styled all the text.

Keep specificity low. Style mostly with classes, avoid IDs in selectors, and save !important for emergencies. Low, even specificity means "later rule wins" stays the main rule, which is easy to reason about.

The media attribute

Both <link> and <style> accept a media attribute. The CSS only applies when the media query matches:

HTML
<link rel="stylesheet" href="/styles/main.css">
<link rel="stylesheet" href="/styles/print.css" media="print">
<link rel="stylesheet" href="/styles/wide.css" media="(min-width: 900px)">
  • media="print" โ€” only when printing or saving as PDF: hide the navigation, use black text, show link addresses.
  • media="screen" โ€” only on screens.
  • media="(min-width: 900px)" โ€” only on wide viewports.

The browser still downloads non-matching stylesheets (with low priority), so this is mainly a way to organize code. Here is the same idea inside a <style> element:

Styles that depend on the media

In a single stylesheet you would usually write the same thing with an @media block instead; the attribute is most useful on <link>.

Put both in the <head>. Then the browser knows about all the CSS before it starts drawing the page, and visitors don't see the layout jump as late styles arrive. The head element lesson shows a recommended order for everything in the head.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which is the best way to share one set of styles across a 50-page website?

    An external stylesheet is written once, cached by the browser and used by every page that links to it.
  2. 2. Two rules with the same selector, .note, set different colors. Which color is used?

    With equal specificity and importance, the later declaration wins.
  3. 3. A paragraph has class="intro" and id="top". Rules exist for p, .intro and #top. Which one sets its color?

    An ID selector is more specific than a class selector, which is more specific than an element selector.
  4. 4. What does <link rel="stylesheet" href="print.css" media="print"> do?

    The media attribute limits the stylesheet to the matching media โ€” here, printing.

Summary

  • CSS can be added inline (style), in a <style> element or in an external file loaded with <link rel="stylesheet">.
  • External stylesheets are the standard: one file for the whole site, cached by the browser.
  • A rule is a selector plus declarations; the cascade settles conflicts by importance, specificity and then order.
  • Some properties, like color and font-family, are inherited by child elements.
  • The media attribute applies a stylesheet only for print, screens or certain viewport sizes.

Next lesson: Adding JavaScript โ€” bringing your pages to life with scripts.