CSS interview questions

19 questions from the CSS Fundamentals: Layout, Flexbox, Grid and Responsive Design course, each with the correct answer and an explanation. Try to answer before you open the answer.

Module 1: Getting Started

1. What ends a CSS declaration?

  1. A comma
  2. A period
  3. A semicolon
  4. A colon
Show answer

Answer: A semicolon

A semicolon `;` separates declarations inside the block.

Lesson: CSS Syntax

Module 2: Selectors

2. Which combinator selects only direct children?

  1. A B
  2. A ~ B
  3. A + B
  4. A > B
Show answer

Answer: A > B

`>` restricts the match to immediate children, not all descendants.

Lesson: Combinators

Module 3: The Box Model

3. Going from inside to outside, what's the correct order?

  1. content → padding → border → margin
  2. content → border → padding → margin
  3. margin → border → padding → content
  4. padding → content → margin → border
Show answer

Answer: content → padding → border → margin

Padding sits right against the content, then the border, then the margin outside everything.

Lesson: The Box Model

Module 4: Positioning & Layout

4. An `absolute` element positions itself relative to what?

  1. The viewport, always
  2. Its nearest positioned ancestor
  3. Its immediate sibling
  4. The document body only
Show answer

Answer: Its nearest positioned ancestor

It looks up the tree for the closest ancestor with position other than static; if none, it falls back to the viewport.

Lesson: The position Property

Module 5: Flexbox

5. Which property controls how much a flex item grows to fill extra space?

  1. flex-shrink
  2. flex-basis
  3. flex-grow
  4. flex-wrap
Show answer

Answer: flex-grow

flex-grow decides how leftover space is distributed among items.

Lesson: Flex Items

Module 6: Grid

6. What does the `fr` unit represent in Grid?

  1. A fixed number of pixels
  2. A percentage of the font size
  3. A fixed number of rows
  4. A fraction of the available space
Show answer

Answer: A fraction of the available space

1fr means "one share of whatever space is left after fixed-size tracks are placed."

Lesson: Grid Basics

Module 8: Responsive Design

7. A mobile-first stylesheet typically uses which media query condition?

  1. min-width
  2. max-width
  3. aspect-ratio
  4. print
Show answer

Answer: min-width

min-width queries add styles as the screen gets wider, matching the mobile-first base-then-enhance pattern.

Lesson: Media Queries & Mobile-First

Module 10: The Cascade

8. Between a class selector and an id selector on the same element, which wins?

  1. Whichever is written last
  2. The id selector
  3. The class selector
  4. The first one declared
Show answer

Answer: The id selector

id selectors carry higher specificity than class selectors, regardless of source order.

Lesson: Specificity

9. How do you read the value of a CSS custom property named --gap?

  1. gap(--gap)
  2. $gap
  3. var(--gap)
  4. get(--gap)
Show answer

Answer: var(--gap)

The var() function reads a custom property's current value.

Lesson: CSS Custom Properties (Variables)

Module 11: Architecture & Tooling

10. Which selector picks a parent based on its children?

  1. :has()
  2. :is()
  3. :where()
  4. :root
Show answer

Answer: :has()

:has() is the relational "parent" selector.

Lesson: Modern Selectors & Native Nesting

Module 12: Common Patterns

11. Which is the shortest way to center children in a grid container?

  1. margin: auto auto
  2. text-align: center
  3. float: center
  4. place-items: center
Show answer

Answer: place-items: center

place-items: center centers grid children on both axes in one declaration.

Lesson: Centering Techniques

12. Which box-model order is correct from inside out?

  1. content, border, padding, margin
  2. margin, border, padding, content
  3. content, padding, border, margin
  4. padding, content, border, margin
Show answer

Answer: content, padding, border, margin

Content sits inside padding, then border, then margin.

Lesson: Sticky Footer & Card Layouts

13. What does box-sizing: border-box change?

  1. Width excludes content
  2. Margins collapse
  3. Borders disappear
  4. Width includes padding and border
Show answer

Answer: Width includes padding and border

It makes width math predictable.

Lesson: Sticky Footer & Card Layouts

14. An absolute element positions itself relative to...

  1. Its nearest positioned ancestor
  2. The viewport always
  3. Its sibling
  4. The body only
Show answer

Answer: Its nearest positioned ancestor

Without one it falls back to the initial containing block.

Lesson: Sticky Footer & Card Layouts

15. Which is best for a two-dimensional layout?

  1. float
  2. CSS Grid
  3. inline
  4. table-layout hacks
Show answer

Answer: CSS Grid

Grid handles rows and columns together.

Lesson: Sticky Footer & Card Layouts

16. Which unit scales with the user's default font size?

  1. px
  2. pt
  3. rem
  4. cm
Show answer

Answer: rem

rem is relative to the root font size.

Lesson: Sticky Footer & Card Layouts

17. Which wins: a class selector or an id selector?

  1. The class selector
  2. Whichever is longer
  3. Neither
  4. The id selector
Show answer

Answer: The id selector

Ids have higher specificity.

Lesson: Sticky Footer & Card Layouts

18. Which properties are best to animate for performance?

  1. transform and opacity
  2. width and height
  3. margin and padding
  4. top and left
Show answer

Answer: transform and opacity

They avoid layout and run on the GPU.

Lesson: Sticky Footer & Card Layouts

19. How do you read a custom property --gap?

  1. $gap
  2. var(--gap)
  3. gap()
  4. @gap
Show answer

Answer: var(--gap)

var() reads custom properties.

Lesson: Sticky Footer & Card Layouts

← Back to the course