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

CSS: Make image fill parent but not change parent size

I have an image gallery and all of the images are squares. The images are in their own personal divs and a big div with the display to flex to parent all the divs. I want the images to fill their parent div, but not make it bigger.

UPDATE: I saw on this website that the flex-grow property does NOT add to the (usable) width of the element. Go see the website for a further explanation, but that basically means i have to use something else than flex-grow to make my parent divs the width of the top div.
Also, i do not want a fixed amount of items on a line!! What I want is when you scale up the window, the elements get wider and wider until a point, where you then shrink the elements and add one from under to the line above.

Here is what i have:
What i have:

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

Here is what i want:
What i want:

<div class="ImagesContainer">

  <div> <img> </div>
  <div> <img> </div>
  <div> <img> </div>
  <div> <img> </div>
  <div> <img> </div>
  <div> <img> </div>
  <div> <img> </div>
  <div> <img> </div>
  <div> <img> </div>
  <div> <img> </div>

</div>

Here is the css:

.ImagesContainer {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: .25rem;
    margin-top: 1rem;
    width: 100%;
}

.ImagesContainer img {
    width: 20vw;
    min-width: 200px;
    margin: 5px;
    transition-duration: 200ms;
}

.ImagesContainer div {
    background-color: red;
    flex-grow: 1;
}

Anybody has an idea?

>Solution :

You need to give width to image wrapper only and fit the image appropriately. If you want some part of your div be seen, you can add padding property to it.
By adding "object-fit: cover" property to your image you can scale it to fit its parent’s width, and "object-position: center" in case your image is not square and a part of it gets cut.

.ImagesContainer {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: .25rem;
    margin-top: 1rem;
    width: 100%;    
}

.ImagesContainer img {     
    transition-duration: 200ms;
    object-fit: cover;
    object-position: center;
}

.ImagesContainer div {
    background-color: red;
    flex-grow: 1;    
    width: 20vw;
    min-width: 200px;
    padding: 5px;
}
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