CSS Fundamentals MCQs for the NSCT
CSS fundamentals means the rules a browser uses to decide which style applies to which element and how big each box is: selectors, specificity and the cascade, the box model, and length units. It is the fourth topic in the Web Development area, which carries 10% of the NSCT according to HEC's Areas of Competencies.
Most questions here are small puzzles: which rule wins, how wide a box ends up, or what size an em resolves to. Each can be worked out exactly, so careful arithmetic beats memorising. It builds directly on HTML Fundamentals.
Selectors
A selector is the pattern that picks which elements a rule styles. The combinators between simple selectors matter most:
| Selector | Matches |
|---|---|
.note |
Elements with class note |
#main |
The element with id="main" |
article p |
Any p inside an article, at any depth (descendant) |
div > p |
A p that is a direct child of a div |
h2 + p |
The p immediately after an h2 (adjacent sibling) |
h2 ~ p |
Every later p sibling of an h2 (general sibling) |
Pseudo-classes such as :hover and :nth-child(2n+1) select whole elements by state or position. Pseudo-elements such as ::before and ::first-line style a part of an element.
Exam angle: p:first-child matches nothing if an h2 comes first in the parent. Use p:first-of-type for the first paragraph.
Specificity
When two rules set the same property on an element, the cascade decides the winner in this order: importance (!important), then specificity, then source order (later wins). Specificity is a three-part score (IDs, classes, types), compared column by column from the left:
- IDs add to the first column.
- Classes, attribute selectors and pseudo-classes add to the second.
- Type selectors and pseudo-elements add to the third.
*and combinators add nothing.:where()adds zero;:is()and:not()add their most specific argument.
| Selector | Specificity |
|---|---|
nav ul li a |
(0, 0, 4) |
.menu a:hover |
(0, 2, 1) |
#list .done |
(1, 1, 0) |
An inline style attribute beats every normal stylesheet rule, but an !important declaration in a stylesheet beats a normal inline style.
Exam angle: one ID beats any number of classes. (0, 3, 0) still loses to (1, 0, 0).
Box model
Every element is a rectangular box made of four layers: content, padding, border and margin. The box-sizing property decides what width measures:
| box-sizing | width covers |
Card: width 300px, padding 24px, border 1px |
|---|---|---|
content-box (default) |
Content only | Border box = 300 + 48 + 2 = 350px |
border-box |
Content + padding + border | Content = 300 − 48 − 2 = 250px |
Margins sit outside the border and never count in either width. In normal flow, the vertical margins of adjacent blocks collapse: margins of 30px and 20px leave a 30px gap, not 50px.
Exam angle: percentage padding, even top and bottom, is worked out from the containing block's width.
Units
Absolute units such as px are fixed. Relative units scale with something else:
| Unit | Relative to |
|---|---|
em |
The parent's font size (for font-size); the element's own font size for other properties |
rem |
The root (html) font size |
% |
Usually the containing block's size |
vw, vh |
1% of the viewport width or height |
Example: with html { font-size: 16px }, h1 { font-size: 2rem } is 32px. A .note { font-size: 0.875em } inside a 20px parent is 17.5px.
Exam angle: nested em font sizes compound (1.5em three levels deep is 16 × 1.5³ = 54px). rem never compounds.
Common traps
- Thinking source order beats specificity. Order only breaks ties.
- Adding margins into an element's width. Margins are outside the border box.
- Adding vertical margins between stacked blocks instead of taking the larger one.
- Forgetting that
widthwith the defaultcontent-boxexcludes padding and border. - Using the parent's font size for
padding: 1em. Outsidefont-size, em uses the element's own size. - Assuming inherited values beat browser defaults. A link stays blue in a red
divbecause the browser's ownarule targets it directly.
Worked examples
1. Understanding. Which combinator selects a p at any depth inside an article?
Answer: the descendant combinator, a space: article p. The child combinator article > p would miss paragraphs nested inside a section.
2. Application. ul#list li.done { color: grey } and #list .done { color: green } both match the same li. Which colour wins?
Answer: grey. The first selector is (1, 1, 2) and the second is (1, 1, 0). The IDs and classes tie, so the type column decides, whatever the source order.
3. Analysis. Three cards with width: 300px; padding: 24px; border: 1px solid sit in a 900px flex row with flex-wrap: wrap, and the third wraps. Why, and what is the cleanest fix?
Answer: with content-box each card is 350px wide, so three need 1,050px. Adding box-sizing: border-box keeps each card at 300px, so all three fit in 900px.
Practise
The quiz below has 100 questions on CSS fundamentals, filterable by tier and difficulty.
Practise: 100 questions
FAQs
How do I calculate CSS specificity quickly?
Count three numbers: IDs, then classes, attributes and pseudo-classes, then types and pseudo-elements. Ignore * and combinators. Compare from the left: the first column that differs decides. If all three tie, the rule written later wins. Inline styles beat all of these unless a stylesheet rule uses !important.
Should I use px, em or rem?
Use rem for font sizes and spacing that should scale with the user's base font size without compounding. Use em when something should scale with its own component's text, such as button padding. Keep px for things that should not scale, such as hairline borders. Percentages and viewport units suit layout widths.
Why do developers set box-sizing: border-box on everything?
Because it makes width mean the visible width of the box, padding and border included. Layout maths becomes simpler: two 50% columns with padding fit side by side instead of overflowing. The usual reset is *, *::before, *::after { box-sizing: border-box; }.
Is CSS Grid or Flexbox part of this topic?
No. Layout systems are covered in Advanced CSS & Responsive Design. This topic covers the base rules every layout depends on: selectors, the cascade, the box model and units.
Next step
Move on to Advanced CSS & Responsive Design, go back to the Web Development MCQs hub, or test yourself in an NSCT mock test.