Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML - Bootstrap - How to crop the center part of an image

I'm creating a website and I want to crop an image to a square image based on the orientation of the image. I'm not really familiar with CSS so I'm not sure how to tackle this problem.

If it's a landscape image (800x500) then I want the cropped image to be like this (500x500):

Cropping 800x500 image

If it's portrait (400x600) then it should crop out this part (400x400) of the image:

Cropping 400x600 image

How should I go about to achieve this? Preferably a method I can use along with Bootstrap's "img-responsive" so I can resize the cropped when needed.

Thanks,

like image 570
FljpFl0p Avatar asked Feb 11 '15 17:02

FljpFl0p


People also ask

How do I center an image in Bootstrap 4?

By default, images are display:inline . If you only want the center the image (and not the other column content), make the image display:block using the d-block class, and then mx-auto will work.

How do I crop the top of an image in CSS?

Option Two: img with a Twist As you can see, the bottom of the image now gets cropped as it widens. But what if you want it to crop from the top? Surprisingly, you can — using CSS3's transform:rotate() we add a “flip” class to both img. scale and div.

How to crop and center an image in CSS?

There are many ways of cropping and centering an image in CSS. For that, you can use the background-image and object-fit properties, or the HTML <img> tag. First, we’re going to show and explain an example where we use the background-image property. Use a <div> with the class name "center-cropped". Specify the width and height properties.

How to center-align images in Bootstrap UI?

If you want an image to be center-aligned for bootstrap UI, you can make use of the class center-block If you have a look at the center-block class in bootstrap.css then you may find, .center-block { display: block; margin-right: auto; margin-left: auto; }

How to center-cropped image with no repeat in HTML?

Use a <div> with the class name "center-cropped". Specify the width and height properties. Add the background-image property and set the background-position to "center center". Add the background-repeat property with the "no-repeat" value. Let’s see the final code.

How to make an image responsive in Bootstrap 4?

If you want it responsive and using bootstrap 4. Try this one: .thumb-post img { object-fit: none; /* Do not scale the image */ object-position: center; /* Center the image within the element */ width: 100%; max-height: 250px; margin-bottom: 1rem; } Add class img-responsive to img tag, which will scale your image and keep width / height ratio.


1 Answers

@FljpFl0p fix it [jsfiddle][1]

[1]: https://www.bootply.com/92024 and there is another answer here How to automatically crop and center an image. Tks!

If you want it responsive and using bootstrap 4. Try this one:

CSS

.thumb-post img {
  object-fit: none; /* Do not scale the image */
  object-position: center; /* Center the image within the element */
  width: 100%;
  max-height: 250px;
  margin-bottom: 1rem;
}
like image 132
Állan Fidelis Toledo Avatar answered Oct 04 '22 16:10

Állan Fidelis Toledo