Frontend.fyi

Tutorial

CSS container queries — it was worth the long wait!

Container queries open up so much more possibilities and make your components so much more versatile.

Code on Github

Playground settings

Title

Description

Public

Editor settings


Packages

These packages can be imported in your JavaScript files.

Package name

Version

@

@

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
import "./index.css";

const App = () => {
  return (
    <div className="space-y-12 px-5 py-20">
      <div className="grid gap-2 sm:grid-cols-3">
        <Category
          name="Hiking"
          image="https://www.frontend.fyi/playground-assets/container-queries/hiking.webp"
          intro="Wind down. Enjoy nature."
        />
        <Category
          name="Trial Running"
          image="https://www.frontend.fyi/playground-assets/container-queries/trail-running.webp"
          intro="In the case walking is too slow for you."
        />
        <Category
          name="Climbing"
          image="https://www.frontend.fyi/playground-assets/container-queries/climbing.webp"
          intro="Bring yourself to new heights."
        />
      </div>
      <div className="grid gap-2 sm:grid-cols-2">
        <Category
          name="Snow sports"
          image="https://www.frontend.fyi/playground-assets/container-queries/snow-sports.webp"
          intro="Don't let the cold stop you."
        />
        <Category
          name="Mountainbiking"
          image="https://www.frontend.fyi/playground-assets/container-queries/mountainbiking.webp"
          intro="They see me rollin'..."
        />
      </div>
      <Category
        name="Most wanted"
        image="https://www.frontend.fyi/playground-assets/container-queries/most-wanted.webp"
        intro="Your favorite picks to explore your favorite places. Check out this month’s best-sellers. "
      />
    </div>
  );
};

type CategoryProps = {
  name: string;
  image: string;
  intro: string;
};

const Category = ({ name, image, intro }: CategoryProps) => {
  return (
    <div className="relative flex aspect-square overflow-clip rounded-md bg-black text-white @container/category">
      <img
        src={image}
        alt=""
        className="absolute h-full w-full object-cover @[700px]:w-[70%]"
      />
      <div className="relative z-10 flex w-full flex-col items-center justify-center p-5 @container @[350px]:items-start @[350px]:justify-end @[700px]:left-[70%] @[700px]:w-[30%] @[700px]:justify-start">
        <p className="text-2xl @[350px]:text-4xl">{name}</p>
        <p className="mt-2 hidden @[350px]/category:block @[700px]/category:mt-5">
          {intro}
        </p>
      </div>
    </div>
  );
};

export default App;
One sec — editor's thinking…

CSS Container queries have been a highly anticipated web feature that has been under discussion for almost over a decade. Zach Leatherman has put together an excellent summary, showing the various iterations and potential names that have been proposed throughout the years. Initially, there were concerns about the feasibility of implementing container queries. One of the major challenges was handling scenarios where a container query triggered a breakpoint, but the subsequent changes caused the container element to become smaller again, resulting in a reverse of the breakpoint and potentially leading to an infinite loop.

Fortunately, the CSS working group came up with a solution to address these challenges. Since the end of 2022, container queries have become available in all major browsers. In this video, I will demonstrate how you can utilize container queries and explore the vast range of possibilities they offer.

Eager to learn more?

I think this video might be a good fit for you too!