SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
CSS
1. CSS Tutorial for Beginners — Learn CSS from Scratch 2. CSS Syntax Explained — Selectors, Properties & Values 3. CSS Selectors Tutorial — Class, ID, Attribute & More 4. How to Add CSS to HTML — Inline, Internal, External 5. CSS Colors Tutorial — HEX, RGB, RGBA, HSL Explained 6. CSS Backgrounds Tutorial — Images, Size & Position 7. CSS Borders Tutorial — Style, Width, Radius & Shadow 8. CSS Margin Explained — Shorthand, Auto & Collapse 9. CSS Padding Explained — Shorthand & Margin Difference 10. CSS Height and Width — px, %, em, rem, vw, vh Units 11. CSS Box Model Explained — Content, Padding, Border 12. CSS Text Styling Tutorial — Color, Align & Spacing 13. CSS Fonts Tutorial — font-family, Sizes & Google Fonts 14. CSS Link Styling — Hover, Visited, Active & Buttons 15. CSS Lists Styling — Markers, Counters & Shorthand 16. CSS Tables Styling — Zebra Stripes & Hover Rows 17. CSS Display Property — Block, Inline, None Explained 18. CSS Position Explained — Relative, Absolute, Fixed 19. CSS Overflow Explained — Hidden, Scroll, Auto & More 20. CSS Float and Clear — Layouts & Clearfix Trick 21. CSS Pseudo-classes — Hover, Focus, nth-child & More 22. CSS Pseudo-elements — ::before, ::after & ::first-line 23. CSS Form Styling — Inputs, Labels & Buttons Tutorial 24. CSS Navigation Bar Tutorial — Dropdown Menu Included 25. CSS Website Layout Tutorial — Header, Sidebar, Footer 26. CSS Flexbox Tutorial — Complete Guide with Examples 27. CSS Grid Tutorial — Columns, Rows & Layouts Guide 28. CSS Responsive Design — Media Queries Tutorial 29. CSS Transitions & Animations — No JavaScript Needed 30. CSS Variables Tutorial — :root, var() & Theming 31. CSS Transforms Explained — Rotate, Scale, Translate 32. CSS Filters Tutorial — Blur, Grayscale, Sepia & More 33. CSS Text Effects — Shadows, Wrapping & Overflow 34. CSS Keyframes Tutorial — Bouncing Ball Animation 35. CSS Grid Template Areas — Layout in Words, Not Numbers
Final Test — Certificate ₹10
Try this code live
Lesson 4 of 35

How to Add CSS to HTML — Inline, Internal, External

4 min read · Free · No login needed

There are three ways to add CSS to a web page: inline, internal, and external. They all do the same job — they attach rules to your HTML — but they live in different places, and they suit different situations. Real websites use the external way almost all the time. Learn all three, then use the right one.

Diagram — see how it fits together
Diagram: CSS How To

The three ways

WayWhere it livesGood for
InlineIn a style attribute on one elementOne quick fix on one element only
InternalInside a <style> block in the <head>One single page
ExternalIn a separate .css file, linked with <link>A whole website

Syntax — all three ways

Inline: the style sits right on the element. It affects only that one element.

<h1 style="color: blue;">Hello</h1>

Internal: the rules sit in a <style> block in the head. They affect the whole page.

<head>
<style>
  h1 { color: blue; }
</style>
</head>

External: the rules live in a separate file, for example style.css, and the page links to it. One file can style a hundred pages.

<head>
<link rel="stylesheet" href="style.css">
</head>
Note: The <link> tag must sit inside the <head>, and the href must point to the real file. If the path is wrong — even by one folder — the page loads with no styling at all, and the browser gives no error message. When a page suddenly looks plain, the stylesheet link is the first thing to check.
Tip: Keep your CSS in a separate file from day one, even while learning. It is a small habit that saves a huge mess later: your HTML stays about structure, your CSS stays about looks, and you never have to hunt through pages to change one colour.

The cascade — which rule wins?

When two rules disagree about the same element, the browser follows the cascade — a set of tie-breakers, in this order:

  1. Inline styles win over internal and external styles.
  2. Later rules win over earlier ones, when they are equally strong.
  3. More specific selectors win over less specific ones. An id beats a class, a class beats an element. This is called specificity.

So #title { color: red; } beats .heading { color: blue; }, which beats h1 { color: green; } — even if the h1 rule comes last. The browser counts the strength of each selector, not just the order.

!important — the emergency override

There is one thing that beats everything: adding !important after a value.

p { color: red !important; }

This rule wins over inline styles, ids, and everything else. It exists for emergencies — for example, when you must override a style from a library you cannot edit. But use it almost never. Once !important appears in a project, people start adding more of it to beat the first one, and soon nobody can tell which rule wins. A stylesheet full of !important is a sign that the selectors need fixing, not more force.

Common mistakes

  • Linking the stylesheet with a wrong path — href="styles.css" when the file is called style.css, or in a different folder.
  • Using inline styles everywhere — quick at first, impossible to maintain. One colour change means editing every element by hand.
  • Expecting an internal rule to beat an inline style — inline always wins, unless the internal rule uses !important.
  • Spraying !important on every rule — it kills the cascade and makes future changes a fight.
  • Forgetting that a later rule only wins when specificity is equal — a later element selector still loses to an earlier id selector.

Remember

  • Inline = style attribute, internal = <style> in the head, external = <link> to a .css file.
  • External is the professional way — one file styles the whole site.
  • Cascade order: inline wins, then the more specific selector, then the later rule.
  • Specificity: id beats class, class beats element.
  • !important beats everything — use it only in a real emergency.

Example explained

  • h1 { color: green; } — the internal rule in the <style> block. It styles every h1 on the page green.
  • <h1 style="color: blue;"> — the inline style on the first heading. Inline wins over the internal rule, so this heading is blue, not green.
  • <h1>Our Courses</h1> — the second heading has no inline style, so the internal rule applies and it is green.
  • .note { color: grey; } — a class rule from the same internal block. It styles the paragraph with class="note".
  • <!-- <link rel="stylesheet" href="style.css"> --> — the comment shows the external way. In a real site this line (without the comment marks) would load the shared stylesheet instead of the internal block.

Example

Example
<!DOCTYPE html>
<html>
<head>
<title>How to Add CSS</title>
<style>
h1 { color: green; }
.note { color: grey; }
</style>
<!-- In a real site, use this instead of the style block above:
<link rel="stylesheet" href="style.css"> -->
</head>
<body>

<h1 style="color: blue;">Welcome to KAMALDNP</h1>
<h1>Our Courses</h1>
<p class="note">The first heading is blue because its inline style beats the internal rule. The second heading is green because only the internal rule applies to it.</p>

</body>
</html>

Output

Output — how it looks in the browser
Output of How to Add CSS to HTML — Inline, Internal, External

Try it yourself — live editor

Change the code below and press Run to see the result instantly. No file to save, no login needed.

Result

Frequently asked questions

The questions learners ask most about this lesson — read the question, then open it to see the answer.

1. What are the three ways to add CSS?

Inline with the style attribute, internal with a style element in the head, and external with a link to a .css file.

2. Which way of adding CSS do real websites use?

The external way, almost all the time. One .css file styles the whole site, so one change updates every page.

3. What wins when CSS rules disagree?

Inline styles win first, then the more specific selector, then the later rule. The !important flag beats everything, but use it only in emergencies.

Check yourself — did you get it?

Finished the lesson? Test yourself with 5 quick questions — you get the answer instantly, with the reason for each one.

1. Which way of adding CSS is best for a whole website?

External CSS lives in a separate .css file linked with <link> — one file can style a hundred pages.

2. Where must the <link> tag for a stylesheet sit?

The <link> tag must sit inside the <head>, and its href must point to the real file.

3. When an inline style and an internal rule disagree, which one wins?

Inline styles win over internal and external styles in the cascade.

4. Which selector wins in a fight: #title, .heading, or h1?

Specificity: an id beats a class, and a class beats an element — #title beats .heading, which beats h1.

5. What does !important do?

Adding !important after a value beats everything — inline styles, ids, all of it. Use it only in a real emergency.

Interview questions — can you answer these?

Read the question first, try to answer it in your own words, then open it to check. These are the questions interviewers actually ask.

1. What are the three ways to add CSS to a page, and which one should you use for a real website?

Inline (a style attribute on one element), internal (a <style> block in the <head>, for one page), and external (a separate .css file linked with <link>). For a real website, use external — one file styles the whole site, so a colour change in one place updates every page.

2. When two CSS rules disagree, which one wins?

The browser follows the cascade, in this order: inline styles win over internal and external styles; then the more specific selector wins — an id beats a class, a class beats an element (this is specificity); and when two rules are equally specific, the later one wins.

3. What does !important do, and why should you almost never use it?

!important after a value beats everything — inline styles, ids, all of it. It exists for emergencies, like overriding a style from a library you cannot edit. But use it almost never: once it appears, people add more of it to beat the first one, and soon nobody can tell which rule wins. A stylesheet full of !important means the selectors need fixing, not more force.

Try it yourself — build this

Reading is not enough — now build it yourself. Write the code in the live editor, run it, check the hint if you get stuck, and open the solution only after you have tried.

1. Three ways, one winner

Create a page that shows the three ways to add CSS AND the cascade. Put an internal rule in a <style> block (h1 green, .note grey). Give the first <h1> an inline style that makes it blue — inline beats internal, so it must end up blue while the second <h1> stays green. Then add an HTML comment in the <head> showing the external way: the <link rel="stylesheet" href="style.css"> line, commented out.

Hint: Inline style goes right on the element: <h1 style="color: blue;">. The internal rule h1 { color: green; } styles every h1 that has no inline style. The <link> line goes inside an HTML comment <!-- ... --> so it does not run.
Open the live editor
Show solution
<!DOCTYPE html>
<html>
<head>
<title>How to Add CSS</title>
<style>
h1 { color: green; }
.note { color: grey; }
</style>
<!-- In a real site, use this instead of the style block above:
<link rel="stylesheet" href="style.css"> -->
</head>
<body>

<h1 style="color: blue;">Welcome to KAMALDNP</h1>
<h1>Our Courses</h1>
<p class="note">The first heading is blue because its inline style beats the internal rule. The second heading is green because only the internal rule applies to it.</p>

</body>
</html>

Why learn this yourself — and not just ask AI?

AI can write code for you, but it cannot think for you. When you learn HTML yourself, your brain builds the real skill: you understand why a page looks the way it does, you can fix it when it breaks, and you can answer it in an exam or an interview without help. That is the skill that gets you a job — not a copied answer.

Get certified — just ₹10

Finish all 35 lessons, pass the test (80%), pay ₹10 and get your certificate.

Take the test

Prefer watching? Video courses teach the same topics step by step.

Go further with a full course

Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.

Advanced Cyber Security Industrial Training
₹5,000
View
Ethical Hacking & Cyber Security Workshop
View
CapCut Mobile Video Editing Course Beginner to Advance
₹99
View
Browse all courses
CSS — Lessons
1. CSS Tutorial for Beginners — Learn CSS from Scratch 2. CSS Syntax Explained — Selectors, Properties & Values 3. CSS Selectors Tutorial — Class, ID, Attribute & More 4. How to Add CSS to HTML — Inline, Internal, External 5. CSS Colors Tutorial — HEX, RGB, RGBA, HSL Explained 6. CSS Backgrounds Tutorial — Images, Size & Position 7. CSS Borders Tutorial — Style, Width, Radius & Shadow 8. CSS Margin Explained — Shorthand, Auto & Collapse 9. CSS Padding Explained — Shorthand & Margin Difference 10. CSS Height and Width — px, %, em, rem, vw, vh Units 11. CSS Box Model Explained — Content, Padding, Border 12. CSS Text Styling Tutorial — Color, Align & Spacing 13. CSS Fonts Tutorial — font-family, Sizes & Google Fonts 14. CSS Link Styling — Hover, Visited, Active & Buttons 15. CSS Lists Styling — Markers, Counters & Shorthand 16. CSS Tables Styling — Zebra Stripes & Hover Rows 17. CSS Display Property — Block, Inline, None Explained 18. CSS Position Explained — Relative, Absolute, Fixed 19. CSS Overflow Explained — Hidden, Scroll, Auto & More 20. CSS Float and Clear — Layouts & Clearfix Trick 21. CSS Pseudo-classes — Hover, Focus, nth-child & More 22. CSS Pseudo-elements — ::before, ::after & ::first-line 23. CSS Form Styling — Inputs, Labels & Buttons Tutorial 24. CSS Navigation Bar Tutorial — Dropdown Menu Included 25. CSS Website Layout Tutorial — Header, Sidebar, Footer 26. CSS Flexbox Tutorial — Complete Guide with Examples 27. CSS Grid Tutorial — Columns, Rows & Layouts Guide 28. CSS Responsive Design — Media Queries Tutorial 29. CSS Transitions & Animations — No JavaScript Needed 30. CSS Variables Tutorial — :root, var() & Theming 31. CSS Transforms Explained — Rotate, Scale, Translate 32. CSS Filters Tutorial — Blur, Grayscale, Sepia & More 33. CSS Text Effects — Shadows, Wrapping & Overflow 34. CSS Keyframes Tutorial — Bouncing Ball Animation 35. CSS Grid Template Areas — Layout in Words, Not Numbers
Final Test — Certificate ₹10

🤖 KAMALDNP AI Assistant

Ask about courses, website & more

Hi! I'm KAMALDNP AI Assistant. Ask me about our courses, website features, or anything else! 😊
Please wait 5s...
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Please log in to download. You will stay on this page after logging in!

New here? Register now →