Explore common pitfalls and solutions in CSS and layout troubleshooting with these scenario-based questions. Enhance your understanding of CSS stacking, box models, positioning, and responsive design techniques while identifying frequent mistakes.
Why might an absolutely positioned element unexpectedly cover up a nearby heading, despite the heading’s order in the HTML?
Explanation: Absolutely positioned elements can establish their own stacking context with a z-index property, causing them to sit above other elements even if those elements come later in the HTML. The HTML order alone does not control stacking when z-index and positioning are involved; option two oversimplifies this. Absolutely positioned elements do not ignore all other elements, so option three is inaccurate. Headings are visible by default, so option four is incorrect.
A parent container with several floated child divs appears collapsed, showing zero height. What is the primary cause of this issue?
Explanation: When child elements are floated, their height is removed from the normal document flow, so a parent not clearing floats will collapse in height. The color property does not influence layout in this way, so option two is incorrect. Floats do not stretch their parent’s height; their content is removed from the normal flow instead, making option three wrong. While a typo could cause styles not to apply, the most direct and common reason is missing clear, so option four is less relevant.
A box set to 200 pixels wide with 20 pixels of padding and a 10-pixel border appears wider than 200 pixels in a browser. Which CSS property fixes this to maintain a total width of 200 pixels?
Explanation: Setting 'box-sizing: border-box;' makes the width property include padding and borders, ensuring the total width stays at 200 pixels. 'mix-sizing: content-box;' is not a valid property and would not affect the calculation. 'flex-sizing: none;' is incorrect and does not exist in CSS. 'border-content: box-size;' is not a valid CSS property. Only box-sizing: border-box solves this precise issue.
When using display: flex, why might some child items not align at the top of the container as intended?
Explanation: Flexbox alignment is controlled in part by the 'align-items' property, which determines how flex items are positioned along the cross axis. If this property is set to 'center' or 'flex-end', items will not align to the top. A background color does not affect alignment, so option two is irrelevant. 'flex-direction' changes the axis but does not guarantee top alignment unless paired with correct alignment properties, and a margin typo would only affect spacing, not baseline alignment. Therefore, option one is correct.
Why might an image overflow its container on smaller screens, even when width: 100% is applied to the image?
Explanation: If the container itself has a fixed width greater than the viewport, the image will scale to 100% of the container, potentially causing overflow. Width: 100% alone does not ensure true responsiveness if the parent is not flexible, so option two is false. The image file extension, like .jpg, does not affect responsiveness, so option three is incorrect. CSS selectors using single or double colons relate to pseudo-elements and do not impact image scaling in this context, making option four unsuitable.