Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cropping the image to aspect ratio in bootstrap responsive grid

Problem: I am trying to place images in the responsive grid system of the Twitter's Bootstrap, so they will be scaled into the given space (for responsive CSS size of the column will differ for different devices). I want them to fit into the aspect ratio -- lets assume it is a square.

Standard CSS cropping technique do not work if I want the images to be rescaled by Bootstrap to fit the grid design. Negative margins works only for cutting top and bottom part -- when the height of the image has to be cut, and the width is left unchanged (e.g. to put portrait image into the square).

I have found that this technique for specifying aspect ratio to cut the width leaving the height unchanged (putting landscape image into the square) works well with Bootstrap's scaling.

The example (jsfiddle) of the cropped images for size 1600x400 and 400x1600. So far I checked it in current Chrome and Safari. Edit: Fixed to also work in Firefox.

Question: How can I do it in one way for both: cutting the width and the height. How to do arbitrary crop on an image, choosing the rectangle, to be properly rescaled by Bootstrap? How can I crop to some aspect ratio without the prior knowledge of the size of the image?

Thank you for any ideas!

like image 756
Marcin Skórzewski Avatar asked Oct 02 '12 17:10

Marcin Skórzewski


People also ask

How do I change the size of an image in Bootstrap?

Responsive images 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.

What is a 2 3 aspect ratio?

As another example, a portrait-style image might have a ratio of 2:3. With this aspect ratio, the height is 1.5 times longer than the width. So the image could be 500px × 750px, 1500px × 2250px, etc.

What is Bootstrap aspect ratio?

Aspect ratios can be customized with modifier classes. By default the following ratio classes are provided: 1x1. 4x3. 16x9.

Which of the following aspect ratio is used with responsive video in Bootstrap?

4:3 aspect ratio.


1 Answers

If your solution only has to work in modern browsers and the img-tag isn't mandatory, using images as background has helped me out often in responsive designs. You can easily position backgrounds in the center and make them fit their container.

http://jsfiddle.net/willemvb/wAqSV/2/

like image 153
Willem Van Bockstal Avatar answered Oct 04 '22 20:10

Willem Van Bockstal