☰
ELEMENTOR / PRACTICE

Container Queries in Elementor: Responsive Blocks Without Relying on Screen Width

Container Queries in Elementor: Responsive Blocks Without Relying on Screen Width

Elementor’s standard responsive settings and CSS media queries primarily respond to the viewport width—the full browser window. However, the same block can be placed in very different conditions: a product card in a wide grid may have plenty of space, while the same card in a sidebar may not.

CSS Container Queries solve this problem. They change an element’s styles based on the width of its parent container. As a result, one card can automatically switch from a vertical layout to a horizontal one exactly when enough space becomes available.

How Container Queries Differ from Breakpoints

A media query checks the viewport width. For example, the @media (min-width: 1024px) rule applies when the browser window becomes wide enough. However, this does not mean that a specific block has 1024 pixels available: some of that width may be occupied by a sidebar, padding, or other columns.

A container query checks the width of the container in which an element is placed. This approach is more convenient for reusable components such as product cards, testimonials, pricing plans, posts in a Loop Grid, and blocks used in different areas of a site.

For example, a card can remain vertical in a sidebar but become two-column in the main content area. You do not need to create separate card versions or choose a breakpoint that works equally well in every template.

  • A media query responds to the browser window width.
  • A container query responds to the width of a specified parent container.
  • One component can behave differently depending on where it is used.

Prepare the Structure in Elementor

For this example, create a parent container with cards inside it. Assign the cards-wrap class to the parent container and the product-card class to each card. Classes are added in the settings of the relevant elements or containers, in the CSS Classes section.

It is important that product-card is a descendant of the cards-wrap container in the HTML structure. If another container between them is also declared as a container with container-type, the browser will use the nearest container context.

Base styles for narrow widths should be defined without a Container Query. This provides the initial and safe display option for smaller screens.

  • Parent container: cards-wrap.
  • Card or repeated element: product-card.
  • Check that the card is actually inside the required container rather than being a neighboring element.
  • Keep the basic vertical structure until the query takes effect.
.product-card {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.product-card img {
  width: 100%;
  height: auto;
  display: block;
}

How to Enable Container Queries in Custom CSS

In the parent container’s Custom CSS, you need to declare it as a query container. In Elementor, selector is used to reference the current element. After that, the @container rule can check its available width.

The example below switches the card to CSS Grid when the parent container reaches 600 pixels in width. The number 600 is not a universal breakpoint: choose it based on the actual width required by your card’s image, heading, buttons, and text.

  • Add the CSS to the parent container rather than to each card separately.
  • Use selector to declare the container-type.
  • In @container, define styles specifically for the child product-card element.
selector {
  container-type: inline-size;
}

@container (min-width: 600px) {
  .product-card {
    display: grid;
    grid-template-columns: 140px 1fr;
    gap: 24px;
  }
}

What inline-size Means

The inline-size value indicates that the container query should respond to the container’s size along the primary horizontal axis. In a standard left-to-right document, this effectively means its width.

Unlike container-type: size, the inline-size option does not create a separate context for the block’s height. For adapting cards based on width, this is usually sufficient.

How to Adapt a Card Beyond Grid

A Container Query can change any styles required by the component: the direction of a flex container, image size, spacing, typography, element order, or the visibility of secondary content.

For example, at smaller widths, the image can appear above the content, while at wider container sizes it can occupy the first column. If the card contains separate inner elements, it is more convenient to assign them their own classes so that the rules do not depend on Elementor’s incidental structure.

  • Use grid-template-columns for a two-column layout.
  • Use flex-direction to change the direction of elements.
  • Do not automatically reduce the font size just to fit long text.
  • Check that the button, price, or heading does not break after switching to a horizontal layout.
@container (min-width: 600px) {
  .product-card {
    display: grid;
    grid-template-columns: 140px minmax(0, 1fr);
    align-items: start;
    gap: 24px;
  }

  .product-card__content {
    min-width: 0;
  }
}

@container (min-width: 900px) {
  .product-card {
    gap: 32px;
  }
}

Using Container Queries with Loop Grid and Sidebars

With Loop Grid, it is especially important to determine which element should act as the container. If all cards are placed inside one wide wrapper, they will respond to the width of that shared wrapper. This may be the right solution for a single grid.

If each repeated item needs to determine its available space independently, container-type should be assigned directly to the wrapper of the individual card, while the query should target its internal structure. The actual HTML hierarchy depends on the template, so it should be checked using the browser’s developer tools.

Container Queries are useful for sidebars because the component does not need to be tied to a global breakpoint. If the sidebar width changes due to other layout settings, the card will automatically reevaluate the condition.

  • Determine whether the query should apply to the entire grid or to each card individually.
  • Check the actual Loop Grid DOM structure in the browser.
  • Do not add container-type to an arbitrary inner element if it does not control the required width.
  • Account for internal padding and gap: they reduce the space available to the card.

Testing and Common Errors

After adding the CSS, test the card in several contexts: inside a wide main container, in a sidebar, at tablet width, and on a narrow mobile screen. Change not only the viewport size but also the width of the parent container itself.

If the rule does not work, first check whether container-type has been declared on the ancestor of product-card. Then check whether an Elementor style or another CSS rule with greater specificity is overriding it.

You should also verify that the class is assigned to the correct element. In builders with nested containers, it is easy to add cards-wrap to an inner block that does not have the required width, or to add product-card to a wrapper that repeats differently than expected.

  • A container query will not work without a container ancestor.
  • The query checks the container width, not the screen width.
  • Excessive padding, gap, or a fixed width may leave insufficient space for a second column.
  • Make sure the CSS was not inserted into a field that does not support the required rule.
  • Test the result in the browsers used by your audience.

Compatibility and Fallback Strategy

CSS Container Queries are supported by modern browsers, but projects that must support older environments should not rely solely on the enhanced layout mode. Base styles without @container should remain complete: the card should be readable, accessible, and usable even without switching to a horizontal structure.

You do not necessarily need to duplicate the entire component with a media query. In many cases, it is enough to keep the vertical card as a fallback and use a Container Query only to improve its appearance in wider containers.

Remember that responsive design is not limited to CSS. Check heading wrapping, contrast, keyboard focus, button accessibility, and the content reading order for screen readers.

  • The base version without @container must work independently.
  • A horizontal card should not reduce text readability.
  • Test more than the visual appearance, including keyboard navigation.
  • For critical components, verify support in the browsers specified by the project requirements.

Frequently asked questions

Can Container Queries be used without the cards-wrap class?

Yes. The class is mainly useful for maintaining a clear structure and managing styles conveniently. If the CSS is added to the Custom CSS of a specific parent container in Elementor, selector can be used to reference it.

Why does @container not respond when the screen width changes?

The query responds to the width of the nearest ancestor with container-type, not to the viewport. If the parent container’s width has not changed, or container-type was added to the wrong element, the result may remain unchanged.

What width should I use in min-width?

Choose it based on the component’s content. The card should switch to a horizontal layout only when the image, text, buttons, and internal spacing fit comfortably. 600 pixels is only an example, not a required value.

Do Container Queries replace all Elementor breakpoints?

No. They complement standard responsive design. Media queries are useful for global layout changes, while Container Queries are intended for components that may be used inside containers of different widths.

Conclusion

Container Queries make it possible to build more independent and reusable components in Elementor. A card adapts to the actual space available to it rather than to the conditions of the entire viewport.

Start with a basic vertical version, declare the correct parent container with container-type: inline-size, and add @container rules for wider states. Then check the DOM structure, text overflow, and the component’s behavior in a grid, sidebar, and Loop Grid.

← All Elementor materials