🎨 CSS Beginner

Introduction to CSS

CSS का परिचय

⏱ 1 hr3 topicsLive playground
🎯 By the end: You can write a CSS rule, link a stylesheet the recommended way, and target elements with element, class and id selectors.

You've built structure with HTML — the bricks of the house. Now comes the paint, the colour, the layout: CSS, Cascading Style Sheets. This is where pages stop looking like a 1995 document and start looking like a real website. And because you'll see every change live in the playground, it's genuinely fun.

1The anatomy of a CSS rule

CSS works by picking elements and giving them style. A single rule looks like this:

h1{color:orange; }selectorpropertyvalue
  • The selector (h1) chooses which elements to style.
  • Inside the braces are declarations: a property (color) and a value (orange), separated by a colon and ended with a semicolon.

You can stack as many declarations as you like inside one rule — color, font-size, padding, and so on, each ending in a semicolon.

Your first CSS
HTML
CSS
Live preview
What's happening: Each rule has a selector (h1, p) and declarations inside the braces. Try changing the color value or the font-size — the preview updates instantly.
Key points
  • A CSS rule = selector + declarations inside braces.
  • A declaration is property: value; — e.g. color: orange;.
  • You can put many declarations in one rule, each ended with a semicolon.

2Three ways to add CSS

MethodHowVerdict
InlineA style attribute on one element: <p style="color:red">Avoid — hard to maintain, can't reuse.
InternalA <style> block in the <head>Fine for a single page or a quick demo.
ExternalA separate .css file linked in the headBest. One file styles your whole site.

The recommended way is an external stylesheet, linked once in the head:

<link rel="stylesheet" href="styles.css">
One external file means you change a colour once and every page updates. That's the "Cascading" power of CSS working for you.
Key points
  • Inline (style attribute) — avoid; can't reuse and clutters HTML.
  • Internal (<style> in the head) — fine for one page.
  • External (<link rel="stylesheet">) — best; one file styles the whole site.

3Selectors — choosing what to style

Selectors are how you point CSS at the right elements. The three you'll use constantly:

SelectorTargetsExample
Elementevery element of that typep { } — all paragraphs
Class (.)any element with that class.card { }<div class="card">
id (#)the one element with that id#header { }<div id="header">

You'll lean on class selectors most — they're reusable across many elements, exactly as you learned in the Attributes lesson. Edit the playground below and watch the .highlight class style only the elements that carry it.

Element, class and id selectors
HTML
CSS
Live preview
What's happening: The element selector p styles all paragraphs. The class .highlight styles only the two paragraphs that have class="highlight". The id #title styles the single element with id="title". Add class="highlight" to the first paragraph and watch it join in.
Key points
  • Element selector (p) targets every element of that type.
  • Class selector (.name) targets any element with that class — reusable, your everyday tool.
  • id selector (#name) targets the single element with that id.

★ Practical: style a tiny page

In any playground, write CSS that:

  1. Sets the color and font-family of all paragraphs with an element selector.
  2. Creates a .box class with a background colour and some padding, and applies it to two elements.
  3. Uses an id selector to style one unique heading.
  4. Confirm the class styles multiple elements while the id styles only one.

Ready to test yourself?

Take the short quiz. Score 60% or more to mark this lesson complete.

Start the quiz →

💡 Log in to save your progress and earn the certificate.