CSS layout: box model, Flexbox, Grid and responsive design – Front End interview prep
2,594 interview-style multiple-choice questions on CSS layout: Flexbox, Grid and responsive design, organized by topic: concepts, scenarios, code to read and tradeoffs, each with a short explanation. Learn the 103 key terms with the flashcards first, then test yourself.
Take the practice test · Flashcards · All study tools
What it covers
- 2.1 Box model and units
- 2.2 Cascade, specificity and inheritance
- 2.3 Positioning, stacking and overflow
- 2.4 Flexbox
- 2.5 CSS Grid
- 2.6 Responsive design and media queries
- 2.7 Modern CSS: custom properties, :has(), layers, nesting
- 2.8 Typography, color, transforms and animation
- 2.9 CSS architecture, performance and tooling
Key terms
2.1 Box model and units
- aspect-ratio
- A preferred width-to-height ratio used when at least one of those sizes is automatic
- Block formatting context
- An independent layout region where floats are contained and margins don't collapse across
- Box model
- Content, padding, border and margin around every element
- box-sizing
- Whether `width` and `height` include padding and border, or only the content box
- ch
- A length equal to the advance width of the "0" glyph in the current font
- CSS pixel
- The CSS reference pixel (`px`), an angle-based unit that may cover several device pixels
- Dynamic viewport units
- `dvh`/`dvw`, which track the visible viewport as mobile toolbars move
- fit-content
- A sizing keyword that shrink-wraps content up to the available space
- Logical properties
- Properties like `margin-inline-start` that follow writing direction
- Margin collapsing
- Adjacent vertical margins in block flow merge into the larger one
- object-fit
- How replaced content like images fills its box: cover, contain, fill
- rem
- A length equal to the font size of the root `html` element
- Specificity
- Weight of a selector (IDs, classes/attributes/pseudo-classes, elements) that decides which rule wins
- Stacking context
- A group of elements layered together; z-index only compares within it
2.2 Cascade, specificity and inheritance
- !important
- A flag that raises a declaration's importance so origin is compared before specificity
- :is()
- A pseudo-class that groups selectors and takes the specificity of its most specific argument
- :not()
- A pseudo-class that matches elements that do not match its selector list
- :where()
- Pseudo-class that matches like :is() but adds zero specificity
- Cascade
- Algorithm that chooses a winning value by origin, importance, layer, specificity and order
- Inheritance
- Some properties, mostly text-related, pass their computed values to children
- initial
- A value that uses a property's initial value from the CSS specification, not the browser default
- revert
- A value that rolls a property back to the previous origin, usually the user-agent style
- unset
- A value that acts like `inherit` on inherited properties and like `initial` otherwise
2.3 Positioning, stacking and overflow
- Containing block
- The rectangle that positions and percentage sizes resolve against
- inset
- Shorthand for top, right, bottom and left offsets on a positioned element
- isolation
- A property that, when set to isolate, creates a stacking context without changing position or z-index
- overflow: clip
- Crops overflowing paint without creating a scroll container
- overscroll-behavior
- Controls scroll chaining and end-of-scroll effects such as bounce or pull-to-refresh
- position: sticky
- A hybrid of relative and fixed: the box stays in flow, then clings to a scrollport edge within its parent
- Scroll snap
- CSS that makes a scroll container settle on defined points
- scrollbar-gutter
- Reserves space for a classic scrollbar so content does not shift when the bar appears
- Top layer
- A browser layer above the page for dialogs, popovers and fullscreen
2.4 Flexbox
- align-self
- Per-item override of the container's align-items
- Auto margin
- A margin set to auto, which absorbs free space in flex and grid
- Cross axis
- The axis perpendicular to the main axis
- Flex container
- Element with `display: flex` whose children become flex items
- flex shorthand
- Short for flex-grow, flex-shrink and flex-basis
- flex-basis
- The starting size of a flex item before free space is shared out
- flex-direction
- Property that sets the main-axis direction of a flex container
- flex-grow
- How much of the free space an item takes, relative to siblings
- flex-shrink
- How much an item shrinks when space is short
- flex-wrap
- Whether flex items stay on one line or wrap onto additional lines
- gap
- Space between flex or grid items, never at the outer edges
- justify-content
- Alignment of flex items along the main axis
- Main axis
- The direction flex items are laid out in, set by flex-direction
- order
- Visual reordering of flex items without changing source order
2.5 CSS Grid
- auto-fill
- Creates as many tracks as will fit in the container and keeps empty ones
- auto-fit
- Fits as many tracks as possible and collapses empty ones so items stretch
- Explicit grid
- Tracks defined by grid-template-rows/columns
- fr unit
- A fraction of the free space in a grid container
- Grid line
- The numbered or named dividing lines between tracks
- grid-area
- Shorthand for an item's four line placements, or a named area
- grid-auto-flow
- Controls the direction of auto-placement and whether packing is dense
- grid-auto-rows
- Size of rows created automatically outside the explicit grid
- grid-template-areas
- Named regions written as quoted strings, which items occupy with grid-area
- Implicit grid
- Rows or columns created automatically when items fall outside the declared grid
- minmax()
- Sets a grid track's minimum and maximum size
- place-items
- Shorthand for align-items and justify-items on a grid (or flex) container
- Subgrid
- A nested grid that uses its parent's tracks
2.6 Responsive design and media queries
- Art direction
- Serving different image crops at different sizes with <picture>
- Breakpoint
- A width where the layout changes, set with a media or container query
- clamp()
- min, preferred and max in one value, for fluid sizes
- Container query
- Styles based on a container's size instead of the viewport
- Mobile-first
- Writing base CSS for small screens and adding min-width media queries for larger ones
- Safe area
- Screen area not covered by notches or rounded corners
- Viewport meta tag
- Tells mobile browsers to lay out at the device width
- Visual viewport
- The currently visible portion of the layout viewport, which shrinks with pinch-zoom or a virtual keyboard
- prefers-color-scheme
- Media feature for a user's light or dark color-scheme preference
- container-type
- Makes an element a query container on its inline axis, both axes, or neither
- matchMedia
- JavaScript API that evaluates a media query and notifies when the result changes
- Fluid typography
- Type that scales smoothly between a minimum and a maximum, often with clamp() and a vw or container unit
- image-set()
- CSS function that picks a background (or other) image by resolution or format
2.7 Modern CSS: custom properties, :has(), layers, nesting
- :has()
- Relational pseudo-class that matches by descendants or following siblings
- Cascade layers
- @layer groups whose order beats specificity
- Custom property
- A `--name` property whose value can be reused with var()
- OKLCH
- Perceptually uniform color space with lightness, chroma and hue
- @property
- At-rule that registers a typed custom property with a syntax, inheritance flag and initial value
- @scope
- At-rule that limits selectors to a scope root, optionally stopping at an inner 'donut' boundary
- light-dark()
- CSS function that picks one of two values based on the used color scheme
- @supports
- Feature query that applies rules only if the engine understands a given syntax
- View transitions
- API and CSS that animate between two DOM or page states using named snapshots
- Anchor positioning
- CSS that positions a floating box relative to an anchor element, with overflow fallbacks
2.8 Typography, color, transforms and animation
- Compositor-friendly properties
- transform and opacity, which can animate without layout or paint
- font-display
- @font-face descriptor that controls text rendering while fonts load
- Reduced motion
- User preference to minimize animation, detected with prefers-reduced-motion
- Variable font
- A font file with continuous axes such as weight and width
- @font-face
- A rule that defines a custom font family, its source files, and descriptors such as display and unicode-range
- font-variation-settings
- Low-level control of variable-font axes; axes you omit may reset to their defaults
- will-change
- A hint that an element will change, which may promote a compositor layer
- @keyframes
- A rule that names the poses of a CSS animation over its timeline
- mix-blend-mode
- How an element's colors blend with what has already been painted behind it
- animation-timeline
- The timeline that drives an animation, such as document time, scroll(), or view()
2.9 CSS architecture, performance and tooling
- BEM
- Block__Element--Modifier naming convention
- Critical CSS
- Minimal CSS inlined to render the first viewport fast
- CSS Modules
- Build-time scoping of class names to a component
- Design tokens
- Named, platform-agnostic values for design decisions
- Utility-first CSS
- Composing UI from single-purpose classes
- ITCSS
- Inverted Triangle CSS: order rules from generic, low-specificity styles to explicit, high-impact ones
- OOCSS
- Object-Oriented CSS: split structure (layout) from skin (visual theme) so each can be reused
- PostCSS
- A plugin pipeline that transforms a CSS AST at build time
- Stylelint
- A linter for CSS and preprocessor syntax and conventions
- Autoprefixer
- A PostCSS plugin that adds vendor prefixes your Browserslist targets still need
- CSS containment
- Hints that limit how much of the page a component's layout, paint, or size can affect
Sample practice questions
With `box-sizing: border-box`, an element has `width: 200px; padding: 20px; border: 5px solid`. How wide is its border box?
- 200px
- 210px
- 240px
- 250px
Answer: A. 200px Border-box makes `width` include padding and border, so the content box shrinks to 150px and the total stays 200px.
Which selector has the highest specificity?
- `#nav .item a`
- `.nav .item a:hover`
- `nav ul li a`
- `.nav > .item > a`
Answer: A. `#nav .item a` One ID (1,1,1) beats any number of classes and pseudo-classes; `.nav .item a:hover` is only (0,3,1).
An element has `position: absolute`. What is it positioned relative to?
- Its previous sibling in the normal flow
- Its nearest ancestor with a non-`static` position
- Always the viewport, whatever its ancestors are
- Its direct parent, whatever that parent's position
Answer: B. Its nearest ancestor with a non-`static` position Absolute positioning uses the nearest positioned ancestor, which is why `position: relative` on a wrapper is common.
In a flex container with `flex-direction: row`, which property aligns items along the main (horizontal) axis?
- `justify-content`
- `align-content`
- `place-self`
- `align-items`
Answer: A. `justify-content` `justify-content` works on the main axis and `align-items` on the cross axis; switching to `column` swaps the axes.
What does `grid-template-columns: repeat(3, 1fr);` create?
- Three columns that share the free space equally
- Three rows that share the height equally
- One 1px column repeated three times per row
- A single column that is 3fr wide
Answer: A. Three columns that share the free space equally `fr` is a fraction of the free space, so three `1fr` tracks are equal.
What does `<meta name="viewport" content="width=device-width, initial-scale=1">` do?
- It switches the page into dark mode when it's viewed on phones
- It disables pinch-to-zoom on all phones and tablets for the page
- It makes every image on the page scale responsively to the screen
- Mobile browsers lay out at device width, not a zoomed-out width
Answer: D. Mobile browsers lay out at device width, not a zoomed-out width Without it, mobile browsers lay out at about 980px and scale down, so media queries don't behave as expected.
What is a container query (`@container`) for?
- Loading a stylesheet only when a container is visible
- Setting the maximum width of the whole page
- Styling a component by its container's size, not the viewport's
- Querying the DOM for the elements inside a container
Answer: C. Styling a component by its container's size, not the viewport's A card can switch layout when its column is narrow, wherever it is placed.
What does `font-size: clamp(1rem, 2.5vw, 2rem);` do?
- It uses 1rem on phones and 2rem on desktops, nothing between
- It picks one of the three values at random on load
- It scales with the viewport but stays between 1rem and 2rem
- It always uses 2.5vw and ignores the other values
Answer: C. It scales with the viewport but stays between 1rem and 2rem `clamp(min, preferred, max)` gives fluid sizing with bounds.
What does the BEM naming convention stand for?
- Block, Element, Modifier
- Bundle, Export, Module
- Base, Extend, Mixin
- Box, Edge, Margin
Answer: A. Block, Element, Modifier `.card__title--large`: block `card`, element `title`, modifier `large`.
Two stacked paragraphs have `margin-bottom: 20px` and `margin-top: 30px`. What is the vertical gap between them in normal block flow?
- 25px, because the browser averages the two margins
- 30px, because the margins collapse to the larger one
- 20px, because the first element's margin always wins
- 50px, because the two margins are added together
Answer: B. 30px, because the margins collapse to the larger one Vertical margins of adjacent blocks collapse. Margins don't collapse inside flex or grid containers.
More Front End
- HTML semantics & accessibility
- JavaScript fundamentals
- Async JavaScript & the event loop
- TypeScript
- React: components, hooks & rendering
- State management: Redux, Zustand, context
- Angular and Vue basics
- Browser internals & rendering
- Web performance
- Build tools & bundlers
- Front-end testing
- Front-end security
- Mobile & cross-platform