CSS Fundamentals MCQs for the NSCT

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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:

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

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

Interactive quiz loading. The first 10 questions are shown below.

  1. Which selector targets every element whose class attribute includes "note"?

    1. #note
    2. note
    3. .note
    4. *note
    Show answer

    Answer: C. .note

    A full stop before a name is a class selector, so .note matches any element with note among its classes. #note matches an id, a bare word matches an element type, and *note is not a valid selector.

  2. In the CSS box model, which property controls the space between an element's content and its border?

    1. padding
    2. margin
    3. outline
    4. border-spacing
    Show answer

    Answer: A. padding

    Padding sits inside the border, around the content. Margin is outside the border, outline is drawn outside the border without taking space, and border-spacing only applies to table cells.

  3. Which CSS length unit is always relative to the font size of the root (html) element?

    1. rem
    2. em
    3. ex
    4. vw
    Show answer

    Answer: A. rem

    rem stands for root em: 1rem equals the computed font-size of the html element. em is relative to the element's own (or, for font-size, the parent's) font size, ex to the x-height, and vw to the viewport width.

  4. Ignoring !important, which of these declarations wins when all four set the same property on the same element?

    1. A rule using an ID selector
    2. An inline style attribute
    3. A rule using a class selector
    4. A rule using a type (element) selector
    Show answer

    Answer: B. An inline style attribute

    Inline style declarations beat any normal declaration from a stylesheet, whatever its selector. Among stylesheet rules, ID beats class, and class beats type selectors.

  5. What is the initial (default) value of the box-sizing property?

    1. border-box
    2. padding-box
    3. content-box
    4. margin-box
    Show answer

    Answer: C. content-box

    The initial value is content-box, so width and height size only the content area; padding and border are added on top. border-box is the common reset many developers apply, but it is not the default.

  6. What does the selector div > p match?

    1. p elements anywhere inside a div
    2. The div that contains a p
    3. p elements that are direct children of a div
    4. p elements that come immediately after a div
    Show answer

    Answer: C. p elements that are direct children of a div

    > is the child combinator: it matches only direct children. A space (div p) is the descendant combinator, which matches at any depth. CSS selectors always select the right-most element, never the parent.

  7. Which elements does the selector h2 + p match?

    1. Every p that comes after an h2 with the same parent
    2. Both the h2 and the next p
    3. A p nested inside an h2
    4. A p that immediately follows an h2 with the same parent
    Show answer

    Answer: D. A p that immediately follows an h2 with the same parent

    + is the adjacent sibling combinator: the p must be the very next element sibling of the h2. The general sibling combinator ~ is the one that matches every later p sibling.

  8. What does the general sibling selector h2 ~ p select?

    1. Only the first p after an h2
    2. Every p sibling before or after an h2
    3. Every p that is a descendant of an h2
    4. Every p sibling that comes after an h2
    Show answer

    Answer: D. Every p sibling that comes after an h2

    ~ is the general sibling combinator: it matches all later siblings of the h2 that are p elements, not just the next one. Earlier siblings and descendants are not matched.

  9. Which of these is a pseudo-element rather than a pseudo-class?

    1. :hover
    2. ::first-line
    3. :first-child
    4. :nth-child(2)
    Show answer

    Answer: B. ::first-line

    A pseudo-element styles a part of an element that is not a separate node, such as its first line, and uses the double-colon syntax. :hover, :first-child and :nth-child() are pseudo-classes: they select whole elements by state or position.

  10. Which of these properties is inherited by child elements by default?

    1. border
    2. margin
    3. padding
    4. color
    Show answer

    Answer: D. color

    Text-related properties such as color, font-family and line-height inherit by default. Box properties such as border, margin and padding do not; each element starts from their initial values.

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.