Container query not working

Hello, my container query on .gridContainer is not working at all. If i change it to @media it works fine and dandy but the moment i try to use @container it just breaks and doesnt create me a 4 column layout on desktop as it should :
@use "sass:math";

@function rem($pixels) {
@return math.div($pixels, 16) * 1rem;
}

flynt-component[name='HeadingBoxesParagraph'] {
background-color: var(--color-blue-10);
margin-block-start: 0;
padding-block: clamp(rem(80), 16vw, rem(160));
padding-inline: var(--container-spacing);

.heading {
text-align: center;
}

.gridContainer {
container-name: grid-test;
container-type: inline-size;
display: grid;
gap: clamp(rem(16), 4vw, rem(32));
grid-template-columns: repeat(2, 1fr);
justify-content: center;
margin-block: 0;
margin-block-start: 10px;
max-inline-size: rem(1400);
padding-block: clamp(rem(32), 6vw, rem(64));
padding-inline: rem(16);

@container grid-test (width > calc(40ch * 4)) {
grid-template-columns: repeat(4, 1fr);
}

.box {
align-items: center;
background-color: var(--color-background, #fff);
block-size: 100%;
border-radius: rem(15);
box-shadow: 0 rem(4) rem(6) rgba(0, 0, 0, 0.1);
box-sizing: border-box;
display: flex;
flex-direction: column;
justify-content: space-between;
padding: clamp(rem(24), 4vw, rem(40));
text-align: center;
transition: all 0.3s ease;

&:hover {
box-shadow: 0 rem(6) rem(12) rgba(0, 0, 0, 0.15);
transform: translateY(rem(-5));
}
}
}
}
@use "sass:math";

@function rem($pixels) {
@return math.div($pixels, 16) * 1rem;
}

flynt-component[name='HeadingBoxesParagraph'] {
background-color: var(--color-blue-10);
margin-block-start: 0;
padding-block: clamp(rem(80), 16vw, rem(160));
padding-inline: var(--container-spacing);

.heading {
text-align: center;
}

.gridContainer {
container-name: grid-test;
container-type: inline-size;
display: grid;
gap: clamp(rem(16), 4vw, rem(32));
grid-template-columns: repeat(2, 1fr);
justify-content: center;
margin-block: 0;
margin-block-start: 10px;
max-inline-size: rem(1400);
padding-block: clamp(rem(32), 6vw, rem(64));
padding-inline: rem(16);

@container grid-test (width > calc(40ch * 4)) {
grid-template-columns: repeat(4, 1fr);
}

.box {
align-items: center;
background-color: var(--color-background, #fff);
block-size: 100%;
border-radius: rem(15);
box-shadow: 0 rem(4) rem(6) rgba(0, 0, 0, 0.1);
box-sizing: border-box;
display: flex;
flex-direction: column;
justify-content: space-between;
padding: clamp(rem(24), 4vw, rem(40));
text-align: center;
transition: all 0.3s ease;

&:hover {
box-shadow: 0 rem(6) rem(12) rgba(0, 0, 0, 0.15);
transform: translateY(rem(-5));
}
}
}
}
3 Replies
snxxwyy
snxxwyy2mo ago
you haven't specified what you want to apply the grid-template-columns to. you'd need to do something like this. Container queries are like media queries where you have to do this.
@container grid-test (width > calc(40ch * 4)) {
.gridContainer {
grid-template-columns: repeat(4, 1fr);
}
}
@container grid-test (width > calc(40ch * 4)) {
.gridContainer {
grid-template-columns: repeat(4, 1fr);
}
}
Even then, that won't work because you can't apply styles to the parent container with the container query. That's why @media works and this doesn't. It has to be a child of the parent that is the container. I'd wrap .gridContainer in a div and make that div the container, then apply the styles to .gridContainer like in my example since it would then be the child.
Chrislizardc
Chrislizardc2mo ago
Thanks dude, I was pulling my hair here, you saved me a lot of time
snxxwyy
snxxwyy2mo ago
no worries man, glad you sorted it.
Want results from more Discord servers?
Add your server