javascript – How to fix layout of the images inside a div, when div changes sizes?-ThrowExceptions

Exception or error:

I want to put images inside a div. However when the div changes size the images contained in the div don’t stay inside the div. Here are the images when the div has its “normal” size:

enter image description here

Here are the images when the div size is altered:

enter image description here

I want the div to add a scroll bar when the images don’t fit in the div.
Here is some code:

// Here is where the tag <img /> is created:
function getData(id) {
  $.get(`/api/IgnicoesAPI/${id}`, function(data) {
    $('#header').append('<h3 style="text-align:center;"><b>Avaliação da Ignição</b></h3>');
    $('#myDiv').append('<p>Estado:' + data.estado + '</p>')
    $('#myDiv').append('<p>ID: ' + id + '</p>');
    $.each(data.listaOcorrencias, function(o, ocorrencia) {
      //Adding images
      $('#myDiv').append('<img src="imagensFogos/' + ocorrencia.fotografia + '" onclick="openModal();" class="hover-shadow" style="width:200px; height:190px;" hspace="4"/>');
    });
    //MORE CODE
  });
}
/* The Modal (background) */

.modal {
  display: none;
  /* Hidden by default */
  position: fixed;
  /* Stay in place */
  z-index: 1;
  /* Sit on top */
  padding-top: 100px;
  /* Location of the box */
  left: 0;
  top: 0;
  width: 100%;
  /* Full width */
  height: 100%;
  /* Full height */
  overflow: auto;
  /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);
  /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);
  /* Black w/ opacity */
}


/* Modal Content */

.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
  height: 80%;
}


/* The Close Button */

.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}

.buttonsClass {
  position: absolute;
  bottom: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="myModal" class="modal">


  <div class="modal-content">
    <span class="close">&times;</span>
    <div id="header" class="headerClass"></div>
    //Here is where the images are stored
    <div id="myDiv" class="added">

    </div>
    <div id="buttons" class="buttonsClass"></div>

  </div>

</div>
How to solve:

Use display: flex to force all images in a row, then add overflow-x: auto to add a horizontal scrollbar when needed.

.modal-content {
  width: 400px;
  padding: 20px;
  border: darkgrey solid 2px;
}

#myDiv {
  max-width : 100%;
  overflow-x : auto;
  border : blue dashed 1px;
  display : flex;
}

img{
  margin-right : 10px;
}
<div class="modal-content">

  <div id="myDiv" class="added">
    <img src="https://picsum.photos/200/150" />
    <img src="https://picsum.photos/190/150" />
    <img src="https://picsum.photos/195/150" />
    <img src="https://picsum.photos/150/150" />
  </div>

</div>

Leave a Reply

Your email address will not be published. Required fields are marked *