WDD  Β·  Web Design & Development

CSS: Selectors, Properties and Box Model

Lesson 6 of 13 Approx 3 hrs (split over two periods)

Learning intentions

  • Understand what CSS is and how it controls the appearance of a web page
  • Know the syntax of a CSS rule: selector, property, and value
  • Know the three ways to apply CSS: inline, internal, and external
  • Know and correctly use element, class, and ID selectors
  • Understand the CSS box model: content, padding, border, and margin

Success criteria

  • I can write a correctly structured CSS rule with a selector, property, and value
  • I can write element, class, and ID selectors to target specific HTML elements
  • I can use common CSS properties: color, background-color, font-size, font-family, font-weight, text-align, border, padding, margin
  • I can describe the four layers of the box model in order: content, padding, border, margin
  • I can explain the difference between padding and margin
Warm up β€” what do you already know?

Answer before the lesson begins.

1. What does CSS stand for?

2. Which CSS selector targets elements with the class highlight?

3. In the CSS box model, what is the correct order from the innermost to the outermost layer?

Key vocabulary

CSS
Cascading Style Sheets β€” the language used to control the visual appearance of HTML elements: colours, fonts, sizes, spacing, and layout.
stylesheet
A collection of CSS rules. Can be written inline, in a <style> block, or in a separate .css file.
selector
The part of a CSS rule that identifies which HTML element(s) to style. E.g. p, .card, #header.
property
What you are styling β€” e.g. color, font-size, margin. Goes before the colon in a declaration.
value
The setting you are applying to the property β€” e.g. red, 1.2rem, 10px. Goes after the colon in a declaration.
declaration
A single property-value pair inside a CSS rule: color: red;. Semicolons separate declarations; the final semicolon is recommended but may be omitted.
inline style
CSS written directly inside an HTML element using the style attribute: <p style="color:red">. Highest specificity but hardest to maintain.
internal stylesheet
CSS rules written inside a <style> element in the <head> of an HTML page. Applies to that page only.
box model
The model that describes how every HTML element is treated as a rectangular box with four layers: content, padding, border, and margin.
padding
Space between the content and the border. Padding is inside the element β€” it takes the element's background colour.
margin
Space outside the border, between this element and neighbouring elements. Margin is transparent β€” it does not take any colour.
class / ID selector
Class selector (.name) targets any element with that class. ID selector (#name) targets one unique element with that id.

CSS: Selectors, Properties and Box Model

What is CSS?

CSS (Cascading Style Sheets) is the language used to control how HTML pages look. While HTML defines the structure and content of a page ("this is a heading", "this is a paragraph"), CSS controls the presentation β€” colours, fonts, sizes, spacing, and layout. Separating structure (HTML) from presentation (CSS) makes websites easier to maintain and update.

CSS rule syntax

A CSS rule has two parts: a selector that identifies which element to style, and a declaration block containing one or more property-value pairs:

p {
  color: #1a1a2e;
  font-size: 1rem;
  line-height: 1.7;
}

Semicolons (;) separate declarations, so they are required between declarations. A semicolon after the final declaration before } is not required by CSS syntax, but including it is strongly recommended. The declaration block is wrapped in curly braces { }.

Three ways to apply CSS

There are three places you can write CSS:

MethodWhere it goesApplies to
InlineInside an HTML element's style attributeThat one element only
InternalInside a <style> element in <head>That one HTML page only
ExternalIn a separate .css file, linked with <link>Every page that links to it
/* Inline β€” directly on the element */
<p style="color: red; font-weight: bold;">Important text</p>

/* Internal β€” in the head */
<style>
  h1 { color: #042C53; }
</style>

Selectors

The selector determines which elements the rule applies to. The three main types are:

Selector typeSyntaxTargetsExample
ElementpAll <p> elements on the pagep { color: navy; }
Class.highlightAll elements with class="highlight".highlight { background-color: yellow; }
ID#headerThe one element with id="header"#header { font-size: 2rem; }
p Element selector Targets ALL <p> elements (0, 0, 0, 1) .intro Class selector Targets elements with class="intro" (0, 0, 1, 0) #header ID selector Targets ONE element with id="header" (0, 1, 0, 0)

Specificity shown as (inline, IDs, classes, elements)

When rules conflict, CSS compares specificity as four separate counts: (inline styles, IDs, classes, elements). Compare the columns from left to right; the first different column decides which rule wins. Do not add the counts into a decimal score. For example, one ID beats any number of classes β€” even 11 classes do not beat one ID.

Common properties

PropertyWhat it controlsExample value
colorText colour#1a1a2e
background-colorBackground fill colour#f8f8fc
font-sizeText size1.2rem
font-familyTypefacesans-serif
font-weightBold / normal thicknessbold
text-alignHorizontal text alignmentcenter
borderElement border (recommended order: width style colour)2px solid #185FA5
paddingSpace inside element (between content and border)1rem
marginSpace outside element (between element and neighbours)20px
width / heightElement dimensions300px

The CSS box model

Every HTML element is treated as a rectangular box. The box has four layers, from the inside out:

  1. Content β€” the actual text, image, or other element inside
  2. Padding β€” transparent space between the content and the border; takes the element's background colour
  3. Border β€” a visible line around the padding (can be styled with width, style, colour)
  4. Margin β€” transparent space outside the border, between this element and others
MARGIN BORDER PADDING CONTENT text / image / etc.

The four layers of the box model β€” innermost to outermost.

Memory aid: Think of a framed picture on a wall. The content is the photograph. The padding is the white mount board between the photo and the frame. The border is the picture frame itself. The margin is the gap between this picture and the next one on the wall.

How padding affects size: With the default box-sizing: content-box, a declared width applies only to the content, so padding and borders add to the box's outer size. With box-sizing: border-box (used by this lesson page), padding and borders are included within the declared width or height, reducing the space available for the content instead.

Padding and margin shorthand

Both padding and margin accept shorthand values to set multiple sides at once:

.box {
  padding: 10px;           /* all four sides */
  padding: 10px 20px;      /* top/bottom  left/right */
  padding: 5px 10px 15px 20px; /* top right bottom left (clockwise) */
  margin: 0 auto;          /* can centre a constrained-width block */
}

margin: 0 auto visually centres a block only when it has a constrained width (using width or max-width) and there is spare horizontal space. A block already filling the available width has nothing to centre.

Worked examples

Example 1 β€” Element selector rule

Write a CSS rule to make all <h1> elements: colour #1a1a2e, font-size 2rem, and text-align center.

1
The selector is h1 β€” targeting all h1 elements. No dot or hash needed for element selectors.
2
Open the declaration block with {. Separate property-value pairs with semicolons; include a final semicolon as recommended practice.
3
Close the block with }. Check all three properties are present.
h1 {
  color: #1a1a2e;
  font-size: 2rem;
  text-align: center;
}
Example 2 β€” Describing the box model

Annotate each layer of the box model for an element with padding: 15px, border: 2px solid #185FA5, and margin: 20px.

1
Content: the text or image inside the element β€” the innermost layer.
2
Padding (15px): 15 pixels of space all around the content, between the content and the border. Has the same background colour as the element.
3
Border (2px solid #185FA5): a 2-pixel solid blue border drawn around the padding.
4
Margin (20px): 20 pixels of transparent space outside the border, pushing neighbouring elements away.

Total space on each side taken up (not including content): 15px padding + 2px border + 20px margin = 37px per side.

Example 3 β€” Class selector with multiple properties

Write a CSS class rule called .card to give elements: background-color #f0f0f8, border 1.5px solid #185FA5, padding 1rem, and border-radius 8px.

1
The selector is .card β€” note the dot prefix for a class.
2
Write border shorthand using the readable convention width, style, colour β€” no commas between the three values. CSS also accepts these components in other orders.
3
Use semicolons to separate declarations and, as recommended practice, after the final declaration. The block ends with }.
.card {
  background-color: #f0f0f8;
  border: 1.5px solid #185FA5;
  padding: 1rem;
  border-radius: 8px;
}

To apply this to an element in HTML: <div class="card">...</div>

Now you try

Write an internal <style> block for a page about a school club. Your rules must:

  • Set the body background-color to #f8f8fc
  • Make all h2 elements colour #185FA5 and font-weight bold
  • Give the class .box a 2px solid border with colour #185FA5 and padding of 10px
<style>
  body {
    background-color: #f8f8fc;
  }

  h2 {
    color: #185FA5;
    font-weight: bold;
  }

  .box {
    border: 2px solid #185FA5;
    padding: 10px;
  }
</style>
Common mistakes
βœ•
Forgetting a semicolon between declarations. In color: red followed by another declaration, the missing separator can cause the declarations to fail. A trailing semicolon after the final declaration is optional, but strongly recommended.
βœ•
Confusing color and background-color. color sets the text colour only. background-color sets the fill behind the element. Using color: blue turns the text blue β€” it does not change the background.
βœ•
Missing the dot on a class selector. Writing highlight { } instead of .highlight { } targets a non-existent HTML element called <highlight>, not elements with class="highlight". The dot is essential for class selectors.
βœ•
Confusing padding and margin. Padding is space inside the element β€” it shows the element's background colour. Margin is space outside the element β€” it is always transparent. With content-box, padding adds to the outer size; with border-box, it fits inside the declared size and reduces the content area. Margin pushes the element away from neighbours.
βœ•
Leaving out a border shorthand component. A readable convention is border: width style colour β€” e.g. border: 2px solid red. The components may appear in any order, so solid 2px red is also valid CSS.
Exam tip

Box-model knowledge is commonly examined at N5. Be ready to name all four layers in order: content, padding, border, margin (inside to outside). If asked to "describe" or "explain" the box model, give one sentence per layer. For padding vs margin, the key distinction is: padding is inside the border; margin is outside the border.

For "write a CSS rule" questions, always check: (1) correct selector syntax β€” dot for class, hash for ID; (2) correct property name spelling β€” especially background-color with a hyphen; (3) semicolons between declarations, with a final semicolon as recommended practice; (4) closing curly brace.

Task Set

Questions 1–4 are auto-checked. Questions 5–10 are self-marked β€” write your answer, then reveal the model answer to check your work.

1. Which of the following is a correctly written CSS rule? TYPE 1

2. What does the CSS margin property control? TYPE 1

3. Which CSS selector targets elements with class="intro"? TYPE 1

4. Where does an internal stylesheet go in an HTML page? TYPE 1

5. Name the four layers of the CSS box model in order, from innermost to outermost. For each layer, write one sentence describing what it is. (4 marks) TYPE 2

1. Content β€” the actual text, image, or other content inside the element.
2. Padding β€” the space between the content and the border; has the same background colour as the element.
3. Border β€” a visible line drawn around the padding; can be styled with width, style, and colour.
4. Margin β€” transparent space outside the border; pushes neighbouring elements away.

1 mark per correctly named and described layer.

6. Identify the errors in this CSS snippet. There are four errors. State each error and how to fix it. (4 marks) TYPE 2

h2 {
  colour: #185FA5
  font-weight bold;
}
.card
  padding: 1rem;
}
1. colour is misspelled β€” CSS uses the American spelling color (no 'u').
2. Missing semicolon after #185FA5 β€” a semicolon is required here to separate it from the next declaration: color: #185FA5;
3. Missing colon in font-weight bold β€” should be font-weight: bold;
4. Missing opening brace { after .card β€” the declaration block must open with a curly brace.

1 mark for each correctly identified and fixed error.

7. Write three CSS rules: (a) make all <p> elements have a font-size of 1rem and line-height of 1.6; (b) make elements with class .highlight have a yellow background-color and bold font-weight; (c) make the element with id page-title have text-align center and colour #042C53. (3 marks) TYPE 2

p {
  font-size: 1rem;
  line-height: 1.6;
}

.highlight {
  background-color: yellow;
  font-weight: bold;
}

#page-title {
  text-align: center;
  color: #042C53;
}
1 mark per correct rule (correct selector prefix + correct properties + required declaration separators + braces).

8. Write a CSS class rule called .info-box with these four properties: background-color #E6F1FB; border 1.5px solid #185FA5; padding 1rem; border-radius 8px. (2 marks) TYPE 2

.info-box {
  background-color: #E6F1FB;
  border: 1.5px solid #185FA5;
  padding: 1rem;
  border-radius: 8px;
}
Marks: correct selector with dot (1), all four correct properties and values with required declaration separators (1).

9. Explain the difference between margin and padding in CSS. (2 marks) TYPE 2

Padding is the space between the content and the border, inside the element. It takes the element's background colour, so it appears as part of the element.

Margin is the space outside the border, between the element and its neighbours. It is always transparent β€” it does not take any background colour.

1 mark for a correct description of padding, 1 mark for a correct description of margin.

10. Write a complete internal <style> block for a page about a sports club. It must include: a body rule setting background-color and font-family; an h1 rule setting color, font-size, and text-align; a .panel class rule setting padding, border, and background-color. (4 marks) TYPE 2

<style>
  body {
    background-color: #f8f8fc;
    font-family: sans-serif;
  }

  h1 {
    color: #042C53;
    font-size: 2rem;
    text-align: center;
  }

  .panel {
    padding: 1rem;
    border: 2px solid #185FA5;
    background-color: #E6F1FB;
  }
</style>
Marks: <style> tags used (1), body rule with correct properties (1), h1 rule with correct properties (1), .panel class rule with dot prefix and correct properties (1).
Teacher notes β€” Shift+T to hide

Suggested timing: Two periods. Period 1 (1hr): warm-up, CSS syntax, selectors, common properties, box model concept (stop after box model diagram). Period 2 (2hr): box model SVG recap, shorthand, now you try, task set Q1–7. Q8–10 as extension or homework.

Box model analogy: Use a framed picture on a wall β€” content=photo, padding=mount board, border=frame, margin=gap between frames. Draw it on the board. Then point to the SVG diagram on the page. Box-model knowledge is commonly examined at N5.

Most consistent errors: (1) American spelling β€” color not colour, background-color not background-colour. (2) Missing semicolons. (3) Missing dot on class selector. Drill these repeatedly before the task set.

Q9 (margin vs padding): prepare pupils to write at least one sentence for each with the keyword "inside/outside the border" to distinguish them clearly; this supports commonly examined box-model knowledge.