Questions
22 of 49
1What is the display property in CSS used for?
2What is the default display value for <div>, <span>, <p>, and <a> tags?
3What is the difference between display: block and display: inline?
4How does display: inline-block behave?
5What happens when you set display: none on an element?
6How is display: none different from visibility: hidden?
7Can display: none affect layout reflow or accessibility?
8What’s the effect of display: block on inline elements like <span>?
9What’s the difference between display: table and actual HTML <table> elements?
10How do replaced elements (like <img> or <input>) behave with different display values?
11How is display: flex different from display: block?
12What is the difference between display: grid and display: flex?
13What happens if you apply display: inline-flex to a container?
14What is the difference between display: contents and display: none?
15What is display: list-item, and when is it used?
16How does display: run-in work, and why is it rarely used?
17What’s the difference between display: inline-grid and display: grid?
18How can you make an element behave like both inline and block elements?
19What happens to child elements when the parent has display: contents?
20How does the display property affect box model calculations?
21How can you change a block-level element to behave like an inline element without changing its tag?
22How do you center elements using display: flex or display: grid?
23How can you hide elements but keep their layout space?
24What happens to child elements when their parent has display: none?
25How can display: contents help improve semantic markup but hurt accessibility?
26What happens if you set display: flex on the <body> tag?
27How do you use display to build responsive layouts?
28What is the difference between using display and position to arrange elements?
29How can you visually hide an element but keep it accessible for screen readers?
30How does display affect stacking and z-index behavior?
31Does display: none remove an element from the DOM?
32Can an element with display: none trigger CSS transitions or animations?
33How does display interact with CSS pseudo-elements (::before, ::after)?
34What is the difference between visibility: collapse and display: none in table elements?
35How can you make text and icons align properly using display values?
36How do block formatting contexts relate to display types?
37What happens if you apply display: table to a flex container?
38How does display behave differently for replaced vs. non-replaced elements?
39Can display be animated using CSS transitions?
40How does display affect the accessibility tree and ARIA roles?
41How can you make a <li> element appear horizontally instead of vertically?
42How do you create a two-column layout without using float or position, using only display?
43How do you make an image and text sit side by side neatly?
44How would you make a navigation menu horizontally aligned using display?
45How can you make all elements behave as border-box and block-level by default?
46How would you use display: flex to make a footer stick to the bottom?
47How can display: grid simplify responsive design compared to floats?
48When would you prefer display: inline-flex over display: flex?
49How can you use display: contents to remove extra wrappers in semantic HTML structures? How can incorrect use of display lead to accessibility or SEO problems?
22 / 49

How do you center elements using display: flex or display: grid?

Centering Elements with Flexbox and Grid in CSS

CSS Flexbox and Grid make centering elements much easier than traditional methods. Both provide properties to align items horizontally, vertically, or both.

Using Flexbox
  1. 1

    Set the parent container to display: flex.

  2. 2

    justify-content: center; centers children horizontally.

  3. 3

    align-items: center; centers children vertically along the cross axis.

  4. 4

    For full centering both horizontally and vertically, combine justify-content: center and align-items: center.

  5. 5

    Optional: flex-direction can change the main axis (row or column) for alignment.

Example: Centering with Flexbox
Using Grid
  1. 1

    Set the parent container to display: grid.

  2. 2

    place-items: center; centers children both horizontally and vertically.

  3. 3

    Alternatively, use justify-items: center; for horizontal centering and align-items: center; for vertical centering.

  4. 4

    Grid is particularly useful for centering multiple items or creating complex layouts.

Example: Centering with Grid
Best Practices
  1. 1

    Use Flexbox for single-axis layouts (row or column) and simple centering.

  2. 2

    Use Grid when you need two-dimensional control and multiple items to be aligned.

  3. 3

    For responsive designs, combine Flexbox or Grid with media queries for consistent centering on different screen sizes.

Difficulty: 3/10
Topics: flexbox alignment, grid alignment, centering patterns

Scenario Questions

0-2 years experience
  1. 1

    You have a card component with a title and button. The designer wants the button centered horizontally at the bottom of the card. How would you do that with flexbox?

  2. 2

    A teammate wrote display: flex; justify-content: center; on a container but the child isn't centered vertically. What's missing and why?

  3. 3

    If you need to center a single div both horizontally and vertically inside a hero section, what's the shortest grid syntax you'd use?

2-5 years experience
  1. 1

    Our modal component uses position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%) for centering. We want to migrate to flex/grid. What's the tradeoff, and what browser support gap should we check?

  2. 2

    In a responsive navbar, the logo is left, links center, user avatar right — but on mobile the links stack and need vertical centering. How would you structure the flex containers to handle both without duplicating markup?

  3. 3

    A bug report: a centered spinner inside a flex column container sits at the top in Safari but centers in Chrome. The container has min-height: 100vh. What's the likely cause and fix?

5-8 years experience
  1. 1

    Design a reusable Centered wrapper component that works for both inline and block children, handles dynamic heights, and doesn't force a specific display type on the consumer. What props or composition pattern would you expose?

  2. 2

    We're seeing layout thrash in a virtualized list where each row uses flex centering for icons. The rows reflow on resize. How would you measure and mitigate the performance cost without dropping flex?

  3. 3

    Our design system has three centering utilities: .center-flex, .center-grid, .center-absolute. Teams use them inconsistently. How would you audit usage, decide on a canonical approach, and migrate without breaking 200+ components?

8+ years experience
  1. 1

    We're standardizing layout primitives across web, React Native, and Flutter. The centering behavior differs subtly (e.g., flexbox vs Yoga vs Flutter's Center widget). How do you define a cross-platform 'center' contract that preserves intent but respects platform idioms?

  2. 2

    A legacy codebase uses margin: 0 auto, table-cell, and transform hacks for centering across 50k LOC. You have 6 weeks and two engineers. Outline a migration strategy that includes automated detection, visual regression gates, and rollback criteria.

  3. 3

    The design team wants 'optical centering' for icons next to text — mathematically centered looks off due to visual weight. How would you build a tokenized, themeable solution that works in CSS-in-JS, CSS modules, and vanilla CSS without runtime calculation?

Follow-up Questions

  • What breaks if the parent has no explicit height?
  • How does flex-direction: column flip which property centers horizontally?
  • Any reason to prefer grid over flex for a simple centered card?