-1

.d1 {
  background: lightblue;
  display: flex;
  padding: 5px;
}

img {
  flex: 1;
}
<div class="d1">
  <img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg">
  <img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg">
  <img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg">
  <img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg">
  <img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg">
</div>

I want to display for example 5 big-sized images in a row inside the flexbox. I want all the images width to fit the current screen and not wrap at the bottom. I tried different combinations of flex: 1, width: 100% of the img tag but none of it worked...

j08691
  • 204,283
  • 31
  • 260
  • 272
darkchampionz
  • 1,174
  • 5
  • 24
  • 47

3 Answers3

1

Taken from this answer about why images do not resize in a flexbox layout:

An initial setting on flex items is min-width: auto. This means that a flex item, by default, cannot shrink below the size of its content.

This means that the image will not shrink unless it is given a width. To get around this, you can wrap your image in a div (which will shrink to fill the flex container) and then give the image a width of 100%

.d1 {
  background: lightblue;
  display: flex;
  padding: 5px;
}

img {
  display: block;
  width: 100%;
}
<div class="d1">
  <div><img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg"></div>
  <div><img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg"></div>
  <div><img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg"></div>
  <div><img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg"></div>
  <div><img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg"></div>
</div>
Pete
  • 57,112
  • 28
  • 117
  • 166
0

You need to justify-content the container. You can then set a flex-basis to the images. I've used space-between here but any of the other values would work.

.d1 {
  display: flex;
  border: 5px solid black;
  justify-content: space-between;
}

img {
  flex: 0 0 20%;
  width: 20%;
}
<div class="d1">
  <img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg">
  <img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg">
  <img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg">
  <img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg">
  <img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg">
</div>

If you don't know the number of images you'll have then I'd suggest wrapping the image in a div as that will flex normally.

SpaceBeers
  • 13,617
  • 6
  • 47
  • 61
0

As per your description provided in the question area, we thought that you need a container with full width and a horizontal scroll containing five full-width images.

.scroll-wrap { border: 5px solid lightblue; width: 100%; overflow: auto;}
.d1 {display: flex; width: 500%;}
.d1 img{width: 20%;}
<div class="scroll-wrap">
  <div class="d1">
    <img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg">
    <img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg">
    <img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg">
    <img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg">
    <img src="https://upload.wikimedia.org/wikipedia/commons/3/3c/Cityoflondon2019june.jpg">
  </div>
</div>
Yudiz Solutions
  • 4,216
  • 2
  • 7
  • 21