"}},{"@type":"Question","name":"What is Margin Collapsing in CSS, when does it occur, and how do you prevent it?","acceptedAnswer":{"@type":"Answer","text":"Margin Collapsing is a layout behavior where adjacent vertical margins (top and bottom) of block-level elements combine into a single consolidated margin instead of adding together.\n\nHow Collapsed Size is Calculated:\n- If both margins are positive: The size equals the LARGEST single margin (e.g., margin-bottom: 30px followed by margin-top: 20px results in a 30px gap, NOT 50px).\n- If one is negative: The negative margin is subtracted from the largest positive margin.\n- If both are negative: The size equals the most negative margin.\n\nThree Scenarios Where Margin Collapsing Occurs:\n1. Adjacent Siblings: Vertical margins between sibling blocks collapse.\n2. Parent and First/Last Child: If there is no border, padding, or inline content separating parent and child, the child's margin-top collapses through the parent, pushing the parent downward!\n3. Empty Blocks: An element with no height, padding, or border will collapse its own top and bottom margins together.\n\nHow to Prevent Margin Collapsing:\n• Create a Block Formatting Context (BFC) on parent: overflow: hidden or display: flow-root.\n• Add padding (padding-top: 1px) or border (border-top: 1px solid transparent).\n• Use Flexbox or Grid layout (Flex and Grid children NEVER collapse margins!).\nCode Example:\n\n
Paragraph 1
\n
Paragraph 2
\n\n\n\n/* Buggy Parent: Child margin-top pushes parent down */\n.buggy-parent {\n margin-top: 0;\n}\n.buggy-parent > h1 {\n margin-top: 40px; /* Leaks out of parent */\n}\n\n/* Fix A: Use display: flow-root (Establishes clean BFC) */\n.fixed-parent-bfc {\n display: flow-root;\n}\n\n/* Fix B: Use CSS Grid or Flexbox (Zero margin collapse) */\n.fixed-parent-flex {\n display: flex;\n flex-direction: column;\n}"}},{"@type":"Question","name":"What is a Block Formatting Context (BFC) and what common layout problems does it solve?","acceptedAnswer":{"@type":"Answer","text":"A Block Formatting Context (BFC) is an isolated visual rendering region of a web page in which block boxes are laid out. Elements inside a BFC are completely isolated from elements outside it.\n\nLayout Problems Solved by BFC:\n1. Containing Floats (Clearfix Alternative):\n - When child elements are floated, the parent container collapses to 0px height. Creating a BFC on the parent forces it to enclose all floated children completely.\n2. Preventing Margin Collapsing:\n - Margins between elements in different BFCs never collapse.\n3. Preventing Content from Wrapping Under Floated Sidebars:\n - In two-column layouts with a floated sidebar, creating a BFC on the main content column causes it to form an independent rectangular box adjacent to the float, rather than text wrapping underneath it.\n\nWays to Create a BFC:\n• display: flow-root (Modern, cleanest standard method designed specifically for BFC creation with zero side effects).\n• overflow: hidden, auto, or scroll (excluding visible).\n• display: flex, grid, inline-block.\n• position: absolute or fixed.\n• float: left or right.\nCode Example:\n/* 1. Traditional Floated Layout Problem */\n.clearfix-parent {\n /* MODERN STANDARD: Creates BFC, automatically wraps floats with no hacks */\n display: flow-root;\n background: #f1f5f9;\n}\n\n.floated-img {\n float: left;\n width: 150px;\n margin-right: 16px;\n}\n\n/* 2. Preventing text wrap around float */\n.main-text-column {\n /* Establishes BFC: Forms solid column beside float instead of wrapping under */\n display: flow-root;\n}"}},{"@type":"Question","name":"What is the difference between CSS Transitions and CSS Animations (@keyframes)?","acceptedAnswer":{"@type":"Answer","text":"Both CSS Transitions and Animations animate changes in CSS properties over time, but their execution mechanisms and control capabilities differ:\n\n1. CSS Transitions:\n - Trigger: Requires an explicit state change (e.g., user interaction like :hover, :focus, or toggling a class with JavaScript).\n - Trajectory: Animates strictly between TWO states (Initial state -> Final state). Cannot define intermediate checkpoints without JS.\n - Looping: Does NOT loop continuously (executes once per trigger).\n - Use Cases: Button hover states, dropdown accordion open/close, toggle switches, modal fade-ins.\n\n2. CSS Animations (@keyframes):\n - Trigger: Does NOT require user interaction or state change; can start automatically on page load.\n - Trajectory: Supports complex multi-step sequences via @keyframes percentages (0%, 25%, 50%, 100%).\n - Looping & Direction: Supports animation-iteration-count: infinite, animation-direction: alternate, animation-play-state: paused.\n - Use Cases: Infinite loading spinners, pulsating notification badges, complex multi-stage illustrations.\nCode Example:\n/* 1. CSS Transition (Triggered by :hover state) */\n.btn-interactive {\n background-color: #2563eb;\n color: white;\n transition: background-color 0.25s ease, transform 0.15s ease;\n}\n\n.btn-interactive:hover {\n background-color: #1d4ed8;\n transform: translateY(-2px);\n}\n\n/* 2. CSS Keyframe Animation (Automatic Infinite Spinner) */\n@keyframes spin {\n 0% { transform: rotate(0deg); }\n 100% { transform: rotate(360deg); }\n}\n\n.loading-spinner {\n width: 40px;\n height: 40px;\n border: 4px solid #e2e8f0;\n border-top-color: #3b82f6;\n border-radius: 50%;\n animation: spin 0.8s linear infinite;\n}"}},{"@type":"Question","name":"Explain the CSS overflow property values: visible, hidden, scroll, auto, and clip.","acceptedAnswer":{"@type":"Answer","text":"The overflow property specifies how the browser handles content that exceeds the physical boundaries (width/height) of an element's box:\n\n1. overflow: visible (Default):\n - Content is not clipped; it bleeds and renders outside the element box into surrounding space.\n - Does not establish a scroll container or BFC.\n\n2. overflow: hidden:\n - Content extending beyond the box is clipped and invisible.\n - No scrollbars are rendered.\n - Establishes a Block Formatting Context (BFC) and scroll container (content can still be scrolled programmatically via JavaScript el.scrollTop).\n\n3. overflow: scroll:\n - Content is clipped. The browser ALWAYS renders scrollbars (both horizontal and vertical), even if the content fits inside the box and doesn't need scrolling!\n\n4. overflow: auto (Recommended):\n - Content is clipped. The browser dynamically displays scrollbars ONLY when the content actually exceeds the container dimensions.\n\n5. overflow: clip (Modern):\n - Clips content strictly at the overflow-clip-margin boundary.\n - Unlike overflow: hidden, overflow: clip completely forbids all scrolling (including programmatic JS scrolling), reducing memory overhead.\nCode Example:\n/* Standard Scrollable Container */\n.scrollable-feed {\n max-height: 400px;\n overflow-y: auto; /* Vertical scrollbar only when needed */\n overflow-x: hidden; /* Prevent horizontal overflow */\n overscroll-behavior: contain; /* Prevents scroll chaining to body */\n}\n\n/* Modern Clipping with Margin Offset */\n.clipped-banner {\n overflow: clip;\n overflow-clip-margin: 10px; /* Allows 10px bleed before hard clip */\n}"}},{"@type":"Question","name":"What are CSS Combinators: Descendant (' '), Child (>), Adjacent Sibling (+), and General Sibling (~)?","acceptedAnswer":{"@type":"Answer","text":"Combinators explain the relationship between two selectors to target specific elements in the DOM hierarchy:\n\n1. Descendant Selector (Space ' '):\n - div p: Matches all
elements that are nested inside a
, regardless of nesting depth (children, grandchildren, great-grandchildren).\n\n2. Child Combinator (>):\n - div > p: Matches only direct children. If the
is nested inside an intermediate inside the
, it is NOT matched.\n\n3. Adjacent Sibling Combinator (+):\n - h2 + p: Matches the
element that immediately follows an
at the exact same tree level. Only targets a single adjacent element.\n\n4. General Sibling Combinator (~):\n - h2 ~ p: Matches ALL
elements that follow an
as siblings at the same tree level, regardless of whether they are immediately adjacent.\nCode Example:\n\n\n
Heading
\n
First paragraph (Direct sibling)
\n
\n
Nested paragraph
\n
\n
Second paragraph
\n\n\n"}},{"@type":"Question","name":"What is the difference between CSS Reset and Normalize.css?","acceptedAnswer":{"@type":"Answer","text":"Browsers ship with their own default User Agent stylesheets (e.g. Chrome's user-agent.css), which introduce default margins, font sizes, heading styles, and form formatting that differ slightly between WebKit, Blink, and Gecko.\n\n1. CSS Reset (e.g. Eric Meyer's Reset):\n - Philosophy: Wipe the slate completely clean.\n - Sets margin: 0, padding: 0, border: 0, font-size: 100% across all elements.\n - Result: Removes all default headings, lists lose their bullets, and tables lose their styling. The developer must re-style every single element from scratch.\n - Drawback: Can strip away useful accessible defaults.\n\n2. Normalize.css (Nicolas Gallagher):\n - Philosophy: Preserve useful browser defaults while fixing cross-browser inconsistencies and bugs.\n - Normalizes line heights, preserves list bullets, fixes font inheritance in form controls, and fixes mobile orientation bugs.\n\n3. Modern Opinionated Resets (Andy Bell, Josh Comeau):\n - Sets box-sizing: border-box, removes body default margins, sets images to display: block; max-width: 100%, and improves text wrapping with text-wrap: balance/pretty.\nCode Example:\n/* Modern Industry Standard CSS Reset */\n*, *::before, *::after {\n box-sizing: border-box;\n margin: 0;\n padding: 0;\n}\n\nhtml {\n -webkit-text-size-adjust: 100%;\n tab-size: 4;\n}\n\nbody {\n line-height: 1.5;\n -webkit-font-smoothing: antialiased;\n}\n\nimg, picture, video, canvas, svg {\n display: block;\n max-width: 100%;\n}\n\ninput, button, textarea, select {\n font: inherit;\n}"}},{"@type":"Question","name":"What is the CSS aspect-ratio property and how does it prevent Cumulative Layout Shift (CLS)?","acceptedAnswer":{"@type":"Answer","text":"The aspect-ratio CSS property allows developers to set a preferred width-to-height ratio for an element's box (e.g. aspect-ratio: 16 / 9, 1 / 1, 4 / 3).\n\nHistorical Problem: The \"Padding-Top Percentage Hack\":\nBefore native aspect-ratio, maintaining responsive video or image containers required a fragile hack:\n.video-box { position: relative; padding-top: 56.25%; /* 9 / 16 = 56.25% */ }\n.video-box iframe { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }\n\nHow aspect-ratio Prevents Cumulative Layout Shift (CLS):\nWhen responsive images load asynchronously, the browser knows the container width (e.g. 100% of phone screen = 390px), but doesn't know the height until the image file header arrives. This causes the text below the image to jump down violently, triggering poor CLS scores.\nBy declaring:\nimg { width: 100%; aspect-ratio: 16 / 9; }\nThe browser immediately calculates the height (390 * 9 / 16 = 219.3px) and reserves layout space BEFORE the image bytes download, achieving a perfect CLS score of 0.00!\nCode Example:\n/* Responsive 16:9 Video Container without padding hacks */\n.responsive-video {\n width: 100%;\n aspect-ratio: 16 / 9;\n border-radius: 8px;\n overflow: hidden;\n}\n\n/* Perfect Square Profile Avatar */\n.avatar-square {\n width: 80px;\n aspect-ratio: 1 / 1;\n border-radius: 50%;\n object-fit: cover;\n}"}},{"@type":"Question","name":"Explain the object-fit and object-position properties for responsive images and videos.","acceptedAnswer":{"@type":"Answer","text":"The object-fit property specifies how the contents of a replaced element (such as an or