1

how do I make the three images on the top not go outside the div while keeping them to be 100% width so that their size remains the same and will be resized based on the browser size?

PS: I need them to be in row form

The ideal result should be like this:

enter image description here

#main {
  background-color: #666666;
  padding: 60px;
  width: 390px;
}

#second {
  display: flex;
  flex-direction: row;
  width: 100%;
  margin-bottom: 30px;
}

.img-child {
  width: 100%;
  margin-right: 30px;
}

.img-child:last-child {
  margin-right: 0;
}

#img-parent {
  width: 100%;
}
<div id=main>
  <div id=second>
    <img class="img-child" src="http://via.placeholder.com/1600x900">
    <img class="img-child" src="http://via.placeholder.com/1600x900">
    <img class="img-child" src="http://via.placeholder.com/1600x900">
  </div>
  <img id="img-parent" src="http://via.placeholder.com/1600x900">
</div>
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

5 Answers5

5

you need to add a min-width to your image. flex needs this for whatever reason, otherwise it will not downscale the elements. I come across this every time i use flex :D

.img-child {
    min-width: 0;
    width: 100%;
    margin-right: 30px;
}
TheWuif
  • 988
  • 6
  • 11
0

You can use max-width for each img. I have edited your code for calculate padding each image below...

#main {
  background-color: #666666;
  padding: 60px;
  width: 390px;
 
}

#second > div {
   
  flex-basis: 100%;
  flex-grow:1;
  flex-shrink: 1;
  padding-left: 15px;
  padding-right:15px;
  max-width:100%;

}

.img-child {
  max-width:100%;
}
#second {
  display: flex;
  flex-direction: row;
  margin-bottom: 30px;
  margin-left:-15px;
  margin-right:-15px;
}

#img-parent {
  width: 100%;
}
<div id=main>
  <div id=second>
    <div>
      <img class="img-child" src="http://via.placeholder.com/1600x900">
    </div>
    <div>
      <img class="img-child" src="http://via.placeholder.com/1600x900">
    </div>
    <div>
      <img class="img-child" src="http://via.placeholder.com/1600x900">
    </div>
    <div>
      <img class="img-child" src="http://via.placeholder.com/1600x900">
    </div>
    <div>
      <img class="img-child" src="http://via.placeholder.com/1600x900">
    </div>
  </div>
  <img id="img-parent" src="http://via.placeholder.com/1600x900">
</div>
Hery Kurniawan
  • 344
  • 2
  • 8
0

Try this :

body
{
    height:100vh;
    justify-content:center;
    align-items:center;
    display:flex;
}
#main {
  width:391px;
  background-color: #666666;
}
#main .img
{
    width:100px;
    float:left;
    margin:15px;
}
#main .img:nth-child(4)
{
    width:360px;
}
img
{
    max-width:100%;
}
<div id=main>
    <div class="img">
        <img src="http://via.placeholder.com/1600x900">
    </div>
    <div class="img">
        <img src="http://via.placeholder.com/1600x900">
    </div>
    <div class="img">
        <img src="http://via.placeholder.com/1600x900">
    </div>
    <div class="img">
        <img src="http://via.placeholder.com/1600x900">
    </div>
</div>
Anonymous
  • 539
  • 2
  • 6
-1

* {
  box-sizing: border-box;
}
.main {
  background-color: #666666;
  padding: 60px;
  width: 100%;
}

.second {
  background-color: #666666;
  display: flex;
  flex-direction: row;
  margin-bottom: 10px;
}
.column {
  float: left;
  width: 33.3%;
  padding: 5px;
}
.largep {
  background-color: #666666;
  display: flex;
  flex-direction: row;
  width: 100%;
  padding: 5px;
}

/* Clearfix (clear floats) */
.row::after {
  content: "";
  clear: both;
  display: table;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3pro.css">
<style>

</style>
</head>
<body>


<div class="row main">
  <div class="column second">
    <img src="https://www.w3schools.com/howto/img_snow.jpg" alt="Snow" style="width:100%">
  </div>
  <div class="column second">
    <img src="https://www.w3schools.com/howto/img_forest.jpg" alt="Forest" style="width:100%">
  </div>
  <div class="column second">
    <img src="https://www.w3schools.com/howto/img_mountains.jpg" alt="Mountains" style="width:100%">
  </div>
  <div class="largep">
    <img src="https://www.w3schools.com/howto/img_mountains.jpg" alt="Mountains" style="width:100%">
  </div>
</div>

</body>
</html>
Weyers de Lange
  • 280
  • 2
  • 11
-1

#second{
display: flex;
align-items: center;
justify-content: space-around;  
}
.img-child{
width: 30%;
margin-top: 30px;
 margin-bottom: 30px;
}
#img-parent{
  width: 97%;
  margin-left: 12px;
}
<div id=main>
  <div id=second>
    <img class="img-child" src="http://via.placeholder.com/1600x900">
    <img class="img-child" src="http://via.placeholder.com/1600x900">
    <img class="img-child" src="http://via.placeholder.com/1600x900">
  </div>
  <img id="img-parent" src="http://via.placeholder.com/1600x900">
</div>
Sachin Yadav
  • 748
  • 9
  • 28