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
paddingandmargin
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
<style> block, or in a separate .css file.p, .card, #header.color, font-size, margin. Goes before the colon in a declaration.red, 1.2rem, 10px. Goes after the colon in a declaration.color: red;. Semicolons separate declarations; the final semicolon is recommended but may be omitted.style attribute: <p style="color:red">. Highest specificity but hardest to maintain.<style> element in the <head> of an HTML page. Applies to that page only..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:
| Method | Where it goes | Applies to |
|---|---|---|
| Inline | Inside an HTML element's style attribute | That one element only |
| Internal | Inside a <style> element in <head> | That one HTML page only |
| External | In 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 type | Syntax | Targets | Example |
|---|---|---|---|
| Element | p | All <p> elements on the page | p { color: navy; } |
| Class | .highlight | All elements with class="highlight" | .highlight { background-color: yellow; } |
| ID | #header | The one element with id="header" | #header { font-size: 2rem; } |
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
| Property | What it controls | Example value |
|---|---|---|
color | Text colour | #1a1a2e |
background-color | Background fill colour | #f8f8fc |
font-size | Text size | 1.2rem |
font-family | Typeface | sans-serif |
font-weight | Bold / normal thickness | bold |
text-align | Horizontal text alignment | center |
border | Element border (recommended order: width style colour) | 2px solid #185FA5 |
padding | Space inside element (between content and border) | 1rem |
margin | Space outside element (between element and neighbours) | 20px |
width / height | Element dimensions | 300px |
The CSS box model
Every HTML element is treated as a rectangular box. The box has four layers, from the inside out:
- Content β the actual text, image, or other element inside
- Padding β transparent space between the content and the border; takes the element's background colour
- Border β a visible line around the padding (can be styled with width, style, colour)
- Margin β transparent space outside the border, between this element and others
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
Write a CSS rule to make all <h1> elements: colour #1a1a2e, font-size 2rem, and text-align center.
h1 β targeting all h1 elements. No dot or hash needed for element selectors.{. Separate property-value pairs with semicolons; include a final semicolon as recommended practice.}. Check all three properties are present.h1 { color: #1a1a2e; font-size: 2rem; text-align: center; }
Annotate each layer of the box model for an element with padding: 15px, border: 2px solid #185FA5, and margin: 20px.
Total space on each side taken up (not including content): 15px padding + 2px border + 20px margin = 37px per side.
Write a CSS class rule called .card to give elements: background-color #f0f0f8, border 1.5px solid #185FA5, padding 1rem, and border-radius 8px.
.card β note the dot prefix for a class.border shorthand using the readable convention width, style, colour β no commas between the three values. CSS also accepts these components in other orders.}..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>
Write an internal <style> block for a page about a school club. Your rules must:
- Set the
bodybackground-color to#f8f8fc - Make all
h2elements colour#185FA5and font-weightbold - Give the class
.boxa 2px solid border with colour#185FA5and padding of10px
<style> body { background-color: #f8f8fc; } h2 { color: #185FA5; font-weight: bold; } .box { border: 2px solid #185FA5; padding: 10px; } </style>
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.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.highlight { } instead of .highlight { } targets a non-existent HTML element called <highlight>, not elements with class="highlight". The dot is essential for class selectors.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.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.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.
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
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;
}
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
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).
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.