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

How to place several divs next to another inside of a container without affecting the container or the children dimensions

I need to place several divs inside a container div. The main issue is the with of the container is 100% to fit the available space and the inner divs can overflow it.

The inner divs do have a fixed width and height.

The main requirement is the container div displays an horiz scrollbar if overflow.

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

I have this:

<div style={{ width: "70%", background: "azure" }}>
    <div
      style={{
        width: "100%",
        overflowX: "auto",
        display: "flex",
        border: "solid 1px #c1c1c1",
        padding: "5px"
      }}
    >
      {Array.from({ length: 10 }, (_, index) => (
        <div
          key={index}
          style={{
            width: "120px",
            height: "200px",
            border: "solid 1px",
            backgroundColor: "lightblue",
            marginRight: "5px"
          }}
        >
          Item {index + 1}
        </div>
      ))}
    </div>
  </div>

But instead of displaying the horizontal scrollbar it insists in resize divs. Here is the corresponding sandbox.

How can I solve this? What am I missing here?

>Solution :

Set the flex-shrink CSS property to 0 so that the inner <div> elements do not shrink.

<div
  key={index}
  style={{
    width: "120px",
    height: "200px",
    border: "solid 1px",
    backgroundColor: "lightblue",
    marginRight: "5px",
    flexShrink: 0
  }}
>
  Item {index + 1}
</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