Container Queries & Modern Responsive

    Lesson 23 โ€ข Advanced Track

    What You'll Learn

    How container queries differ from media queries
    Setting up containment with container-type
    Writing @container rules to style by parent size
    Container query units: cqw, cqh, cqi, cqb
    Building truly reusable responsive components
    Combining container queries with CSS Grid and Flexbox

    ๐Ÿ’ก Think of It Like This

    Media queries ask: "How big is the screen?" Container queries ask: "How big is my parent?"This is a game-changer for reusable components. A card component can adapt its layout based on whether it's in a wide main content area or a narrow sidebar โ€” without knowing anything about the viewport.

    FeatureMedia QueryContainer Query
    MeasuresViewport (screen) sizeParent container size
    Reusabilityโš ๏ธ Tied to page layoutโœ… Works anywhere
    Syntax@media (min-width:)@container (min-width:)
    SetupNone neededcontainer-type: inline-size

    Container Queries in Action

    The same card component adapts its layout based on its container's width โ€” not the viewport.

    Container Query Card

    Same card in different-width containers

    Try it Yourself ยป
    Code Preview
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Container Queries</title>
      <style>
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body { background: #0f172a; font-family: system-ui, sans-serif; color: #e5e7eb; padding: 30px; }
    
        h1 { color: #3b82f6; margin-bottom: 10px; }
        h2 { color: #22c55e; margin: 20px 0 10px; font-size: 1rem; }
    
        /* Set up containment on the wrapper */
    
    ...

    Container Query Units

    Container query units let you size things relative to the container, not the viewport.

    Container Query Units

    Size elements relative to their container

    Try it Yourself ยป
    Code Preview
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Container Query Units</title>
      <style>
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body { background: #0f172a; font-family: system-ui, sans-serif; color: #e5e7eb; padding: 30px; }
    
        h1 { color: #3b82f6; margin-bottom: 20px; }
    
        .demo-container {
          container-type: inline-size;
          background: #1e293b;
          padding: 20px;
          border-radius: 8px;
          margin: 15px 0;
          resize: horizontal;
    ...

    โš ๏ธ Common Mistakes

    • Forgetting container-type โ€” Container queries only work if the parent has container-type: inline-size (or size). Without it, @container rules are ignored.
    • Using container queries on the element itself โ€” A container can't query its own size. The @container query checks the nearest ancestor with containment.
    • Replacing all media queries โ€” Media queries are still useful for page-level layout (overall column count). Container queries are for component-level adaptation.
    • Performance with size containment โ€” container-type: size (both axes) requires the element to have explicit dimensions. Use inline-size for width-only containment.

    ๐ŸŽ‰ Lesson Complete

    You now understand container queries โ€” the future of responsive design:

    • โœ… Container queries style components based on their parent's size, not the viewport
    • โœ… Set up with container-type: inline-size on the parent
    • โœ… Use @container (min-width: 400px) to write responsive component styles
    • โœ… Container units (cqw, cqi) size things relative to the container

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service