CSS Basics

CSS Introduction

Discover CSS - the powerful language that transforms plain HTML into beautiful, styled web pages with colors, layouts, and interactive effects.

What is CSS?

CSS (Cascading Style Sheets) is the language that controls how HTML looks: colours, fonts, spacing, layout and animation. HTML provides the structure and content, and CSS describes the presentation, usually in a separate .css file linked from the page.

<!-- in the HTML <head> -->
<link rel="stylesheet" href="styles.css">

/* styles.css */
h1 {
  color: navy;
  font-size: 2rem;
}

What CSS Controls

CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of HTML elements. It defines how HTML elements should be displayed, including colors, fonts, spacing, layout, and visual effects.

CSS works by applying rules to HTML elements through selectors, with styles cascading from parent to child elements and following specificity rules to determine which styles take precedence.

Visual Design

  • Colors and backgrounds
  • Fonts and typography
  • Borders and shadows

Layout Control

  • Positioning elements
  • Spacing and sizing
  • Responsive design

Interactivity

  • Hover effects
  • Animations
  • Transitions

Your First CSS Page

See how CSS transforms plain HTML into a styled webpage. This example shows internal CSS (styles within the HTML file) to demonstrate the immediate impact of CSS.

Code Editor

<h1>Hello, CSS</h1>
<p class="intro">CSS changes how HTML looks.</p>
<button>Hover me</button>

Preview

What This Demonstrates

  • •How CSS styles are applied to HTML elements
  • •Color and typography control
  • •Layout and spacing management
  • •Interactive hover effects

Key Concepts Shown

  • •CSS selectors (element, class)
  • •Property-value pairs
  • •Color and background styling
  • •Hover pseudo-class

Three Ways to Add CSS

1

Inline CSS

Quick fixes

Inline CSS: CSS rules applied directly to individual HTML elements using the style attribute. Styles are written as property-value pairs within the style attribute and affect only the specific element they're applied to.

<h1 style="color: blue; font-size: 24px;">Hello World!</h1>
Pros: Quick for testingCons: Hard to maintain
2

Internal CSS

Single page

Internal CSS: CSS rules defined within a <style> element placed in the <head> section of an HTML document. These styles apply to all matching elements within that specific HTML page only.

<head> <style> h1 { color: blue; font-size: 24px; } </style> </head>
Pros: Good for single pagesCons: Not reusable
3

External CSS (Recommended)

Best Practice

External CSS: CSS rules stored in separate .css files and linked to HTML documents using the <link> element. This method allows multiple HTML pages to share the same stylesheet, promoting consistency and maintainability.

HTML file:

<head> <link rel="stylesheet" href="styles.css"> </head>

styles.css file:

h1 { color: blue; font-size: 24px; }
Pros: Reusable & maintainableProfessional approach

CSS Best Practices

Do This:

  • Use external CSS files for better organization
  • Write descriptive class names like .header-nav
  • Group related styles together with comments
  • Use consistent indentation and formatting
  • Test your styles across different browsers

Avoid This:

  • Overusing inline styles for complex styling
  • Using vague class names like .red or .big
  • Writing overly complex selectors
  • Forgetting to organize and comment your code
  • Using !important excessively

Your Learning Path

Master CSS fundamentals step by step

What You've Learned

  • ▶What CSS is and how it works
  • ▶Three methods to add CSS to HTML
  • ▶CSS syntax and basic selectors
  • ▶CSS best practices and organization
  • ▶Basic styling properties and values

What's Next

  • ▶CSS Syntax - Rules and declarations
  • ▶CSS Selectors - Target specific elements
  • ▶CSS Colors - Create beautiful palettes
  • ▶CSS Backgrounds - Add visual appeal
  • ▶CSS Box Model - Control spacing and layout

Pro Learning Tip

Practice by styling simple HTML pages first. Start with colors and fonts, then gradually explore layout properties. Experiment with the code editor above!

CSS Introduction — step by step

1 / 4
1

What CSS Does

HTML structures content; CSS styles it. CSS rules consist of a selector (what to target) and a declaration block (property: value pairs). The browser reads both files and paints the final visual result.

1 of 4
💡External Stylesheets Are Cached by the Browser
When you use a linked external stylesheet, the browser downloads it once and caches it. Every subsequent page on your site that links the same file serves it instantly from cache — unlike inline or internal styles which are re-parsed on every page load.

🎯 Quick Check

Which CSS application method is recommended for production websites?

CSS Introduction: summary

Key points

  • CSS can be added three ways: an external stylesheet (best), a <style> block in the page, or an inline style attribute.
  • A rule is a selector (what to style) plus a declaration block of property: value; pairs.
  • "Cascading" means several rules can target one element; the browser picks a winner by importance, specificity and source order.
  • Some properties, such as color and font-family, are inherited by child elements; others, such as margin and border, are not.

Frequently asked questions

›Is CSS a programming language?

CSS is a style sheet language, not a general-purpose programming language. It declares how things should look rather than giving step-by-step instructions, although modern CSS has variables, functions like calc() and conditional rules.

›What is the difference between HTML and CSS?

HTML defines what's on the page (headings, paragraphs, images, links). CSS defines how it looks (colours, fonts, layout). You need both to build a web page.

›How long does it take to learn CSS?

You can learn the basics (selectors, colours, box model, Flexbox) in a couple of weeks of practice. Layout skills like Grid and responsive design take a few more weeks of building real pages.