Question 1
Given a parent and child div, which CSS property combination correctly centers an absolutely positioned child div both vertically and horizontally inside its parent?
- position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
- float: center; margin: auto; padding: 50%;
- display: flex; align-content: center; justify-items: center;
- vertical-align: middle; text-align: center;
- display: block; margin-left: 50%; margin-top: 50%;
Question 2
When using Flexbox to center a child element in a parent container, which set of CSS properties should you apply to the parent?
- display: flex; align-items: center; justify-content: center;
- display: flexbox; align-content: center; justify-items: center;
- display: block; text-align: center; vertical-align: middle;
- display: flex; flex-direction: row; float: center;
- display: flex; align-content: flex-end; justify-content: flex-end;
Question 3
Which one of the following is a correct and modern way to use CSS Grid to perfectly center an element inside its parent?
- display: grid; place-items: center;
- display: block; text-align: center; margin-top: 50%;
- display: grid; align-items: flex-start; justify-items: flex-end;
- display: inline-grid; align-content: middle;
- display: grid; margin: auto 0; padding: center;
Question 4
What is the primary advantage of using the 'transform: translate(-50%, -50%)' method for centering over Flexbox or Grid?
- Compatibility with older browsers that do not support Flexbox or Grid.
- Requires less code than Flexbox or Grid.
- Allows for automatic text alignment within the element.
- Prevents any scrolling issues in overflow situations.
- Automatically resizes the parent container to fit the child.
Question 5
Consider the following styles: '.parent { position: relative; } .child { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }'. What is the effect of omitting 'transform: translate(-50%, -50%)'?
- The child will be positioned with its top-left corner at the center of the parent, not truly centered.
- The child will be hidden and not visible.
- The child will stretch to fit the parent entirely.
- The child will only be horizontally centered, not vertically.
- The child will remain in its default static position.
Question 6
When centering with Flexbox, which property ensures children are centered vertically within a horizontal flex container?
- align-items: center;
- justify-items: center;
- flex-direction: column;
- text-align: center;
- align-content: stretch;
Question 7
Which of the following is NOT a correct property or value for centering with CSS Grid?
- align-items: center;
- justify-items: center;
- place-items: center;
- vertical-align: center;
- display: grid;
Question 8
You inherit a code base using only 'margin: 0 auto' to try to center a div. Why will this NOT center the element vertically?
- 'margin: 0 auto' only centers the element horizontally if it has a set width.
- 'margin: 0 auto' breaks vertical alignment rules in modern browsers.
- Vertical margins can only be set using Flexbox or Grid.
- Browsers ignore any margin values on block elements.
- 'margin: 0 auto' forces the display to inline-block, preventing vertical centering.
Question 9
A developer writes '.parent { display: flex; flex-direction: column; align-items: center; justify-content: center; }' and '.child { margin-top: auto; margin-bottom: auto; }'. What is the effect?
- The child will be perfectly vertically and horizontally centered within the parent.
- Only vertical centering will apply due to the column direction.
- The child will not be centered because margin auto conflicts with flex centering.
- The child will be biased towards the bottom of the parent.
- The child will be removed from normal flow due to incorrect margins.
Question 10
Why might a candidate mention browser support when discussing CSS Grid for centering elements during an interview?
- Because some older browsers may not support CSS Grid, and it's important to demonstrate awareness of compatibility.
- Because CSS Grid is deprecated and shouldn't be used in modern projects.
- Because Grid layouts increase page load times on all browsers.
- Because Flexbox cannot be used with responsive designs.
- Because CSS Grid automatically overrides any Flexbox settings.