All subjects · Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript

The cascade, inheritance and the CSS box model

Edexcel IAL Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript

When several rules apply to one element, the cascade decides which wins; inheritance passes some styles down; the box model decides how much space every element takes (spec 8.1.5 and 8.1.6).

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

The cascade (8.1.5)

When rules conflict, the browser decides in this order:

1. Importance: a declaration marked !important wins (avoid it; it makes code hard to maintain).

2. Specificity: the more specific selector wins. Inline style beats ID, ID beats class (and attribute and pseudo-class), class beats type.

3. Source order: if specificity is equal, the rule that comes last wins. This includes the order of linked and internal style sheets in the head.

p { color: black; }
.intro { color: green; }
#first { color: purple; }
p { color: grey; }

For <p id="first" class="intro"> the text is purple, because the ID selector is the most specific. A plain p is grey: the two p rules are equally specific, so the later one wins.

Inheritance and efficiency

Some properties are inherited by child elements: mainly text properties such as color, font-family, font-size, line-height and text-align. Box properties such as border, margin, padding, width and background are not inherited.

So setting body { font-family: Arial, sans-serif; color: #222; } styles all the text on the page in one rule. This is efficient CSS: set shared styles once on a parent, group selectors, and use classes rather than repeating declarations.

The box model (8.1.6)

Every element is a rectangular box with four layers, from the inside out: content, padding (space inside the border, which shows the background), border, and margin (transparent space outside the border, separating it from other elements).

.card {
  width: 200px;
  padding: 10px;
  border: 5px solid black;
  margin: 20px;
}

By default (box-sizing: content-box), width sets only the content, so the visible width is 200 + 2 x 10 + 2 x 5 = 230 pixels, and with margins the box takes up 270 pixels. With box-sizing: border-box; the width includes padding and border, so the visible width is exactly 200 pixels, which makes layouts easier to calculate.

Worked example

A box has width: 300px, padding: 15px, border: 2px solid and margin: 10px, with the default content-box sizing. How wide is it from the outside of one border to the other?

  1. Content 300
  2. Padding on both sides: 2 x 15 = 30
  3. Border on both sides: 2 x 2 = 4
  4. Total 300 + 30 + 4 = 334

Answer: 334 pixels (margins are outside the border)

Key idea: Conflicts are settled by importance, then specificity (inline > id > class > type), then source order (last wins). Text properties inherit; box properties do not. Box model: content, padding, border, margin; by default width is the content only.

Check yourself

  1. With default content-box sizing, a div has width: 250px, padding: 20px and border: 3px solid red. What is its total visible width, outside edge of border to outside edge, in pixels?
    Show answer

    296 pixels. 250 + 2 x 20 + 2 x 3 = 296.

  2. An element `<h2 id="top" class="title">` has rules h2 { color: blue; }, .title { color: red; } and #top { color: green; }, in that order. What colour is it?
    • Blue
    • Red
    • Green
    • Black
    Show answer

    Green. The ID selector is the most specific, so green wins whatever the order.

  3. Two rules have equal specificity: p { color: red; } then later p { color: blue; }. What colour are paragraphs?
    • Blue
    • Red
    • Purple
    • The browser default
    Show answer

    Blue. With equal specificity, the later rule wins.

  4. Which property is inherited by child elements by default?
    • border
    • margin
    • padding
    • font-family
    Show answer

    font-family. Text properties such as font-family and color inherit; box properties do not.

  5. What is the difference between padding and margin?
    • Padding is outside the border; margin is inside
    • Padding is space inside the border; margin is space outside the border
    • Padding only applies to text; margin only to images
    • They are two names for the same space
    Show answer

    Padding is space inside the border; margin is space outside the border. Padding sits between content and border and shows the background; margin is transparent space outside the border.

  6. Which value of box-sizing makes width include the padding and border? Type the value.
    Show answer

    border-box. box-sizing: border-box includes padding and border in the width and height.

  7. Two block boxes are stacked. The upper one has margin-bottom: 30px and the lower one has margin-top: 20px. Vertical margins between blocks collapse into the larger of the two. How many pixels apart are their borders?
    Show answer

    30 pixels. The margins collapse to the larger value: 30 pixels.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.