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

Do not fill the width of growable flex containers

I am building a flexbox container, the container width is resizable, each element should fill the container width horizontally and grow to fill the space between them, and additionally stack vertically if there are too many elements for the row.

The problem is that when there is a row with a single element it fills the entire width of the container, this is cause the flex-grow: 1; rule.

enter image description here

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

If I remove the flex-grow: 1; rule the elements stop growing and does not fill the space between them.

enter image description here

What I want is make elements growable to fill the space between them but don´t fill the entire width of the container.

Expected result:
enter image description here

article{
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    width: 800px;
    border: 1px solid blue;
}
section{
    min-width: 200px;
    box-sizing: border-box;
    flex-grow: 1;
    margin: 10px;
    padding: 10px;
    border: 1px solid black;
}
<article>
    <section>1</section>
    <section>2</section>
    <section>3</section>
    <section>4</section>
</article>

>Solution :

I think it would be better to use css grid instead:

* {
  box-sizing: border-box;
  margin: 0;
  padding:0;
}

article {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 15px;

  width: 800px;
  padding: 10px;
  border: 1px solid blue;
}

section {
  padding: 10px;
  border: 1px solid black;
}
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