0

CodeSandbox: https://codesandbox.io/s/upbeat-sanne-mdh5s

I'll try to reproduce it here with a minimal example that I've built on CodeSandbox.

I've been having some troubles while keeping the proper width for a the items of a nested flex container. What I mean by nested, is that this (inner) flex container lives inside an another (outer) flex container.

Alright, I have an outer flex container that splits my layout into 2 columns. One column is for the main content and one for a side bar.

const Layout_DIV = styled.div`
  display: flex;                    // THIS IS THE OUTER FLEX CONTAINER
`;

const Content_DIV = styled.div`
  flex: 1 1 1000px;                 // THIS WILL EXPAND OR SHRINK WHEN NECESSARY
`;

const SideBar_DIV = styled.div`
  flex: 0 0 200px;                  // THIS RENDERS A SIDE BAR OF WIDTH 200px
`;

And inside that Content_DIV I have a carousel with overflow-hidden and I need to render some product cards inside of it.

const Carousel_DIV = styled.div`
  width: 100%;
  overflow: hidden;
`;

const FlexContainer_DIV = styled.div`
  display: flex;                            // THIS IS THE INNER FLEX CONTAINER
`;

const FlexItem_DIV = styled.div`
  flex: 0 0 25%;
`;

And inside those FlexItem_DIV I will render the product cards. With a thumbnail and a title. Since, the FlexItem_DIV has flex: 0 0 25%, only 4 cards should be visible at a time, and they should all fit inside Content_DIV

When I'm rendering only the thumbnails, everything works fine and I get the intended behavior. Nice!

enter image description here

But as soon as I add the product title, that behavior changes completely and everything gets messed up. Nothing fits anymore.

enter image description here

The only difference in those two GIFs above is the presence of the title. Literally nothing else was changed.

What I've tried:

  • Lots of things. Including giving a px width for the flex item, instead of 25%
  • I've also tried working with the white-space: wrap for the title, but nothing works.

What I think is causing this:

This seems to be caused by the presence of the outer flex container. I mean, that one that handles the split of the columns for the Content_DIV and SideBar_DIV.

Cause when I get rid of it (the outer flex container), then I get the desired behavior. See now that the SideBar is rendering below the Carousel and not on the right side anymore. Of course, I need it to be on the side and not below. That's why this solution does not work for me. But just to illustrate, I'll do this:

const Layout_DIV = styled.div`
  /* display: flex; */              <---- I'VE COMMENTED THIS LINE
`;

While this works as far as shrinking the title div and fitting everything on the screen, I really need the outer flex container so I can have the SideBar on the side, and not below.

enter image description here

PS: Tested both on Chrome and Firefox.

QUESTION

Has anyone seen this problem before? I need the behavior from the 1st GIF, but with the product titles present.

CodeSandbox: https://codesandbox.io/s/upbeat-sanne-mdh5s

EXTRA

I don't think this is relevant for the issue, but this is the code of the product card that renders inside FlexItem_DIV:

const CardContainer_DIV = styled.div``;

const AspectRatio_DIV = styled.div`
  position: relative;
  height: 0;
  padding-top: 100%;
`;

const Thumbnail_DIV = styled.div`
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
`;

const Thumbnail_IMG = styled.img`
  width: 100%;
  height: 100%;
  object-fit: cover;
`;

const CardTitle_DIV = styled.div`
  width: 100%;
  max-width: 100%;
  font-size: 22px;
  font-weight: bold;
  line-height: 1.1em;
`;
cbdeveloper
  • 27,898
  • 37
  • 155
  • 336
  • 1
    Same as your [previous question today](https://stackoverflow.com/q/63614638/3597276), no? See my explanation in the comments there. – Michael Benjamin Aug 27 '20 at 16:48
  • 1
    @MichaelBenjamin yes! This was an attempt to make the question more clear. This almost drove me crazy today. Worst thing I've ever had to debug. The `min-width` did the trick. I think this question ended up better than the other one. I'll delete the other. – cbdeveloper Aug 27 '20 at 16:50
  • 1
    I think you're stumbling onto the [flex minimum size algorithm](https://stackoverflow.com/q/36247140/3597276). Add `min-width: 0` to `const Content_DIV = styled.div`. And try switching the `font-size` in `const CardTitle_DIV = styled.div` from `22px` to, let's say, `2vw`. – Michael Benjamin Aug 27 '20 at 16:52
  • That's it. I could make it work by setting `min-width: 0` to `Content_DIV`, which is the outer `flex` item. Then it fixed the `width` for the nested `flex` container's items. Thanks a lot, man. This trick feels like "dark-magic", though. They should "fix" this somehow, or add a property with a more friendly name that allows us to work with that solution more easily. Thanks again. – cbdeveloper Aug 27 '20 at 16:56
  • 1
    You're not alone. See the comments [here](https://stackoverflow.com/a/38383437/3597276), [here](https://stackoverflow.com/a/43312314/3597276) and [here](https://stackoverflow.com/a/36247448/3597276). – Michael Benjamin Aug 27 '20 at 17:00
  • @MichaelBenjamin This is surreal... horrible pitfall to run into. Do you know if this solution works fine across browsers? The `min-width: 0` "trick" ? – cbdeveloper Aug 27 '20 at 17:06
  • 1
    Yep. Probably millions of hours wasted trying to figure this one out. As one [commenter mentioned](https://stackoverflow.com/a/38383437/3597276), this "feature" should be disclosed front and center in flex documentation. – Michael Benjamin Aug 27 '20 at 17:10

0 Answers0