28

Responsive Images in Bootstrap 4, with class .img-fluid are resized disproportionally. They keep it's height even width is shrinking down correctly. That is deforming whole image. Is there any way how to make it as smooth as it was in Bootstrap 3 with class .img-responsive? Thanks

<div class="col-sm-6">
    <img class="img-fluid" src="/assets/blogs/14853991244821.jpg" alt="">
</div>

By the way it is same in Bootstrap 4 documentation (https://v4-alpha.getbootstrap.com/content/images/) where sample svg image keeps height 250 pixels regardless of it's width.

Igor Mizak
  • 1,088
  • 1
  • 11
  • 19

10 Answers10

49

The problem was I wrapped image in div with class .row which have display property set to flex. once removed this class from parent div image working as expected

Igor Mizak
  • 1,088
  • 1
  • 11
  • 19
9

Actually height:auto did not work for me, but height:100% worked like a champ.

Bugs
  • 4,491
  • 9
  • 32
  • 41
bwk
  • 135
  • 2
  • 8
8

Try wrapping your image in a figure element.

<div class="col-sm-6">
    <figure>
        <img class="img-fluid" src="/assets/blogs/14853991244821.jpg" alt="">
    </figure>
</div>
Bruce.Norton
  • 171
  • 1
  • 10
  • @RicardoVigatti Make sure the columns are inside a row. See a codepen example: https://codepen.io/brucenorton/pen/pqZyOe – Bruce.Norton Jan 08 '19 at 21:23
8

<div class="row">
<picture>
    <img src="images/nameimage.png" class="img-fluid" alt="img">
</picture>
</div>

Hi, I managed to solve by placing the tag picture before the image.

Regards,

Mário

Mario Tulio
  • 81
  • 1
  • 1
5

My magic hax

<style>
    figure {
        margin: 16px 40px !important; 
    }
    .img-fluid {
      max-width: 100%;
      height: auto;
    }
</style>
<div class="row align-items-center">
    <div class="col-lg-2">
       <figure>
           <img class="img-fluid" src="path-image.png" />
       </figure
    </div>
</div>
SiD quakers
  • 63
  • 1
  • 4
  • @SteveBuzonas my native language is not English, so I can not explain correctly :) But this will help to solve the problem, I hope :) – SiD quakers Mar 08 '18 at 10:30
3

We have an (asp.net core 2.2) app (public portal) with Bootstrap 4.x, whereby the customer can upload his own images and use the img-fluid on various views (with different sizes) in flex.
The images are scaled (resized) automatically by the upload to the size how the images are showed to the end users ("query view").
On the editing view, we have to show the images with correct aspect ratio but smaller.
The end user view is optimized to show the images in landscape ( 1 ) format, but the customer also is able to upload images in portrait ( 2 ) format (the images then have to be scaled to a given height) what is not a problem on the end user view, as the images are scaled automatically correctly by the upload, but in the (smaller) editing view, it definitely is a problem...

Standard behaviour

Per default, the .img-fluid class applies max-width: 100%; and height: auto; to the image: In my case (I set the width), this leads to a scale of a portrait image to the full width ( 2 ) , what shows the image
way to high ( 3 ).
I want to scale the height to to a given height ( 4 ) (in the example it is 185 Pixel).

Principally, I would be able to overwrite the max-height to the img-fluid to have the needed hight:

 img-fluid {
  max-height: 185px;
}

But this would be a really bad idea, as it would affect all images on all views.

The only solution, that works for my case is:
to overwrite the max-height to 100%:

.img-fluid {
  max-height: 100%;
}

and set the height to the container control explicitly to the needed height:

<div id="WirUeberUnsFotoUpload" style="height:185px">
   <div> --- Noch kein Foto  hochgeladen --- </div>
</div> 

Note: The Text to the div „ --- Noch kein Foto hochgeladen --- „ is replaced by the uploaded image via JQuery at runtime.

This leads to the wanted result ( 5 ) :
Result after change

This way, the image ( 5 ) is scaled correct to the needed height ( 4 ) and don’t affect the other views.
Note: Both, to set the height to the container and to set max-height: 100% is needed.

FredyWenger
  • 2,236
  • 2
  • 32
  • 36
2

Try adding .h-100 class to the img element. This forces the image's height to match the parent div.

<div class="col-sm-6">
    <img class="img-fluid h-100" src="/assets/blogs/14853991244821.jpg" alt="">
</div>
Armaan S
  • 73
  • 5
1

just logged in to share this super quick, and hopefully minimal changing fix

in you style, simply set min-height to 1px or 1rem

<head>
    ...
    <style>
    .ieFix
    {
        min-height:1px;
    }
    </style>
</head>
<body>
<div class="card text-center">
    <img src="..." class="card-img-top ieFix" alt="...">
        <div class="card-body">
            <h5 class="card-title">...</h5>
            <p class="card-text">...</p>
            <p class="card-text post-meta"...</p>
        </div>
</div>
</body>

Have confirmed this works and nothing changes on Chrome in mobile phone and tablet screens.

jordan
  • 3,436
  • 11
  • 44
  • 75
0

If you set the css height property to auto then the height will also be fluid when resizing.

gotomanners
  • 7,808
  • 1
  • 24
  • 39
  • 1
    this is Bootstrap 4 css rule applied to img-fluid class: .img-fluid { max-width: 100%; height: auto; } – Igor Mizak Jan 26 '17 at 11:31
  • @IgorMizak Exactly! If you inspect the bootstrap documentation example,you'll see it explicitly sets a `height: 250px`. Thats why it stays fixed, so If you remove it you'll see the image height changing when you resize. – gotomanners Jan 26 '17 at 11:40
  • yes just noticed element property height is set to 250px but i dont have set height anywhere. and image is deformed. Actually I found what is the problem :) Will answer myself – Igor Mizak Jan 26 '17 at 11:55
0

This worked for me:

img {

    height: 75% !important;  
}

Or you can also use max-height:

img {
        max-height:75%;
}