University of Newcastle · FACULTY OF COMPUTER SCIENCE

SENG1050 Chap.4 CSS Selectors, Properties and the Box Model

- one subject, every graph, every model, every mark
8 Chapters4-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 4 of 12 · SENG1050

CSS Selectors, Properties and the Box Model

This chapter follows the Week 4 lecture, which the exam review stars for the structure of a CSS file and for selectors.

It explains why styling moved out of HTML, the three ways to attach CSS and when to use each, the cascade order and how specificity is scored, the full range of selector forms, common properties, units and colour values, and the box model, including why a stated width does not include padding, border or margin.

The lab that follows styles the same pages inline, then in an embedded sheet, then from an external file, and tests how the cascade resolves each conflict.

In this chapter

What this chapter covers

  • 01

    Why styling left HTML

  • 02

    Inline, embedded and external CSS

  • 03

    Rule syntax and comments

  • 04

    Cascade order and specificity

  • 05

    Combinators and pseudo-classes

  • 06

    Attribute, ID and class selectors

  • 07

    Units and colour values

  • 08

    The box model and box-sizing

Worked example · free

Work out how much space a styled box occupies

Q [3 marks]. A div has width: 200px, padding: 10px, border: 2px and margin: 15px. How wide is the visible box and how much horizontal space does it occupy? What changes with box-sizing: border-box? The mark allocation here is AskSia's own and is not a University marking scheme.
A styled div drawn as nested layers: the dashed outer band is margin, the dark ring is border, and the labelled numbers show why 200px of width ends up occupyinA styled div drawn as nested layers: the dashed outer band is margin, the dark ring is border, and the labelled numbers show why 200px of width ends up occupying 254px. margin 15px padding 10px content width 200px border 2px occupies 254px across
A styled div drawn as nested layers: the dashed outer band is margin, the dark ring is border, and the labelled numbers show why 200px of width ends up occupying 254px.
  • 1Width applies to the content only, so the visible box from border to border is 200 + 2 x 10 + 2 x 2 = 224px.
  • 1Margins sit outside the border, so the space occupied is 224 + 2 x 15 = 254px.
  • 1With box-sizing: border-box the 200px includes padding and border, so the border box is 200px and the content shrinks to 200 - 20 - 4 = 176px.
The visible box is 224px wide and the element occupies 254px including margins. Under border-box sizing the border box becomes exactly 200px and the content area 176px.
Sia tip — Add the layers in pairs, one for each side: padding twice, border twice, margin twice, then check whether border-box has moved padding and border inside the stated width.
Glossary

Key terms

Cascade
The cascade is the order of priority that decides which style applies when several rules target the same element.
Specificity
Specificity is the weight of a selector, compared by counting IDs, then classes, then element types.
Pseudo-class
A pseudo-class selects elements by state or position, such as a:hover or li:nth-child(2n).
Box model
The box model describes an element as content wrapped in padding, then a border, then a margin.
Margin collapse
Margin collapse is the merging of adjacent vertical margins into a single margin.
Combinator
A combinator joins two selectors by relationship, such as a space for descendants or a plus sign for the next sibling.
FAQ

CSS Selectors, Properties and the Box Model FAQ

Which wins, an ID selector or several classes?

The ID wins. Specificity compares IDs first, and only when the ID counts are equal does it look at classes, then element types. One ID outranks any number of classes, and if two selectors tie in every column the rule applied last takes effect.

When should I use inline, embedded or external CSS?

Put site-wide rules in an external file linked from every page, use an embedded style element for rules that apply to one document, and keep inline style attributes for rare one-off cases. An external file holds only rules, never style or head tags.

Why is my element wider than the width I set?

Width applies to the content area only, so padding and border are added on both sides and margins sit outside that. Setting box-sizing to border-box makes the stated width include padding and border.

What is the difference between a descendant and a child selector?

A descendant selector, written with a space as in div p, matches a paragraph at any depth inside a div. A child selector, written with a greater-than sign as in div > p, matches only paragraphs whose direct parent is the div.

How do em, rem and px differ?

A px is an absolute unit of one ninety-sixth of an inch. An em is relative to the parent element's font size, while a rem is relative to the root element's font size, so rem values stay predictable when elements are nested.

Study strategy

Exam move

Make a single page of selector flashcards: the selector form on one side and an example of what it matches on the other, and test yourself on a small piece of markup. Score six or seven selectors for specificity each session, writing the three counts every time. For the box model, sketch the four layers and recompute one width question a day until the arithmetic is automatic.

Use a temporary border on boxes in your own assignment pages to see where padding ends and margin begins.

Working through CSS Selectors, Properties and the Box Model in SENG1050? Sia is AskSia’s AI Computer Science tutor — ask any SENG1050 CSS Selectors, Properties and the Box Model question and get a clear, step-by-step explanation grounded in how SENG1050 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 14 of your University of Newcastle subjects - and 1,000+ Bibles across every Australian university.
Sia - your SENG1050 tutor, unlimited, worked the way the exam marks it
The full 4-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works