CSS 3 min read

CSS Basics: Selectors, the Box Model and the Cascade

CSS controls how every web page looks. This beginner-friendly guide explains selectors, the box model, the cascade and specificity — the core ideas behind every layout and design.

On this page

Key takeaways

  • CSS (Cascading Style Sheets) controls how HTML looks: colours, fonts, spacing, layout and responsive behaviour.
  • Three ideas explain most CSS behaviour: selectors, the box model and the cascade (with specificity).
  • Small CSS knowledge goes a long way when customising WordPress themes safely.

If you’ve ever added “Additional CSS” in the WordPress Customizer and watched nothing happen, you’ve met the cascade. CSS is simple to start and full of surprises later. This guide explains the core concepts so you can read, write and debug styles with confidence.

How CSS is written#

/* selector   { property: value; } */
.ikp-btn--primary {
  background: #7ed957;
  color: #0b0d12;
  padding: 12px 22px;
  border-radius: 999px;
}

A rule has a selector (which elements), and declarations (what to change). Styles can live in an external stylesheet (best), a <style> block, or inline style="" attributes (avoid for anything reusable).

Selectors you’ll use daily#

Selector Matches Example
p Every element of that type All paragraphs
.card Elements with a class <div class="card">
#site-header The element with an id Avoid for styling — too specific
.card h3 Descendants H3s anywhere inside .card
.menu > li Direct children Top-level menu items only
a:hover, :focus-visible States Hovered links, keyboard focus
li:first-child, :nth-child(2n) Position Zebra-striped rows
[data-theme="dark"] Attributes Dark-mode overrides
.card:has(img) Parents containing something Cards with images (modern browsers)

The box model#

Every element is a rectangular box made of four layers: content, padding (space inside the border), border, and margin (space outside). By default, width applies only to the content box, so padding and border make elements wider than you expect. Almost every modern site starts with:

*, *::before, *::after { box-sizing: border-box; }

With border-box, a 300 px wide element stays 300 px wide including padding and border.

The cascade and specificity#

When several rules target the same element, the browser decides which wins in this order:

  1. Importance: !important declarations beat normal ones (use rarely).
  2. Specificity: inline style > ID > class/attribute/pseudo-class > element. .nav a (class + element) beats a.
  3. Source order: if specificity is equal, the later rule wins.
Why your WordPress CSS doesn’t apply The theme’s selector is probably more specific than yours. Inspect the element in DevTools, copy the exact selector the theme uses, and match or slightly exceed it — instead of reaching for !important.

Need a hand with this?

Get a quick, honest review of your website’s SEO.

Chat on WhatsApp

Inheritance and the cascade in practice#

Some properties inherit from parents (colour, font, line-height); others don’t (margin, border, background). That’s why setting font-family on body styles the whole page. Modern CSS also has custom properties (variables) that inherit, which make theming and dark mode easy — see CSS variables and dark mode.

Layout in one minute#

  • Normal flow: block elements stack vertically; inline elements flow in lines.
  • Flexbox: one-dimensional layouts — navbars, button groups, centring. See Flexbox guide.
  • Grid: two-dimensional layouts — card grids, page templates. See CSS Grid guide.
  • Position: sticky headers, fixed chat buttons, absolute badges.

Debugging with DevTools#

  1. Right-click an element → Inspect.
  2. In the Styles panel, see every rule that applies; crossed-out declarations were overridden.
  3. Edit values live to test changes before saving them in your theme.
  4. Use the Computed tab to see final values and the box model diagram.

Where to add custom CSS in WordPress#

  • Customizer → Additional CSS for small tweaks (stored in the database, survives theme updates).
  • Child theme stylesheet for larger changes — see how to create a child theme.
  • Block styles / Site Editor for block themes.
  • Never edit a parent theme’s files directly — updates overwrite them.

CSS basics FAQ#

Does CSS affect SEO?#

Indirectly. CSS affects speed, mobile usability and layout stability. Hiding content with CSS to manipulate rankings is against Google’s guidelines, but tabs and accordions for usability are fine.

Should I use a CSS framework?#

Frameworks like Tailwind or Bootstrap speed up development but add learning and sometimes weight. See Tailwind vs custom CSS.

Why avoid !important?#

It breaks the normal cascade, making later changes harder. Use it only to override inline styles or third-party code you can’t edit.

Found this useful? Share it

Join the discussion

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

Security code image. If you cannot read it, press refresh or contact me on WhatsApp.