Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to crop an image with CSS?

Tags:

css

I want to show a photo in my page, the DIV layer is 500 * 500px. I will replace the picture very often, the picture size is not sure, may be horizontal version may be vertical version, maybe 800*600px maybe 576*720px.

I don't want to get the photo deformation. How to set CSS or JS, make the photo show only the center 500 * 500 px, hide the around part.

like image 786
Mostafa Elkady Avatar asked Dec 17 '22 20:12

Mostafa Elkady


1 Answers

Use a background image on a DIV with pre-defined dimensions, and set the image position to 50%, which essentially centers it. Whatever overflows the 500x500 will be cropped...

#yourImageDiv {
  background: url(../img/image.jpg) no-repeat 50%;
  height: 500px;
  width: 500px;
}
like image 199
Josh Stodola Avatar answered Jun 14 '23 04:06

Josh Stodola