Swiper carousel width becomes extremely large with flex-col layout

I’m running into an issue where the Swiper component width grows to massive values when using flex-col direction in my container. When I check the browser inspector, the width shows something crazy like ‘33599999999px’. This only happens with flex-col but works fine with flex-row.

Here’s my test component:

"use client"
import { Swiper, SwiperSlide } from 'swiper/react';
import { Navigation } from 'swiper/modules';
import 'swiper/css';
import 'swiper/css/navigation';

const SliderTest = () => {
    const weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
    const currentDate = new Date();

    const createSlides = () => (
        weekdays.map((day, idx) => (
            <SwiperSlide key={idx}>
                <div className='text-center'>{day}</div>
            </SwiperSlide>
        ))
    );

    const buildSlider = () => {
        return (
            <Swiper
                modules={[Navigation]}
                navigation
                spaceBetween={20}
                slidesPerView={1}
            >
                {createSlides()}
            </Swiper>
        );
    };

    return (
        <div className='flex flex-col gap-4 p-4 border bg-gray-900 md:flex-row'>
            {buildSlider()}
            {buildSlider()}
        </div>
    );
};

export default SliderTest;

With flex-row everything displays correctly, but switching to flex-col causes this width explosion. Has anyone encountered this before?

Had this exact nightmare with Swiper a few months back. Swiper calculates width based on the parent container, and flex-col breaks that calculation completely.

Easiest fix is adding explicit width constraints to your Swiper container. Wrap your Swiper in a div with fixed or max width:

<div className='flex flex-col gap-4 p-4 border bg-gray-900 md:flex-row'>
    <div className='w-full max-w-md'>
        {buildSlider()}
    </div>
    <div className='w-full max-w-md'>
        {buildSlider()}
    </div>
</div>

If Tailwind doesn’t work, add width: 100% directly to the Swiper component using the style prop.

Basically, flex-col creates a different sizing context than flex-row, so Swiper gets confused when measuring available space.

yeah, I’ve hit this swiper + flex bug before. set overflow: hidden on the parent container - flex-col creates weird overflow that tricks swiper into thinking it’s got infinite space. also try flex-shrink-0 on each swiper to stop them from shrinking and triggering that recalculation mess.

This happens because Swiper and flex-col clash during render. Instead of fighting CSS fixes that’ll probably break later, I’d just automate the whole carousel setup.

I did this for a client dashboard that needed dynamic carousels based on changing data. Rather than debug Swiper weirdness constantly, I automated the workflow to generate carousel components with pre-calculated dimensions.

The automation detects layout, calculates container sizes, and generates responsive breakpoints. No more guessing with min-width or max-width. When data changes, it rebuilds the carousel config automatically.

This killed all those weird Swiper edge cases we kept running into. Consistent sizing no matter what flex direction you use, and you never touch container dimensions manually again.

You can set this up pretty easily: https://latenode.com

Yeah, this is a classic Swiper + flexbox column bug. Swiper tries to calculate width before the flex container figures out its dimensions. Had this exact issue when I built a dashboard with vertical cards. What fixed it for me was adding min-width: 0 to the Swiper container. Flexbox items have this weird implicit minimum width that screws with Swiper’s sizing. Just add the min-w-0 class:

<div className='flex flex-col gap-4 p-4 border bg-gray-900 md:flex-row min-w-0'>

Or you can delay Swiper initialization with useEffect so the container renders fully first. Those crazy width values happen because of timing - flex layout and Swiper init are stepping on each other.

I’ve hit this exact problem tons of times with dashboard layouts. Swiper’s width calculation gets messed up by how flex-col handles sizing.

What always works for me: add contain: layout to the Swiper container. This CSS property isolates the layout calculations so Swiper doesn’t inherit weird dimensions from the flex-col parent.

<div className='flex flex-col gap-4 p-4 border bg-gray-900 md:flex-row'>
    <div style={{contain: 'layout'}}>
        {buildSlider()}
    </div>
    <div style={{contain: 'layout'}}>
        {buildSlider()}
    </div>
</div>

Or try setting allowTouchMove: false during component mount, then turn it back on once the layout settles. I use this when multiple components are fighting over sizing.

This video covers a bunch of Swiper configuration gotchas if you want to see best practices:

The contain property trick has saved me hours of debugging weird Swiper sizing issues.

Swiper initializes before the flex-col container figures out its dimensions. It tries to measure the container width during initial render, but flex-col doesn’t give explicit width constraints like flex-row does. I fixed this by forcing Swiper to recalculate after the container stabilizes. Add a key prop that changes with your flex direction, or set updateOnWindowResize to true in your Swiper config. You can also try observer: true and observeParents: true in the Swiper parameters - this makes it watch for DOM changes and recalculate automatically. Another option is setting a specific height on your flex-col container. Swiper gets confused when the parent has undefined dimensions in the cross axis.