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.
Fundamentals
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.
Together
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).
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.
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.
Targeting
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.
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.
Pro Tips
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
Hands-On
Examples Gallery
Six examples from inline styles to a complete styled page. Each includes View Output and Try It Yourself.
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>
HTML holds content; CSS lives in complete.css. Link the stylesheet once and update the look of the whole page from that file.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
CSS linking methodsUniversal
Bottom line: Write CSS once with standard syntax—it runs everywhere learners practice HTML.
Wrap Up
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.
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.