How to Add CSS to HTML

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 6 Examples + 6 Try It
Styling basics

Introduction

CSS (Cascading Style Sheets) styles and lays out web pages. It controls colors, fonts, spacing, and positioning—while HTML defines the structure and meaning of your content.

HTML and CSS work together: HTML says what is on the page; CSS says how it looks. Once you can connect the two, you can turn plain text into polished, readable websites.

What You’ll Learn

01

What CSS Is

Role & syntax.

02

Three Methods

Inline, internal, external.

03

Properties

Color, size, spacing.

04

Selectors

Element, class, ID.

05

Best Practices

Clean, reusable CSS.

06

Practice

Try It editor.

What Is CSS?

CSS stands for Cascading Style Sheets. It is a stylesheet language that describes how HTML elements should appear on screen, on paper, or in other media.

With CSS you can change fonts, colors, margins, backgrounds, layout, and animations—without changing the HTML structure itself. That separation keeps your markup focused on content and your styles easy to update in one place.

💡
Beginner Tip

Think of HTML as the skeleton of a house and CSS as the paint, furniture, and floor plan. Both are needed, but they do different jobs.

How CSS Works with HTML

HTML defines the structure of a webpage—headings, paragraphs, links, images. CSS defines the presentation—how those elements look and how they are spaced on the page.

You connect CSS to HTML in one of three ways:

  • Write styles inside an HTML tag (inline).
  • Write styles in a <style> block in the <head> (internal).
  • Link a separate .css file with <link> (external).

The browser reads your HTML, loads any linked CSS, matches selectors to elements, and applies the matching declarations (property + value pairs).

Three Ways to Add CSS to HTML

Every web project uses one or more of these approaches. Here is a quick comparison:

MethodWhere CSS LivesBest For
Inlinestyle attribute on one elementQuick one-off tweaks (use sparingly)
Internal<style> in <head>Single-page demos and early learning
ExternalSeparate .css file + <link>Real websites and team projects
  • Inline CSS — Styles applied directly on an element using the style attribute.
  • Internal CSS — CSS written inside a <style> element in the <head> section.
  • External CSS — A separate stylesheet linked with <link rel="stylesheet" href="styles.css">. View demo styles.css · complete.css

CSS Syntax

A CSS rule has two parts: a selector (which element to style) and a declaration block (the styles to apply). Inside the block, each declaration is a property and a value, separated by a colon and ended with a semicolon.

css
selector {
  property: value;
  another-property: another-value;
}

Example

This rule targets every p element and sets text color and size:

css
p {
  color: #2563eb;
  font-size: 18px;
}

The selector (p) matches all paragraphs. The declarations inside the curly braces define how they look.

Common CSS Properties

These properties appear in almost every stylesheet. You do not need to memorize them all at once—refer back as you build pages.

Text
color
font-size
font-family
font-weight
line-height

Typography and readability

Box model
margin
padding
border
width
height

Spacing and dimensions

Background
background-color
background-image
background-size

Surfaces behind content

Layout
display
text-align
max-width

How elements flow on the page

  • color — Sets the text color of an element.
  • font-size — Controls the size of the text (e.g. 16px, 1.125rem).
  • margin — Space outside an element’s border.
  • padding — Space inside an element, between content and border.
  • background-color — Fills the background behind an element.
  • width and height — Control the dimensions of an element.

How to Apply CSS

Below are the three methods in detail. Try each one in the Examples Gallery or open the Try It editor links.

1. Inline CSS

Add the style attribute directly on an HTML element. Good for quick tests; avoid using it everywhere.

html
<h1 style="color: crimson; text-align: center;">Hello World</h1>

2. Internal CSS

Place a <style> block inside <head>. All rules in that block apply to the current page.

html
<head>
  <style>
    h1 {
      color: #15803d;
    }
  </style>
</head>

3. External CSS

Save your CSS in a file such as styles.css, then link it from the <head>. When the HTML and CSS files sit in the same folder, use a relative path:

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

The separate stylesheet file might look like this (full file):

css
body {
  font-family: system-ui, sans-serif;
  background-color: #f1f5f9;
  margin: 0;
  padding: 2rem;
  color: #1e293b;
}

h1 {
  color: #1e3a8a;
  margin-top: 0;
}

p {
  color: #475569;
  line-height: 1.6;
  max-width: 36rem;
}

Keep the href path correct relative to your HTML file. If both files are in the same folder, href="styles.css" works.

📁
Live demo files

Working examples ship with this tutorial under /demos/html/how-to-add-css-to-html/. Open the full external CSS files directly: styles.css · complete.css. HTML demos: external.html · complete.html.

CSS Selectors

Selectors tell the browser which HTML elements receive each rule. These three are the most important for beginners.

Element Selector

Targets elements by tag name. Every matching tag gets the style.

css
p {
  color: #1e293b;
  line-height: 1.6;
}

Class Selector

Targets elements with a specific class attribute. Classes are reusable—add the same class to many elements.

css
.intro {
  font-weight: bold;
  color: #7c3aed;
}

In HTML: <h1 class="intro">Welcome</h1>

ID Selector

Targets one element with a unique id attribute. Use IDs sparingly for styling—prefer classes.

css
#header {
  background-color: #fef08a;
  padding: 1rem;
}

In HTML: <header id="header">...</header>

⚠️
Specificity hint

When rules conflict, more specific selectors usually win: inline styles beat IDs, IDs beat classes, and classes beat element selectors. External stylesheets loaded later can also override earlier ones.

Best Practices

✅ Do

  • Use external CSS files for real projects—keeps HTML readable
  • Prefer classes over IDs for styling hooks
  • Name classes by purpose (.card, .btn-primary), not appearance alone
  • Group related rules and add short comments in your CSS
  • Test on mobile—use max-width and responsive units like rem

❌ Don’t

  • Style every element with inline style attributes
  • Reuse the same id on multiple elements
  • Depend on !important to fix specificity problems
  • Mix presentation into HTML when a class would work
  • Forget that file paths in href must match your folder structure

Examples Gallery

Six examples from inline styles to a complete styled page. Each includes View Output and Try It Yourself.

🎨 Adding CSS

The three ways to connect styles to HTML.

Example 1 — Inline CSS

Style a single element with the style attribute:

html
<h1 style="color: crimson; text-align: center;">Hello World</h1>
<p style="font-size: 18px; color: #334155;">Styled with inline CSS.</p>
Try It Yourself

How It Works

Each declaration is written inside the style attribute. Fast for demos, hard to maintain at scale.

Example 2 — Internal CSS

Rules live in a <style> block in the document head:

html
<head>
  <style>
    h1 { color: #15803d; text-align: center; }
    p { font-size: 18px; color: #1e293b; }
  </style>
</head>
<body>
  <h1>Styled with Internal CSS</h1>
  <p>Rules are in the head.</p>
</body>
Try It Yourself

How It Works

One <style> block can hold many rules. Great for single-page tutorials and prototypes.

Example 3 — External CSS

Link a separate stylesheet from the HTML head. The demo HTML and CSS live in the same folder so href="styles.css" resolves correctly:

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>External CSS Demo</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <h1>Welcome to My Website</h1>
  <p>Styles come from an external file.</p>
</body>
</html>
css
body {
  font-family: system-ui, sans-serif;
  background-color: #f1f5f9;
  margin: 0;
  padding: 2rem;
  color: #1e293b;
}

h1 {
  color: #1e3a8a;
  margin-top: 0;
}

p {
  color: #475569;
  line-height: 1.6;
  max-width: 36rem;
}

How It Works

The browser fetches styles.css from the same folder as the HTML file and applies its rules. One CSS file can style every page on your site.

🎯 Selectors

Target elements with classes and IDs.

Example 4 — Class Selector

html
<h1 class="intro">Welcome!</h1>
<p class="note">Reusable class styles.</p>
css
.intro {
  font-weight: bold;
  color: #7c3aed;
}
.note {
  background: #fef9c3;
  padding: 0.75rem 1rem;
}
Try It Yourself

How It Works

A dot (.) before the name targets the class attribute. Add the same class to many elements.

Example 5 — ID Selector

html
<header id="header"><h1>Site Header</h1></header>
<footer id="footer">© 2026 My Site</footer>
css
#header {
  background-color: #fef08a;
  padding: 1rem;
  text-align: center;
}
#footer {
  font-size: 0.875rem;
  color: #64748b;
}
Try It Yourself

How It Works

A hash (#) targets an id. Each id should appear only once per page.

Example 6 — Complete Styled Page

A full page with HTML structure and a linked external stylesheet (complete.css):

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>HTML CSS Example</title>
  <link rel="stylesheet" href="complete.css">
</head>
<body>
  <h1 class="intro">Welcome to My Website</h1>
  <p>This is a simple example of using CSS with HTML.</p>
  <p>Learn more on <a href="https://example.com">Example.com</a>.</p>
</body>
</html>
css
body {
  font-family: Georgia, serif;
  background-color: #f0f0f0;
  margin: 0;
  padding: 2rem;
  color: #333;
}

h1.intro {
  color: navy;
  margin-bottom: 0.5rem;
}

p {
  font-size: 16px;
  line-height: 1.6;
  max-width: 40rem;
}

a {
  color: #2563eb;
}

a:hover {
  text-decoration: underline;
}

How It Works

HTML holds content; CSS lives in complete.css. Link the stylesheet once and update the look of the whole page from that file.

Universal Browser Support

Inline styles, internal <style> blocks, and external stylesheets via <link rel="stylesheet"> are supported in every modern browser and have been standard for decades.

Baseline · Since CSS1

CSS in HTML

All three methods work in Chrome, Firefox, Safari, Edge, and mobile browsers. No polyfills needed for the basics in this tutorial.

100% Core CSS support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
CSS linking methods Universal

Bottom line: Write CSS once with standard syntax—it runs everywhere learners practice HTML.

Conclusion

CSS is essential for web design. It works hand-in-hand with HTML to create visually appealing, user-friendly pages. Understanding inline, internal, and external styles—plus basic selectors—gives you a solid foundation for any site you build.

Start with internal CSS while learning, then move to external stylesheets as your projects grow. Explore the <style> tag and <link> tag references for deeper detail.

Key Takeaways

📝 02

Syntax

selector { prop: val; }

Rules
🏷️ 03

Classes

Reusable .classname.

Selectors
🔖 04

IDs

Unique #idname.

Targeting
📁 05

External CSS

Best for real sites.

Practice
▶️ 06

Try It

Edit & preview live.

Hands-on

❓ Frequently Asked Questions

Inline CSS uses the style attribute on a single element. Internal CSS goes inside a style element in the head. External CSS lives in a separate .css file linked with link rel="stylesheet" href="styles.css". External stylesheets are best for most websites.
Start with internal CSS in a style block while learning—it keeps everything in one file. As your project grows, move rules into an external stylesheet so HTML stays clean and CSS can be reused across many pages.
A class selector (.intro) can be reused on many elements. An ID selector (#header) should identify one unique element per page. Prefer classes for styling; reserve IDs for anchors and JavaScript hooks.
Yes. Add multiple link elements in the head, each pointing to a different .css file. Browsers load them in order, so later rules can override earlier ones when selectors match.
Generally yes—inline styles on an element have high specificity and often win over rules in a stylesheet, unless the stylesheet uses !important (avoid that as a beginner). This is one reason external CSS is easier to maintain.
No. Write CSS in a style block or .css file and open your HTML in any browser. No compiler or server is required for local practice, though a code editor with syntax highlighting helps.
Did you know?

CSS was first proposed in 1994 by Håkon Wium Lie. The “C” stands for Cascading—when multiple rules target the same element, the browser picks the most specific one. That cascade is why load order and selector choice matter.

Style your first page with CSS

Open the Try It editor, tweak colors and fonts, and watch the preview update instantly.

Open Try It editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful