How to Use CSS in HTML

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 4 Examples
Getting Started

What You’ll Learn

CSS describes how HTML elements look on screen, paper, or other media. This guide covers the fundamental ways to add CSS to your pages and the core techniques for styling text, backgrounds, borders, layouts, and animations.

01

Inline

style attr.

02

Internal

<style> tag.

03

External

.css file.

04

Text

Fonts & color.

05

Layout

Flex & Grid.

06

Motion

Animations.

Introduction

CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of documents written in HTML or XML. It defines how HTML elements should be displayed on the screen, on paper, or in other media.

This page covers the fundamental ways of using CSS to style web pages effectively — from adding your first color to building responsive layouts.

💡
Beginner Tip

Every CSS rule follows selector { property: value; }. Start with a <style> block in your HTML <head>, target an element like p, and set color: blue; to see instant results.

📝 How to Add CSS to HTML

There are three primary ways to include CSS in your HTML documents:

1. Inline CSS

Add styles directly to an HTML element using the style attribute. Quick for one-off changes, but hard to maintain on large sites.

inline.html
<p style="color: blue;">This is a blue paragraph.</p>

2. Internal CSS

Write CSS inside a <style> tag in the <head> section. Good for single-page demos and learning.

internal.html
<head>
  <style>
    p {
      color: red;
    }
  </style>
</head>

3. External CSS

Link a separate .css file from your HTML. This is the standard approach for real websites — one stylesheet can style many pages.

external.html
<head>
  <link rel="stylesheet" href="styles.css">
</head>
MethodWhereBest for
Inlinestyle attributeQuick tests, email templates
Internal<style> in <head>Learning, single-page demos
ExternalLinked .css fileProduction websites
External .css <style> block style=""

📝 How to Style Text

Text styling is one of the most common uses of CSS. Control color, size, font, alignment, and decoration.

text.css
p {
  color: #333;
  font-size: 16px;
  font-family: Arial, sans-serif;
  text-align: center;
}
  • Font propertiesfont-family, font-size, font-weight, line-height
  • Text propertiescolor, text-align, text-decoration, letter-spacing

📝 How to Style Backgrounds

CSS provides properties to style element backgrounds with colors, gradients, and images.

background.css
body {
  background-color: #f0f0f0;
}

.hero {
  background-image: url('background.jpg');
  background-size: cover;
  background-position: center;
}
  • Background propertiesbackground-color, background-image, background-size, background-position

📝 How to Style Borders

Define borders around elements with width, color, style, and rounded corners.

border.css
.card {
  border: 2px solid #000;
  border-radius: 5px;
}
  • Border propertiesborder-width, border-color, border-style, border-radius

📝 How to Create Layouts

Modern CSS layouts use Flexbox and Grid for responsive, flexible page structures.

Flexbox

A one-dimensional layout method for arranging items in rows or columns.

flex.css
.container {
  display: flex;
  justify-content: space-around;
}

Grid

A two-dimensional layout method controlling rows and columns together.

grid.css
.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}

📝 How to Add Animations

CSS animations and transitions add interactivity. Start with simple hover effects, then explore keyframe animations.

animation.css
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.fade {
  animation: fadeIn 2s ease-in-out;
}
  • Animation propertiesanimation-name, animation-duration, animation-timing-function
  • Transitionstransition for smooth property changes on hover or focus

⚡ Quick Reference

TaskCSS / HTML
Inline style<p style="color: blue;">
Internal stylesheet<style> p { color: red; } </style>
External stylesheet<link rel="stylesheet" href="styles.css">
Change text colorcolor: #333;
Page backgroundbackground-color: #f0f0f0;
Border + rounded cornersborder: 2px solid #000; border-radius: 5px;
Flex row layoutdisplay: flex; justify-content: space-around;
3-column griddisplay: grid; grid-template-columns: repeat(3, 1fr);
Hover transitiontransition: background-color 0.5s;

When to Use Each CSS Method

  • External CSS — Multi-page sites, team projects, and maintainable codebases.
  • Internal CSS — Single-page prototypes, email previews, or learning exercises.
  • Inline CSS — Rare one-off overrides; avoid for site-wide styling.
  • Flexbox — Navbars, button groups, and one-row/column alignment.
  • Grid — Page layouts, card galleries, and two-dimensional structures.

👀 Live Preview

See inline vs internal styling and a bordered box:

Inline — style attribute

Blue text from inline CSS

Internal — selector rule

Red-styled paragraph (simulated internal CSS)

Border + border-radius

Examples Gallery

Practice the three ways to add CSS, then build a complete styled page with borders and hover effects.

🔠 Adding CSS

Learn inline and internal methods before moving to external files.

Example 1 — Inline CSS

Apply styles directly on an element with the style attribute.

inline.html
<p style="color: #2563eb;">Blue paragraph</p>
Try It Yourself

How It Works

The browser reads the style attribute and applies those declarations only to that element. No selector needed.

Example 2 — Internal CSS

Write rules in a <style> block so one rule can style many elements.

internal.html
<style>
  p { color: #dc2626; }
</style>
Try It Yourself

How It Works

The p selector targets every paragraph. Rules in <style> apply to the current HTML document only.

🎨 Styling Techniques

Combine text, background, and border properties.

Example 3 — Style Text & Backgrounds

Use font, color, alignment, and background properties together.

text-bg.css
body {
  background-color: #f0f4f8;
  font-family: Georgia, serif;
}

p {
  text-align: center;
  background-color: #fff;
  padding: 1rem;
}
Try It Yourself

How It Works

background-color on body sets the page backdrop. The paragraph gets its own white background, creating a card effect.

Example 4 — Complete Styled Page

Combine text, backgrounds, borders, border-radius, and a hover transition.

complete.html
.box {
  width: 100px;
  height: 100px;
  background-color: #ef4444;
  border: 2px solid #991b1b;
  border-radius: 10px;
  transition: background-color 0.5s;
}

.box:hover {
  background-color: #2563eb;
}
Try It Yourself

How It Works

transition animates the background change over 0.5 seconds when you hover. :hover is a pseudo-class that applies styles on mouse-over.

💬 Usage Tips

  • Start internal, go external — Learn in one file, then split CSS into styles.css.
  • Use semantic HTML — Style h1, p, nav rather than only divs.
  • Inspect in DevTools — Right-click any element and choose Inspect to see applied CSS.
  • Group related rules — Keep typography, layout, and component styles in logical sections.
  • Link to deeper guides — Explore Flexbox and Grid tutorials once basics feel comfortable.

⚠️ Common Pitfalls

  • Specificity issues — Overusing !important creates conflicting styles that are hard to debug.
  • Neglecting browser compatibility — Check support for newer properties; most basics work everywhere.
  • Mixing too many units — Combining px, em, and % carelessly can cause unexpected sizing.
  • Inline styles everywhere — Makes updates painful; use classes and external files instead.
  • Forgetting the semicolon — Every declaration needs property: value; with a trailing semicolon.

♿ Accessibility

  • Color contrast — Ensure text is readable against backgrounds (WCAG contrast ratios).
  • Don’t rely on color alone — Use icons, labels, or patterns alongside color cues.
  • Focus styles — Keep visible outlines on interactive elements for keyboard users.
  • Readable font sizes — Avoid tiny text; use relative units like rem for scalability.
  • Motion preferences — Respect prefers-reduced-motion for users who disable animations.

🧠 How CSS Styling Works

1

HTML provides structure

Tags like p, div, and h1 define content and semantics.

HTML
2

CSS rules target elements

Selectors match HTML elements; declarations set visual properties.

Selectors
3

Browser applies the cascade

Multiple rules resolve by specificity, source order, and importance.

Cascade
=

Styled web page

Colors, spacing, layout, and motion transform plain HTML into polished UI.

🖥 Browser Compatibility

Core CSS properties covered here — colors, fonts, backgrounds, borders, Flexbox, Grid, and transitions — have universal support in modern browsers.

Baseline · Universal support

CSS Fundamentals

Inline, internal, and external CSS work in every browser. Flexbox, Grid, and animations are production-ready.

100% Modern browsers
Google Chrome Full support
Full support
Mozilla Firefox Full support
Full support
Apple Safari Full support
Full support
Microsoft Edge Full support
Full support
Opera Full support
Full support
CSS basics 100% supported

Bottom line: Everything in this tutorial works in all current browsers without vendor prefixes.

🎉 Conclusion

CSS provides numerous ways to style and enhance web pages. By understanding inline, internal, and external styles, plus core techniques for text, backgrounds, borders, layouts, and animations, you can create visually appealing and responsive websites.

Practice the four examples above, then move to external stylesheets and dedicated layout tutorials. CSS transforms plain HTML into engaging, interactive experiences.

💡 Best Practices

✅ Do

  • Use external stylesheets for maintainability
  • Keep HTML structure separate from presentation
  • Use responsive design with media queries
  • Name classes clearly (.card, .nav-link)
  • Comment sections in large CSS files

❌ Don’t

  • Rely on inline styles for entire sites
  • Overuse !important
  • Skip semantic HTML tags
  • Ignore color contrast for readability
  • Mix layout methods without a plan

Key Takeaways

Knowledge Unlocked

Five things to remember about using CSS

Foundation skills for every web developer.

5
Core concepts
Aa 02

Text

Color & fonts.

Style
bg 03

Backgrounds

Colors & images.

Fill
fx 04

Layout

Flex & Grid.

Structure
mv 05

Motion

Transitions.

Animate

❓ Frequently Asked Questions

Inline CSS uses the style attribute on an element. Internal CSS uses a style block in the head. External CSS links a separate .css file with a link tag. External stylesheets are best for real websites.
Start with internal CSS in a style block while learning — everything is in one file. Move to external CSS files once you understand selectors and properties. Avoid inline styles except for quick tests.
Inline CSS applies to one element via the style attribute. Internal CSS writes rules in a style tag that can target many elements with selectors like p or .card.
Yes. Add multiple link tags in the head, each pointing to a different .css file. Later files can override earlier ones depending on specificity and source order.
Yes. CSS styles HTML elements. Learn basic tags like p, div, h1, and a first, then add CSS to control colors, spacing, and layout.

Practice in the Live Editor

Open the HTML editor, add a <style> block, and experiment with colors, fonts, and layout.

HTML 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.

5 people found this page helpful