Skip to main content

Front-End Development

How to Write Media Queries in Optimizely Configured Commerce (Spire)

Online News On A Smartphone And Laptop, Woman Reading News Or Articles In A Mobile Phone Screen Application At Home. Newspaper And Portal On Internet.

Media queries are essential for modern websites. They enable responsive design by allowing your site or UI to adapt seamlessly across different device sizes. In this blog, we’ll explore how to implement media queries in Optimizely Spire and how to update or customize breakpoints to suit your design needs.

Let’s get started!

Setting Up Breakpoints

First, we’ll define breakpoints based on our style guide. Breakpoints help your website adjust its layout depending on the screen size, such as mobile, tablet, or desktop.

In Optimizely Spire, breakpoints are defined within the theme configuration file. To update these breakpoints in the base code, you need to create a Start.tsx file—if it hasn’t been made already.

Path: modules/blueprints/CustomBlueprint/src/Start.tsx

Where to Place Breakpoint Configuration

Place the breakpoint configuration within either the setPreStyleGuideTheme or setPostStyleGuideTheme functions. These functions allow you to customize the theme before or after the default style guide is applied.

This setup ensures that your custom breakpoints are recognized and used consistently throughout your application.

Below is the configuration structure for setting custom breakpoints in Optimizely Spire:

Breakpoint Configured commerce

Understanding the Breakpoint Configuration

To make your site responsive, you define breakpoints—specific screen widths where your layout should adapt. Here’s what each part of the configuration means:

  • Keys – Represent screen size labels such as:

These are shorthand names for different device sizes:

    • xs – Extra small (e.g., small phones)
    • sm – Small (e.g., larger phones)
    • md – Medium (e.g., tablets)
    • lg – Large (e.g., small laptops)
    • xl – Extra large (e.g., large desktops)
  • Values – Minimum Widths

These define when each screen size category starts:

    • xs starts at 0px
    • sm starts at 576px
    • md starts at 768px
    • lg starts at 992px
    • xl starts at 1200px
  • Max Widths – Content Limits

These define the maximum width the content area can grow to at each breakpoint. Even if the screen is wider, the content won’t stretch beyond these limits (example: 1140px for xl).

Think of it like this:

  • On a phone → small screen → use xs styles
  • On a tablet → medium screen → use md styles
  • On a laptop or desktop → big screen → use lg or xl styles

Creating Media Queries in a Standardized Way

To create media queries in Optimizely Spire, we rely on the breakpoint configuration defined earlier. This ensures consistency across the application and simplifies responsive design.

How It Works

Behind the scenes, Spire uses a utility called breakpointMediaQueries to convert standardized media query definitions into browser-readable CSS.

Utility Location:  modules\mobius\src\utilities\breakpointMediaQueries.ts

This utility helps generate clean and consistent media queries based on your defined breakpoints.

Available Media Query Types:

Here are three types of media query options available:

  • min – Applies styles when the screen is at or above a certain width.
  • max – Applies styles when the screen is at or below a certain width.
  • minMax – Applies styles between two specific widths.

Default Media Query Structure

Here’s what a typical media query definition looks like using this utility:

Media query structure configured commerce

Now let’s see how this media query utility works with min, max, and minMax.

  • min

The min-width media query structure will be as follows:

Min Width media query configured commerce

 Example: To apply styles for min-width: 576px, insert your CSS code in the appropriate section.                                               Min Width media query example configured commerce

Similarly, if you want to apply CSS for min-width: 768px, just replace the respective null with your CSS code.

  • max

The max-width media query structure will be as follows:

Max Width media query configured commerce

Example: To apply styles for max-width: 575px, insert your CSS code in the appropriate section.Max Width media query example configured commerce

Similarly, if you want to apply CSS for max-width: 767px, just replace the respective null with your CSS code.

  • minMax

The minMax media query structure will be as follows:minMax Width media query example configured commerce

Example: To apply styles between min-width: 576px and max-width: 767px, insert your CSS code in the appropriate section.minMax Width media query example configured commerce

 

Similarly, if you want to create media queries for other screen sizes, follow the same approach as described above.

You can also define multiple CSS rules as shown in the examples below.

With Multiple Css Option configured commerce

Conclusion

Creating responsive layouts doesn’t have to be complicated or repetitive. A utility like breakpointMediaQueries helps you apply consistent styling logic throughout your application. Whether you use a mobile-first (min), desktop-first (max), or range-based (minMax) approach, this system lets you apply styles exactly where they’re needed.

By simplifying the media query logic, your code becomes easier to read and maintain, so you can focus more on design and less on repetitive code. Choose the approach that best fits your project, and let your breakpoints handle the rest.

 

Thoughts on “How to Write Media Queries in Optimizely Configured Commerce (Spire)”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Purushottam Rathi

As a Senior Technical Consultant at Perficient, Purushottam brings over four years of experience in front-end development, specializing in Magento and Optimizely within the E-commerce domain. He is deeply passionate about technology, consistently shares knowledge with his team, and continuously pursues learning opportunities in emerging technologies.

More from this Author

Follow Us