Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set responsive image's max width (Bootstrap 4)?

Question:

  • How do you set the max-width of an image in Bootstrap 4 while retaining the image's responsiveness?

Background:

  • Boostrap 4's .img-fluid class makes images responsive.

max-width: 100%; and height: auto; are applied to the image so that it scales with the parent element (https://getbootstrap.com/docs/4.1/content/images/)

My situation:

  • See JS Fiddle: https://jsfiddle.net/aq9Laaew/290257/
  • I have an image that is very big... too big.
  • I have set max-width: 500px (inline styling), and this sets the image's width - Awesome.

  • Problem: It does not resize / respond when you shrink the window size... It is no longer responsive.

Hope that was clear; I've tried to look for similar questions here but wasn't successful. Cheers!

like image 853
daCoda Avatar asked Dec 11 '18 10:12

daCoda


People also ask

How do I make an image max-width to responsive?

To make an image responsive, you need to give a new value to its width property. Then the height of the image will adjust itself automatically. The important thing to know is that you should always use relative units for the width property like percentage, rather than absolute ones like pixels.

Which Bootstrap 4 class is used to make images responsive?

Images in Bootstrap are made responsive with . img-fluid . max-width: 100%; and height: auto; are applied to the image so that it scales with the parent element.

How do I change the width and height of an image in Bootstrap 4?

The width and height can be set for an element, by using 25%, 50%, 75%, 100%, and auto values. For instance, use w-25 (for remaining values, replace 25 with those values) for width utility and h-25 (for remaining values, replace 25 with those values) for height utility.

Which layout in Bootstrap will provide 100% width?

Containers are the most basic layout element in Bootstrap and are required when using our default grid system. Choose from a responsive, fixed-width container (meaning its max-width changes at each breakpoint) or fluid-width (meaning it's 100% wide all the time).


2 Answers

Your answer is here

.img-max {
  max-width: 500px;
  width:100%;
}

and use the class in the image.

I have edited your fiddle. Please refer the link

jsfiddle.net/saravanan7944/ygamjz7s/1

like image 185
Saravanan Avatar answered Sep 17 '22 11:09

Saravanan


.img-max {
  max-width: 500px
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<p class="alert-primary p-3">
  I can set the max-width of the image... but then it stops being responsive.
</p>
  



<div class="container">
  <div class="img-max">
  <img  class=" mx-auto d-block mb-2 float-md-left mr-md-4 img-thumbnail"  src="https://images.pexels.com/photos/2422/sky-earth-galaxy-universe.jpg">
  </div>
  <p>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore ipsa ea dolorem mollitia repudiandae odit dolore quod et voluptatem asperiores odio quam ipsam placeat nisi quo voluptate, laborum provident earum?
  </p>
</div>
like image 41
Ashish Mehta Avatar answered Sep 19 '22 11:09

Ashish Mehta