Reading history

CSS Introduction: Selectors, Box Model and Flexbox

4 min read 3 views

Learn CSS basics: how rules work, three ways to add CSS, element, class and ID selectors, specificity, the box model and a first Flexbox layout.

CSS (Cascading Style Sheets) controls how an HTML page looks: colours, fonts, spacing, and where boxes sit on the screen. HTML says “this is a heading”; CSS says “make headings dark blue and centred”. One stylesheet can style an entire website.

In this lesson you will learn

  • How a CSS rule is written
  • Three ways to add CSS to a page
  • Element, class and ID selectors
  • The box model: content, padding, border and margin
  • A first look at Flexbox layout

How a CSS Rule Works

A rule has a selector, which picks elements, and a block of declarations, each a property and a value ending in a semicolon.

h1 {                 /* selector */
  color: navy;       /* property: value; */
  font-size: 32px;
}

Three Ways to Add CSS

MethodExampleWhen to use
External file<link rel="stylesheet" href="style.css"> in <head>Almost always: one file styles every page
Internal<style> p { color: red; } </style> in <head>A single page or quick tests
Inline<p style="color: red">Rarely: hard to maintain

Selectors: Element, Class and ID

/* Element selector: every paragraph */
p {
  color: #333333;
  font-size: 18px;
  line-height: 1.6;
}

/* Class selector: any element with class="note" */
.note {
  background-color: #fff6d6;
  border-left: 4px solid #e0a800;
  padding: 12px 16px;
}

/* ID selector: the one element with id="site-title" */
#site-title {
  text-align: center;
}

With that stylesheet, every <p> becomes dark grey and easier to read, any element with class="note" gets a pale yellow box with a gold stripe on the left, and the single element with id="site-title" is centred.

  • Element selector (p) styles every element of that type.
  • Class selector (.note) styles any element with that class; many elements can share it.
  • ID selector (#site-title) styles one unique element.

When several rules target the same element, the more specific one wins: an ID beats a class, and a class beats an element. If two rules are equally specific, the one that comes later wins; that is the “cascade” in the name.

The Box Model and Flexbox

The browser treats every element as a rectangle made of four layers: the content, then padding around it, then a border, then margin separating it from its neighbours.

.card {
  width: 300px;
  padding: 20px;          /* space inside the border */
  border: 2px solid #2b6cb0;
  margin: 24px auto;      /* space outside; auto centres it */
  box-sizing: border-box; /* width now includes padding and border */
}

.menu {
  display: flex;
  justify-content: space-between;
  gap: 16px;
}
  • padding adds space inside the border; margin adds space outside it.
  • margin: 24px auto centres a block that has a fixed width.
  • box-sizing: border-box makes width include padding and border, so the card is exactly 300px wide. Many developers apply it to every element.
  • display: flex lines up an element’s children in a row. justify-content spreads them out and gap puts space between them, the easiest way to build a navigation menu.

Common CSS Mistakes

  1. Missing semicolons. One missing ; can break the next declaration.
  2. Forgetting the dot or hash. note { } looks for a <note> element, not class="note".
  3. Wrong stylesheet path. If style.css is in another folder, the href must point there.
  4. Overusing !important. It hides specificity problems instead of fixing them.
  5. Fixed widths everywhere. Use percentages, max-width and Flexbox so pages fit phones too.

Practice Exercises

Exercise 1. Write a rule that makes every <h2> green and 24px.

Show answer
h2 { color: green; font-size: 24px; }

Exercise 2. A <p> has both class="note" and id="intro". A class rule sets it blue and an ID rule sets it red. Which colour wins?

Show answer

Red. An ID selector is more specific than a class selector.

Exercise 3. A box has width: 200px; padding: 10px; border: 5px solid and no box-sizing. How wide is it on screen?

Show answer

200 + 10 + 10 + 5 + 5 = 230px. With box-sizing: border-box it would be exactly 200px.

Frequently Asked Questions

What is the difference between a class and an ID?

A class can be used on many elements; an ID must be unique on the page. Use classes for styling and save IDs for unique elements and links within the page.

Should I learn Flexbox or Grid first?

Flexbox, for lining items up in one row or column. Learn CSS Grid next for full two-dimensional page layouts.

Do I need a framework like Bootstrap?

Not to start. Learn plain CSS first; frameworks make much more sense once you know what they do for you.

Summary

  • A CSS rule is a selector plus property-value declarations.
  • Link an external stylesheet for real sites.
  • Element, class and ID selectors target elements with increasing specificity.
  • Every element is a box: content, padding, border and margin.
  • display: flex is the simplest way to line items up.
Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *