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 make autoresize flex elements?

I have some squares inline:

.parent {
  padding: 0;
  list-style: none;
  display: flex;
  gap: 20px;
}

.parent div {
  background: red;
  width: 120px;
  height: 120px;
}
<div class="parent">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>

I need to resize .parent div if the blocks are not fitted inline.

How to do that using 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

>Solution :

If you want the divs to resize, you should not use absolute units such as px, but write them as relative size against the parent div, such as in %. However, you might want to max them out at a certain size before they get too big, so you might want to use a max-width such as follows.

.parent {
  padding: 0;
  list-style: none;
  display: flex;
  gap: 20px;
}

.parent div {
  background: red;
  width: 20%;
  max-width: 120px;
  aspect-ratio: 1;
}
<div class="parent">
  <div></div>
  <div></div>
  <div></div>
  <div></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