What is CSS?

CSS stands for Cascading Style Sheets. It's the language of design on the web. While HTML provides the structure (headings, paragraphs, links), CSS controls the appearance โ€” colors, fonts, spacing, layouts, animations, and more.

Think of HTML as the skeleton and CSS as the skin, clothes, and makeup. The structure is still there, but CSS makes it look beautiful and professional.

Every website you visit โ€” from simple blogs to complex e-commerce sites โ€” uses CSS. Google's homepage is simple (plain HTML could do it), but Gmail, YouTube, Instagram, and Netflix all rely heavily on CSS to create their visual designs.

Key point:
HTML describes what content exists. CSS describes how it looks. Together, they create the complete web experience.

The problem CSS solves: Separation of concerns

Before CSS, web developers had to put styling directly into HTML using attributes like bgcolor and color. This was messy:

  • Hard to maintain โ€” changing a color meant editing dozens of files.
  • Code became bloated โ€” every element had styling attributes.
  • Not reusable โ€” you couldn't apply the same style to multiple pages easily.

CSS solved this by separating style from structure. Now you write HTML once, and a single CSS file can style an entire website. Change the CSS, and every page updates instantly.

Modern web development:
Today, separating content (HTML), presentation (CSS), and behavior (JavaScript) is a best practice. It makes code cleaner, faster, and easier to maintain.

CSS syntax: selector, property, value

Every CSS rule has three parts: a selector (which HTML elements to style), a property (what to change), and a value (how to change it).

styles.css CSS syntax breakdown

The basic structure is:

selector { property: value; property: value; }

  • Selector โ€” The HTML tag name (or more) you want to style. Examples: h1, p, .button, #header.
  • Property โ€” What aspect to change. Examples: color, font-size, background, padding.
  • Value โ€” The new value for that property. Examples: blue, 16px, #6D28D9.
  • Semicolon โ€” Always end each property-value pair with a semicolon.

Three ways to add CSS

There are three methods to apply CSS to an HTML page. Each has pros and cons:

example.html Three ways to add CSS

Method 1: Inline styles (NOT recommended)

Inline styles use the style attribute directly on an HTML element:

<h1 style="color: blue; font-size: 32px;">Hello</h1>

Pros: Quick for testing. Cons: Hard to maintain, not reusable, makes HTML messy. Avoid in production.

Method 2: Internal stylesheets (Good for learning)

Internal styles go in a <style> tag in the <head>:

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

Pros: Keeps styles organized in one place. Cons: Not shared across pages, increases HTML file size. Use for single-page projects.

Method 3: External stylesheets (BEST PRACTICE)

External styles go in a separate .css file, linked from the <head>:

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

Pros: Reusable across all pages, browser caching, clean HTML. Cons: Requires a separate file. This is how professionals do it.

How the cascade works

CSS stands for Cascading Style Sheets. "Cascade" means that when multiple styles apply to an element, the browser follows specific rules to decide which one wins.

The basic rule: More specific styles override general styles. Also, styles later in the file override earlier ones.

cascade.css How cascade works

The hierarchy (from least to most specific):

  1. Element selectors: p โ€” applies to all paragraphs.
  2. Class selectors: .warning โ€” applies to elements with class="warning". More specific than element selectors.
  3. ID selectors: #header โ€” applies to the single element with id="header". More specific than classes.
  4. Inline styles: style="color: red;" โ€” Most specific, but avoid them.
Pro tip:
Avoid relying too heavily on the cascade. Write CSS that's predictable and easy to understand. Don't nest selectors too deeply or use too many IDs.

Try it yourself: Three ways to style

Edit the code below to see all three CSS methods in action. Try changing colors and font sizes in both the style block and inline attributes.

โœ๏ธ Try It Yourself โ€” Three CSS Methods
Preview

Your first CSS properties

Here are the CSS properties you'll use most often. These control colors, fonts, spacing, and sizing:

styles.css Common first CSS properties

Color values: Use hex codes (#6D28D9), named colors (blue), or RGB (rgb(109, 40, 217)).

Sizing units: px (pixels, fixed), % (percentage of parent), em (relative to font size).

Try it yourself: CSS makeover

See the power of CSS! Left side shows plain HTML. Right side is the same HTML with CSS styling. Edit the CSS to transform it further.

โœ๏ธ Try It Yourself โ€” CSS Makeover
Preview

Why external CSS is best practice

Professional web developers always use external stylesheets. Here's why:

  • Reusable: One CSS file styles your entire website. Change a color once, and it updates everywhere.
  • Cacheable: Browsers cache the CSS file. Users download it once, then it loads instantly on every page.
  • Maintainable: All styles in one place. Easy to find and update them later.
  • Clean HTML: Your HTML files stay focused on content, not presentation.
  • Team collaboration: Designers can edit CSS while developers edit HTML, without conflicts.
Production standard:
Once you finish learning, always use external stylesheets. Link them in the <head>: <link rel="stylesheet" href="styles.css">
Mini Challenge: Style a Page

Task: Take the plain HTML below and style it using an internal stylesheet. Do NOT edit the HTML โ€” only add CSS in the <style> tag.

  • Change the <body> background to a light color (e.g., #f0f0f0).
  • Set a nice font-family on the body (e.g., Arial, sans-serif).
  • Style the <h1> with a color and centered alignment.
  • Add padding and border-radius to all <p> elements to make them look like cards.
  • Add a subtle border to the paragraphs.

Hint: Use the properties: background-color, font-family, color, text-align: center, padding, border-radius, and border. Example: p { padding: 16px; border: 2px solid #ddd; border-radius: 8px; }

Key takeaways

  • CSS controls presentation. HTML defines structure, CSS controls appearance.
  • CSS syntax: selector { property: value; }
  • Three ways to add CSS: inline (quick testing), internal (single page), external (best practice).
  • The cascade: More specific selectors override general ones. IDs > Classes > Elements.
  • Common properties: color, background-color, font-size, padding, margin, border, border-radius.
  • Always use external stylesheets in production. They're reusable, cacheable, and maintainable.

Test your knowledge

Question 1 of 5
What does CSS stand for?
Question 2 of 5
Which method of adding CSS is best for production websites?
Question 3 of 5
What is the correct CSS syntax?
Question 4 of 5
Which CSS method uses the style attribute directly on an HTML element?
Question 5 of 5
What does "cascading" mean in CSS?
0
/5
Great start! You understand CSS basics.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You just styled your first page.
Imagine what you'll design next.

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’