ThanhLam112358
ThanhLam112358

Reputation: 914

Responsive image in Bootstrap 4

I have a problem with responsive image in Bootstrap 4.0. I use class="img-fluid" but it seems not work.

I use add-on on Chrome to check with another device (iPhone 4) and this image not scale with device monitor.

enter image description here

Here is my code:

<!doctype html>
    <html lang="en">
    <head>
    <title>Màn hình danh sách điện thoại</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    </head>
    <body>
    <div class="container-fluid">
        <div class="row border border-primary mt-1 ml-1 mr-1 rounded">
            <img src="img/PET.png" style="width: 8rem" class="img-fluid float-left pb-2">
            <div class="col-md-6 text-center text-danger">
                 <h5 class="font-weight-bold">CỬA HÀNG ĐIỆN THOẠI PET</h5>
                 <h5>Địa chỉ: 42 Phạm Ngọc Thạch Phường 6 Quận 3 TP.HCM</h5>
                 <h5>Điện thoại: 0907.888.511</h5>
            </div>
            <div class="col-md-6">      
            </div>
        </div>
    </div>
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  </body>
</html>

I want this image to be like this:

enter image description here

Upvotes: 0

Views: 1921

Answers (3)

samaspin
samaspin

Reputation: 2402

Wrap the image in an extra div INSIDE the .row div, like this :

<div class="container">
    <div class="row">
        <div class="col">
            <img class="img-fluid" src="my-image.jpg" />
        </div>
    </div>
</div>

note: the .col class is not required but I found it made things align the way I would expect when using containers and rows

Upvotes: 1

nourza
nourza

Reputation: 2321

Try this in the css

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

Upvotes: 0

לבני מלכה
לבני מלכה

Reputation: 16251

Wrap img tag in div and set class="img-fluid float-left pb-2" there instead of in img.

And use col-6 to second div.

See fiddle:https://jsfiddle.net/3f5ke0c2/

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<div class="container-fluid">
    <div class="row border border-primary mt-1 ml-1 mr-1 rounded">
    <div class="img-fluid float-left pb-2">
        <img src="https://material.angular.io/assets/img/examples/shiba1.jpg"
        style="width: 8rem">
    </div>
        <div class="col-6 text-center text-danger">
             <h5 class="font-weight-bold">CỬA HÀNG ĐIỆN THOẠI PET</h5>
             <h5>Địa chỉ: 42 Phạm Ngọc Thạch Phường 6 Quận 3 TP.HCM</h5>
             <h5>Điện thoại: 0907.888.511</h5>
        </div>
        <div class="col-md-6">      
        </div>
    </div>
</div>

Upvotes: 0

Related Questions