• About Us
  • Privacy Policy
  • Disclaimer
  • Contact Us
AimactGrow
  • Home
  • Technology
  • AI
  • SEO
  • Coding
  • Gaming
  • Cybersecurity
  • Digital marketing
No Result
View All Result
  • Home
  • Technology
  • AI
  • SEO
  • Coding
  • Gaming
  • Cybersecurity
  • Digital marketing
No Result
View All Result
AimactGrow
No Result
View All Result

Type Queries as Conditional Modifiers

Admin by Admin
November 25, 2026
Home Coding
Share on FacebookShare on Twitter


This text is customized from a lesson in Writing Resilient CSS, the latest module in Kevin’s course, CSS Demystified. The module covers configuration choices with customized properties, math capabilities, relative colours, :has(), type queries, and extra, all put to work in sensible, reusable patterns. The course is 32% off till December 2, 2026.

Modifiers have been with us for a very long time, typically with modifier lessons, but in addition with attribute selectors. Some widespread examples are issues like totally different variations of alerts and buttons.

/* BEM strategy */
.button--accent {
  background-color: var(--clr-accent);
}

/* CUBECSS strategy */
.toast[data-type="error"] {
  border-color: var(--clr-warning);
}

For lots of use instances, both of these approaches work nice, however in terms of format, plenty of the time we would like the context of the place the aspect is getting used to regulate issues.

A card is a pleasant instance of this, the place we’d have a number of variations.

.card {
  show: grid;
  hole: .75rem;
}

.card--horizontal {
  grid-template-columns: 1fr 2fr;
}

.card--compact {
  padding: .75rem;
  hole: .5rem;
}

The difficulty right here is, if I’m utilizing .card--compact, I in all probability wish to do this for all of the playing cards inside a piece. That may be annoying to use to every card, however that’s rendered moot since most individuals are doing a little type of looping in a template like this lately.

{relatedPosts.map((submit) => (

{submit.title}

{submit.excerpt}

))}

And this works nice, so long as we at all times need the playing cards to be compact.

Nonetheless, you would possibly put this collectively and ship it off to the design workforce for approval, just for them to not like that when the compact playing cards are decrease down the web page, the smaller padding isn’t wanted to create visible separation between the 2 sections. As an alternative of serving to with the hierarchy, it’s merely creating a wierd visible inconsistency.

We may use one thing like a resize observer to attempt to take care of this, however we’ve a greater instrument at our disposal: Container Type Queries, which at the moment are Baseline Newly Obtainable.

Whereas all of them begin with container, for the remainder of this text I interchangeable use container question or dimension question for a container dimension question, and type question for what we’re speaking about right here.

A fast have a look at type queries

Earlier than we get into the great things, a fast have a look at the fundamentals of fashion queries.

You’re in all probability accustomed to dimension queries, which work much like @media however have a look at the scale of a container, reasonably than the viewport. Container type queries use the identical @container at-rule as container dimension queries, however as a substitute of a dimension, they have a look at the worth of a customized property.

@container type(--theme: high-contrast) {
  .card {
    background: white;
    shade: black;
    border-color: black;
  }
}

This reads as, “if the closest container has --theme: high-contrast, type the .card like this.”

A few essential notes about them:

  • You don’t must arrange a container. In contrast to dimension queries, each aspect is a method container by default, so there’s no container-type to declare.
  • They solely work with customized properties for now. Querying common properties, like type(show: flex), is within the spec however isn’t supported anyplace but.

Components can’t question themselves

Similar to with dimension queries, type queries have a look at an ancestor, not the aspect itself. Meaning this gained’t work:

.card {
  --card-layout: various;
}

@container type(--card-layout: various) {
  .card {
    grid-template-columns: 1fr 2fr; /* would not work */
  }
}

The customized property must be set on one thing above the cardboard. Generally this may be annoying, and it at all times tends to catch folks out, however due to how I take advantage of them, I don’t discover this to be a lot of a limitation.

if() can also be on the best way, and permits for type queries as nicely, however if() is ready to see the worth of customized properties on the aspect it’s getting used. The massive distinction is it will probably solely be used for a single property, whereas type queries can be utilized for guidelines with a number of properties.

From a modifier to a method question

Let’s check out a card once more, first with a modifier like we’d do now. To alter it up, we’ll use an attribute modifier as a substitute of a category.

[data-card-density="compact"] {
  padding: .5rem;
  hole: .5rem;
}

Switching this up to make use of a method question, we may do one thing like this as a substitute:

[data-card-layout="alternative"] {
  --card-layout: various;
}

.card {
  @container type(--card-layout: various) {
    padding: .5rem;
    hole: .5rem;
  }
}

If we’re within the mindset of how we’re used to utilizing modifiers, this looks like much more work with no actual payoff, however now, something that may set a customized property can change it: a media question, a container question, a :has() selector, or a mother or father part. The types for the choice format are written as soon as, and there are many methods to modify them on, making it a lot extra versatile.

One simple win right here is when you’re working with a CMS. Now, the writer of the web page can selected from an possibility, which simply units one customized property worth for a piece of the web page, and the suitable types kick in.

The place I really feel like this has probably the most potential is when coping with conditional types just like the sidebar we checked out earlier, the place we will allow the compact card format nevertheless we would like:

.format {
  show: grid;
}

@media (width > 700px) {
  .format {
    grid-template-columns: 1fr 18rem;

    .sidebar {
      --card-density: compact;
    }
  }
}

We are able to achieve this far more

Right here’s an overflow grid sample I take advantage of fairly a bit:

.overflow-grid {
  show: grid;
  hole: 1rem;
  grid-auto-flow: column;
  grid-auto-columns: max(40%, 25ch);
  padding-block-end: 1rem;
  overflow: auto;
}

The issue is that it at all times overflows, even when I don’t have sufficient youngsters to warrant it. Like right here, the primary row, the third card is simply barely overflowing, making it awkward greater than something.

Two rows of three article cards that overflow the horizontal space, creating scrollbars.

One answer is to make use of a container question and :has(), the place we let the objects fill the area when there are fewer than 5 of them, and there’s sufficient area:

.overflow-grid {
  /* ... */

  /* fewer than 5 youngsters */
  &:not(:has(> :nth-child(5))) {
    @container (width > 75ch) {
      grid-auto-columns: 1fr;
    }
  }
}

And this works rather well! However generally, we’d must make some larger adjustments relying on the place the weather are getting used.

Let’s say, for instance, our web page has a hero element that goes to 2 columns on bigger screens, with a heading on one facet and a set of featured articles in an overflow grid on the opposite. The container question is ensuring the overflow is on within the slim area, however horizontal scrolling there appears a bit of awkward.

When the cut up occurs, I’d reasonably the grid scrolled vertically as a substitute, and we will leverage type queries to drag this off.

First, we give the overflow grid a configuration possibility for its path, defaulting to columns:

.overflow-grid {
  grid-auto-flow: var(--overflow-grid-direction, column);
}

Now the cut up can change when it goes to 2 columns:

.cut up {
  @container most important (width > 40rem) {
    --overflow-grid-direction: row;

    grid-template-columns: repeat(2, 1fr);
  }
}

And this works, however the consequence isn’t nice. The grid remains to be sized for horizontal scrolling, so we find yourself with a tall, awkward column, and no overflow in any respect.

That is the place type queries are available. The overflow grid can “hear” for the path change and modify its personal types to match.

.overflow-grid {
  @container type(--overflow-grid-direction: row) {
    grid-template-columns: 1fr; /* a single, full-width column */
    max-block-size: 50vb; /* preserve the overflow, however vertically */
  }
}

The cut up part isn’t styling the overflow grid straight, which is feasible, however at all times awkard, and results in fragile code bases. As an alternative, we created some configurable settings on that grid, and easily adjusted them as wanted.

They aren’t a alternative for modifiers

I don’t assume type queries imply it’s time to throw out modifier lessons (or information modifiers). They nonetheless work nice, particularly since a method question can’t have a look at the aspect it’s styling. If I merely need my button to be an accent button, or a tablet form, then a modifier makes plenty of sense.

On high of that, a daily media question or container question, and even an intrinsic sample would possibly get the job finished, and that’s nice too.

Wrapping up

The place type queries shine is when the situation and the types must reside in other places, or when one situation must have an effect on a number of issues directly, just like the cut up above. As an alternative of a format reaching in and styling its youngsters, it adjustments a number of settings, and every element decides how you can reply.

That’s a giant a part of what I consider as writing resilient CSS: parts that preserve working, and adapt sensibly, irrespective of the place they find yourself.


This text is customized from a lesson in Writing Resilient CSS, the latest module in my course, CSS Demystified. The module covers configuration choices with customized properties, math capabilities, relative colours, :has(), type queries, and extra, all put to work in sensible, reusable patterns. The course is 32% off till December 2, 2026.

Tags: ConditionalmodifiersQueriesStyle
Admin

Admin

Next Post
The Obtain: AI swamping open data, and firefighting for permafrost

The Obtain: AI swamping open data, and firefighting for permafrost

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended.

Genshin Affect codes for April 2025

Genshin Affect codes for April 2025

April 16, 2025
The Productiveness Technique That Truly Works

I Evaluated the 6 Finest Time and Attendance Software program in 2026

April 6, 2026

Trending.

HunyuanCustom Brings Single-Picture Video Deepfakes, With Audio and Lip Sync

HunyuanCustom Brings Single-Picture Video Deepfakes, With Audio and Lip Sync

May 8, 2025
AI & data-driven Starbucks – Deep Brew

AI & data-driven Starbucks – Deep Brew

May 18, 2026
A SQL MERGE assertion performs actions primarily based on a RIGHT JOIN

Use jOOQ’s code generator to simply go table-valued parameters

April 26, 2025
A SQL MERGE assertion performs actions primarily based on a RIGHT JOIN

With jOOQ – Java, SQL and jOOQ.

May 25, 2025
A SQL MERGE assertion performs actions primarily based on a RIGHT JOIN

Implicit path joins could now skip pointless tables within the be a part of tree

April 11, 2025

AimactGrow

Welcome to AimactGrow, your ultimate source for all things technology! Our mission is to provide insightful, up-to-date content on the latest advancements in technology, coding, gaming, digital marketing, SEO, cybersecurity, and artificial intelligence (AI).

Categories

  • AI
  • Coding
  • Cybersecurity
  • Digital marketing
  • Gaming
  • SEO
  • Technology

Recent News

The Obtain: AI swamping open data, and firefighting for permafrost

The Obtain: AI swamping open data, and firefighting for permafrost

November 25, 2026
Type Queries as Conditional Modifiers

Type Queries as Conditional Modifiers

November 25, 2026
  • About Us
  • Privacy Policy
  • Disclaimer
  • Contact Us

© 2025 https://blog.aimactgrow.com/ - All Rights Reserved

No Result
View All Result
  • Home
  • Technology
  • AI
  • SEO
  • Coding
  • Gaming
  • Cybersecurity
  • Digital marketing

© 2025 https://blog.aimactgrow.com/ - All Rights Reserved