How to Add CSS to HTML (Inline, Internal, External CSS)

How to Add CSS to HTML (Inline, Internal, External CSS)

HTML CSS – Inline, Internal and External CSS Explained | TipsInLearning
Lesson 3 of 9

HTML CSS — Adding Styles to Webpages

Learn all three ways to add CSS to HTML — inline, internal, and external CSS — plus CSS syntax, element/class/ID selectors, and the CSS priority cascade rule.

12 min read
Beginner
SEO Friendly
What You'll Learn
What CSS is and why it's used
Inline, Internal and External CSS
CSS property: value syntax
Element, class and ID selectors
CSS priority (cascade) rules
When to use each CSS method

What is CSS?

CSS stands for Cascading Style Sheets. It is used to control the appearance and layout of HTML elements — colors, fonts, spacing, size, positioning, and much more.

Without CSS, every webpage would look the same: plain black text on a white background. CSS is what makes the web beautiful and responsive.

HTML vs CSS: HTML defines the structure (what content is there). CSS defines the presentation (how it looks). They work together — HTML is the skeleton, CSS is the skin and clothes.

3 Ways to Add CSS to HTML

There are exactly three methods to apply CSS to an HTML document — each suited for different situations:

1
style=""
Inline CSS
CSS written directly inside an HTML tag using the style attribute.
⚠ Avoid for large sites
2
<style>
Internal CSS
CSS written inside a <style> tag in the <head> section of the HTML page.
✓ Single page only
3
style.css
External CSS
CSS written in a separate .css file and linked to the HTML with a <link> tag.
⭐ Best Practice

1. Inline CSS

Inline CSS — style="" attribute

Inline CSS is written directly inside an HTML element's opening tag using the style attribute. Multiple CSS properties are separated by semicolons.

Quick styling for one element
Hard to maintain on large sites
Useful for email HTML
Repeated code everywhere
HTML — Inline CSS
<!DOCTYPE html>
<html lang="en">
<body>

  <!-- CSS is written inside the style attribute -->
  <h1 style="color: blue; font-size: 40px;">
    Hello World
  </h1>

  <p style="color: red; font-family: Arial;">
    This is a red paragraph.
  </p>

</body>
</html>

Hello World

This is a red paragraph.

🌐 Inline CSS — color and font-size applied directly inside the tag

2. Internal CSS

Internal CSS — <style> in <head>

Internal CSS is written inside a <style> tag placed in the <head> section of your HTML document. It applies to the entire current page — you write rules once and they apply to all matching elements on that page.

Better than inline CSS
Cannot reuse across multiple pages
Good for single-page websites
Increases HTML file size
HTML — Internal CSS
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Internal CSS</title>

  <!-- CSS written inside <style> in <head> -->
  <style>
    h1 {
      color: blue;
      text-align: center;
    }

    p {
      color: green;
      font-size: 18px;
    }
  </style>
</head>
<body>

  <h1>Welcome to TipsInLearning</h1>
  <p>This paragraph is styled by internal CSS.</p>

</body>
</html>

Welcome to TipsInLearning

This paragraph is styled by internal CSS.

🌐 Internal CSS — h1 and p styled via rules in the <style> tag

3. External CSS — Best Practice

External CSS — Separate style.css file

External CSS is written in a completely separate file with a .css extension. It is linked to your HTML page using the <link> tag in the <head>. This is the professional standard for all real websites.

One CSS file styles all pages
HTML stays clean and readable
Browser caches the CSS file
Easy to maintain and update

External CSS requires two steps — create the CSS file and link it to HTML:

CSS — style.css (Step 1: Create this file)
/* style.css — Your external stylesheet */

body {
  background-color: #f8fafc;
  font-family: Arial, sans-serif;
}

h1 {
  color: #7c3aed;
  text-align: center;
}

p {
  color: #475569;
  font-size: 18px;
  line-height: 1.8;
}
HTML — index.html (Step 2: Link the CSS file)
<!DOCTYPE html>
<html lang="en">
<head>
  <title>External CSS Example</title>

  <!-- Link the external CSS file -->
  <link rel="stylesheet" href="style.css">

</head>
<body>

  <h1>TipsInLearning</h1>
  <p>Styled by an external CSS file!</p>

</body>
</html>

The <link> tag explained: rel="stylesheet" tells the browser this linked file is a CSS stylesheet. href="style.css" is the file path to your CSS file. The <link> tag always goes inside <head>.

CSS Syntax

Every CSS rule follows the same structure — a selector targeting an element, followed by curly braces containing property: value pairs:

CSS — Basic Syntax
/* Basic syntax */
selector {
  property: value;
}

/* Real example */
h1 {
  color: red;
  font-size: 30px;
  text-align: center;
}

CSS Selectors

CSS selectors define which HTML elements a CSS rule applies to. There are three main types used by beginners:

1. Element Selector
p { }
Targets all elements of that type. p applies to every <p> on the page.
2. Class Selector
.classname { }
Targets elements with a specific class. Can apply to multiple elements. Uses a dot prefix.
3. ID Selector
#idname { }
Targets one unique element with that id. Only one element per id per page. Uses a # prefix.
CSS — All 3 Selector Types
/* 1. Element selector — targets ALL p tags */
p {
  color: blue;
}

/* 2. Class selector — targets elements with class="highlight" */
.highlight {
  background-color: yellow;
  font-weight: bold;
}

/* 3. ID selector — targets the ONE element with id="title" */
#title {
  color: green;
  font-size: 32px;
}
HTML — Using All 3 Selectors
<!-- Element selector targets this -->
<p>This paragraph is blue.</p>

<!-- Class selector targets this -->
<p class="highlight">This is highlighted.</p>

<!-- ID selector targets this -->
<h1 id="title">This is the big green title.</h1>

CSS Priority — The Cascade

When multiple CSS rules apply to the same element, CSS uses a priority order (called the cascade) to decide which one wins. Inline CSS always has the highest priority:

Inline CSS style="" attribute Highest priority ⬆
Internal CSS <style> in head Middle priority
External CSS style.css file Lowest priority ⬇
HTML — Priority in Action
<style>
  p { color: blue; }  /* internal CSS says blue */
</style>

<!-- Inline CSS overrides — this text will be RED, not blue -->
<p style="color: red;">
  This is red (inline wins over internal CSS)
</p>

<!-- No inline style — uses internal CSS -->
<p>This is blue (internal CSS applies)</p>

Comparison Table

CSS Methods Comparison
MethodWhere WrittenBest ForReusable?Recommended?
Inline CSS Inside HTML tag (style="") Quick one-off changes, email HTML ❌ No Avoid
Internal CSS <style> in <head> Single-page sites, demos This page only OK sometimes
External CSS Separate .css file All real websites and projects ✅ All pages ⭐ Best Practice

Best Practice Summary: Use External CSS for all real projects — it keeps HTML clean, CSS reusable across pages, and files load faster because the browser caches the stylesheet. Only use inline CSS for quick testing or HTML emails.

Try It Yourself

Challenge — Style a Page Three Ways
1Create a heading with inline CSS — make it purple and 36px using style=""
2Add a <style> block in <head> — use internal CSS to make all paragraphs green with 18px font
3Create a style.css file and use external CSS to set the body background color and link font family
4Add a class class="highlight" to one paragraph and style it with a yellow background using a class selector
5Test CSS priority — add the same element to inline, internal, and external CSS with different colors — see which one wins
Quick Summary
1Inline CSSstyle="" inside HTML tag; highest priority but avoid for large sites
2Internal CSS<style> in <head>; applies to current page only
3External CSS — linked .css file; reusable, cacheable, best practice
4CSS syntax: selector { property: value; }
53 selectors: p (element) · .class · #id
6Priority: Inline > Internal > External — inline always wins

Post a Comment