Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk3 min

How to Add CSS to an HTML Page: Inline, Internal, and External Stylesheets

Add CSS to an HTML page inline, in a style block, or with a linked stylesheet. See copyable examples and learn how CSS conflicts are resolved.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To add CSS to an HTML page, put a style attribute on one element, add a <style> block in the page’s <head>, or link a separate .css file with <link rel="stylesheet">. For styles you want to reuse across pages, an external stylesheet is usually the best default: one file can control the appearance of multiple HTML documents, as MDN explains in its CSS getting-started guide.

Choose where the CSS should live

Method Where the CSS goes Best fit Main tradeoff
Inline An element’s style attribute A single element or a quick demonstration The declaration is attached to markup and is harder to reuse consistently.
Internal A <style> element in an HTML document A small, single-page example or page-specific CSS The rules stay in that HTML file rather than a shared stylesheet.
External A separate CSS file connected by a <link> element Styles reused across pages The link’s href must point to the file’s actual location.

MDN documents all three approaches and recommends external stylesheets as the most common and useful method, particularly when the same styles are needed on multiple pages. See its overview of adding CSS.

As an Amazon Associate I earn from qualifying purchases.

How to add CSS to one element with inline styles

Place CSS declarations inside the element’s style attribute. The value uses normal CSS declaration syntax, including a colon between the property and value and a semicolon after the declaration:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<p style="color: rebeccapurple;">This paragraph is purple.</p>

Inline CSS is useful when a style applies to just that element. Because the presentation is mixed into the HTML, repeating or changing styles across many elements can become awkward to maintain.

How to add CSS to one HTML document with an internal stylesheet

Put rules inside a <style> element, conventionally in the document’s <head>. The selector identifies which elements the rule affects:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Example</title>
  <style>
    p {
      color: rebeccapurple;
    }
  </style>
</head>
<body>
  <p>This paragraph is purple.</p>
</body>
</html>

The <style> element contains styling information for a document or part of one. Internal CSS keeps page-specific rules with the HTML file, which is convenient for a self-contained example but does not provide a shared stylesheet for other pages. MDN describes the element in its reference for <style>.

Rank #2
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

How to link an external CSS file to HTML

1. Create the stylesheet

Save a file named styles.css alongside the HTML file. Put CSS rules in the file without HTML tags:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
p {
  color: rebeccapurple;
}

2. Link it from the document head

Add a <link> element inside the HTML document’s <head>:

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

The rel="stylesheet" value identifies the linked resource as a stylesheet, and href gives its location. The path must match where the file is saved: if it is in a folder named css, use href="css/styles.css". MDN explains the element and its stylesheet relationship in the reference for <link>.

An external file is a practical default when styles should be shared among pages. Each page can link to the same CSS file, so a rule need not be copied into every HTML document.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Where to put CSS when a page uses more than one method

A page can contain inline declarations, a <style> block, and one or more linked stylesheets. Their effects are resolved by the CSS cascade; simply placing a rule in one location does not mean it universally wins.

For normal author declarations, an inline style takes precedence over normal author stylesheet declarations. But the cascade evaluates origin and importance before specificity, and layer order, scoping proximity, and source order can also affect the result. Important declarations, user or browser styles, animations, and transitions add further exceptions. Specificity is compared only at the appropriate stage and within the same origin and layer. MDN describes the purpose of the process: “The cascading algorithm determines how to find the value to apply for each property for each document element.” Read its introduction to the CSS cascade and specificity guide for the full rules.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When styles unexpectedly differ, first check that the external file’s href resolves to the file you edited. Then inspect the competing declarations and their importance, origin, layer, specificity, and order rather than assuming that inline CSS always wins.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.