Anyone who has used twitter bootstrap knows that an image can be made responsive using the img-responsive
class in the html img tag. However, these images take up 100% of the width of the division.
How can I make the image responsive while still keeping its original width?
3
Answers
The
.img-responsive
class appliesmax-width: 100%;
andheight: auto;
to the image, so if you want to keep its original width explicitly you have to set width of image using width attribute of img tag.You can put the image in a wrapper and give the wrapper the width you want the image to have.
HTML
CSS
The above should in theory make the
imgwrapper
80% of the width of the parent-element, and theimg-responsive
-class on the image will fill up the whole wrapper.If this doesn’t answer your question, could you explain a bit better what you mean with
keep original width, but make it responsive
? Since making it responsive will resize the image, which means it will not have the original width.You should have a wrapper around the image defining the actual size of the parent that could be used to place that image. It will be related to the parent div. Then apply .img-responsive to the image. This will cause the image to have the same width as the wrapper.
HTML
CSS
If you want to keep the original size (it will be resized to have small size but never higher), you should also add a
max-width
which will have to correspond to the image’s original size. This will overwrite the original value of 100%