Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bootstrap responsive img but change image height and width

my header is .png image and I gave it the class="responsive-img" but the header looks big .. how to change the height and width but keep it smaller?

like image 981
Ahmed Albusaidi Avatar asked Jun 12 '14 17:06

Ahmed Albusaidi


People also ask

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

how to stretch image in size using bootstrap. you have to set the width of image to 100%, for that you can use Bootstrap class "w-100". keep in mind that "container-fluid" and "col-12" class sets left and right padding to 15px and "row" class sets left and right margin to "-15px" by default. make sure to set them to 0.

How do I set the width and height of an image 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.

How do I scale an image in Bootstrap?

To make an image responsive in Bootstrap, add a class . img-responsive to the <img> tag. This class applies max-width: 100%; and height: auto; to the image so that it scales nicely to the parent element.

How do I fit a container image in Bootstrap?

The OTHER answer is the best one, simply adding class="img-responsive" to the img tag does the trick! better from width:100%; is max-width:100%; and better all them is class img-responsive in BS3 or img-fluid in BS4.


1 Answers

Bootstrap 4 & 5

Since Bootstrap 4, a few new classes regarding images have been introduced.

Responsive images are now .img-fluid

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.

example:

<img src="..." class="img-fluid" alt="Responsive image">

What does img-fluid do?

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

Images used as thumbnails are .img-thumbnail


Pre-bootstrap 4

First off it's class="img-responsive" and not class="responsive-img"

That class itself won't get you far since it only does this:

.img-responsive{
  display: block;
  max-width: 100%;
  height: auto;
}

I recommend you overwrite the class and add the specifics of what you want. If you only want to change the height and width (and keep it responsive and smaller), here is a basic example:

.img-responsive {
  max-width: 90%; /* or to whatever you want here */
  max-height: auto; /* or to whatever you want here */
}

Touching the height will deform your responsive image (unless that is what you want), so I recommend you play with the max-width. Try adding a min-width as well.

Media Queries

Media queries are useful when you want to modify your site or app depending on a device's general type (such as print vs. screen) or specific characteristics and parameters (such as screen resolution or browser viewport width).

This essentially allows you to adapt your images on various devices, however there may be an overwhelming amount and picking general cases may be preferred.

/* target devices with small screens usually mobile devices */
@media screen and (max-width: 479px) {
  image {
    margin: 0;
  }
}
like image 134
kemicofa ghost Avatar answered Sep 28 '22 22:09

kemicofa ghost