1

Need to fit an image inside div without making image as background image of the div

My code as follows

<div style="display:none" id="previewImage" height="300px" width="300px">
    <img src="" id="previewImg" height="400px" width="450px">
</div>
<div>
    <input type='file' class="inputfile" name='file' id='file' size='50' accept='image/*' />
    <input type='submit' value='Upload' name='Upload image'/>
</div> 
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77
Nick
  • 51
  • 1
  • 8

2 Answers2

3

function readURL(input) {
  if (input.files && input.files[0]) {
    var reader = new FileReader();

    reader.onload = function(e) {
      $('#passport_photo')
        .attr('src', e.target.result)
        .width(150)
        .height('auto');
    };

    reader.readAsDataURL(input.files[0]);
  }
}
$("#imgpath").change(function() {
  readURL(this);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" id="imgpath" name="file" accept="image/*">

<div class=" ffset-2" >
  <img id="passport_photo" src="#" alt="image" style="width:150px;height:auto;">
</div>
Himesh Suthar
  • 562
  • 4
  • 14
0

Basically :

width:100%;
height:auto;

https://jsfiddle.net/3scdng5h/

dc-p8
  • 784
  • 1
  • 6
  • 19