Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does Flex causes multiple images to shrink?

I want to create a carousel of identically sized images using Tailwind CSS. I want to have 5 images next to each other that the viewer will be able to scroll left and right to see the images. When the images were imported without Flex, they fit the max-width that i’ve set beforehand. however, after implementing Flex, the images did become side by side, but their width shrink.

In my trial i’m just using the same image 5 times. I have set the max-width of the container to be 1200px. When importing the image, i’ve set the image to w-full, and each images successfully fit the 1200px requirement. However, when i put Flex, the images are placed next to each other but with reduced size.

As you can see on the first image, **before **Flex, the right side of the image is directly below the O in "Our Program".
before flex

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

but after Flex, the first image’s width only reaches the A in "About Us".
after flex

How do I stop this from happening? I don’t want the image to shrink. I have also written shrink-0

This is how i’ve written the program:

 <div id="scroll header" className="slider-container">
          <div className="relative my-0 mx-auto max-w-[1200px]"> {/* Class: Slider-wrapper */}
            <div className="flex shrink-0 overflow-x-auto h-auto mx-auto"> {/* Class: Slider */}
              <img src="/assets/banner2.jpg" className="w-full"/>
              <img src="/assets/banner2.jpg" className="w-full"/>
              <img src="/assets/banner2.jpg" className="w-full"/>
              <img src="/assets/banner2.jpg" className="w-full"/>
              <img src="/assets/banner2.jpg" className="w-full"/>
            </div>

>Solution :

The default value of flex-shrink is 1. Thus, when the sum of flex child elements’ lengths in the main flex axis is larger than the available space in that axis, the elements will shrink in that dimension until they all fit or until they meet their min-content size in that direction.

In your situation, they shrink up to the width that they do because that is the image file’s natural width, which is the calculated min-content width.

Thus, to stop the images from shrinking (and thus keep to the width: 100% from w-full), you could consider applying flex-shrink: 0 via the shrink-0 Tailwind class name:

<script src="https://cdn.tailwindcss.com/3.4.3"></script>

<div class="relative my-0 mx-auto max-w-[1200px]">
  <div class="flex shrink-0 overflow-x-auto h-auto mx-auto">
    <img src="https://picsum.photos/1000/300" class="w-full shrink-0"/>
    <img src="https://picsum.photos/1000/300?" class="w-full shrink-0"/>
    <img src="https://picsum.photos/1000/300?0" class="w-full shrink-0"/>
    <img src="https://picsum.photos/1000/300?1" class="w-full shrink-0"/>
    <img src="https://picsum.photos/1000/300?2" class="w-full shrink-0"/>
  </div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading