/    Sign up×
Bounties /Pin to ProfileBookmark

Write a detailed summary and code demo of CSS Container Queries in action

Now that container queries are supported in major browsers, please write a practical demo and explanation of how these work. Include a CodePen demo (or similar) with your reply.

I’m seeking information on why container queries will be useful, what CSS properties these supersede, if any, and am looking for an in-depth code example.

to post a answer
CSS

2 Replies

Copy linkTweet thisAlerts:
@steimeMar 06.2023 — Container queries are a new feature in CSS that allow developers to apply CSS styles based on the size of the container element, rather than just the size of the browser window. This is a major advancement in web design, as it allows for much more flexible and responsive designs that can adapt to different screen sizes and device types.

One major advantage of container queries is that they allow developers to create more reusable and modular CSS code. Instead of having to create different CSS rules for every different page or component on a site, developers can create rules that are based on the size of the container element, and these rules can be reused across different pages and components.

Another advantage of container queries is that they allow developers to create more precise and targeted styles for different elements on a page. For example, if you have a page with several different sections, each with its own container element, you can apply different styles to each section based on the size of its container element.

Container queries also allow developers to avoid using JavaScript to handle responsive design, which can be much more efficient and easier to maintain.

To use container queries in your CSS, you simply need to use the @container at-rule, followed by a media query that specifies the minimum and maximum size of the container element. Within the media query, you can then define the styles that should be applied when the container element falls within that size range.

Here you find a CodePen demo https://codepen.io/steime/pen/wvEqaXa . This demo uses the @media query selector. I wasn't able to get this working using Container Queries in CodePen. To switch to Container Queries, you simply need to @media to @container. You can use cqfill to support this in browser without support. You'll find some more info regarding this topic here: https://github.com/jsxtools/cqfill
I hope this helps.
Davinci•003 auto-replyCSS Container Queries are a powerful tool for web developers that allow them to create responsive designs that respond to the size of the container they are in. This means that developers can create designs that look great on any device, regardless of the size of the screen. To demonstrate how this works, let's look at an example.
// HTML

This is some content

// CSS
.container {
display: flex;
flex-wrap: wrap;
}

.item {
flex: 1 1 auto;
}

@container-query (min-width: 500px) {
.item {
flex: 1 1 50%;
}
}

In this example, we have a container with a single item inside. By default, the item will take up the full width of the container. However, when the container is 500px or wider, the item will take up only half of the container's width. This is an example of how CSS Container Queries can be used to create responsive designs that look great on any device.

Was this helpful?

×

Success!

Help @code-lightning spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...