Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center and crop an image to always appear in square shape with CSS?

Tags:

html

css

I need to always crop a random-sized image to a square 160x160 using only CSS. The images should stay centered when cropped.

My markup should be:

<a href="#" class="cropper">    <img src="image" alt="description" /> </a> 

Searching on StackOverflow I've found some answers (such as CSS - How to crop an image to a square, if it's already square then resize it), but they don't work for cases where your image can be larger horizontal (wide) OR vertical (tall).

Specifically, I need to be able to present both a wide image like this:

wide image

and a tall image like this:

tall image

in square form, without knowing in advance which one is a horizontal rectangle or a vertical rectangle. It should also support already squared images.

like image 413
PurpleFoxy Avatar asked Sep 07 '13 13:09

PurpleFoxy


People also ask

How do I crop a picture to make it square?

To Select a Square to CropClick the shape (or the arrow beneath Shapes) and choose the rectangle. 2. Hold down your shift key and use your mouse to draw the size square you need. 3.

How do you centralize an image in CSS?

To center an image, we have to set the value of margin-left and margin-right to auto and make it a block element by using the display: block; property. If the image is in the div element, then we can use the text-align: center; property for aligning the image to center in the div.


2 Answers

jsFiddle Demo

div {     width: 250px;     height: 250px;     overflow: hidden;     margin: 10px;     position: relative; } img {     position: absolute;     left: -1000%;     right: -1000%;     top: -1000%;     bottom: -1000%;     margin: auto;     min-height: 100%;     min-width: 100%; }
<div>     <img src="https://i.postimg.cc/TwFrQXrP/plus-2.jpg" /> </div>

A note regarding sizes

As Salman A mentioned in the comments, we need to set the img's position coordinates (top, left, bottom, right) to work with percents higher than the image's actual dimensions. I use 1000% in the above example, but of course you can adjust it according to your needs.

Before and after the fix

* Further explanation: When we set the img's left and right (or: top and bottom) coordinates to be -100% (of the containing div), the overall allowed width (or: height) of the img, can be at most 300% of the containing div's width (or: height), because it's the sum of the div's width (or: height) and the left and right (or: top and bottom) coordinates.

like image 186
Itay Avatar answered Sep 29 '22 10:09

Itay


object-fit property does the magic. On JsFiddle.

CSS

.image {   width: 160px;   height: 160px; }  .object-fit_fill {   object-fit: fill }  .object-fit_contain {   object-fit: contain }  .object-fit_cover {   object-fit: cover }  .object-fit_none {   object-fit: none }  .object-fit_scale-down {   object-fit: scale-down } 

HTML

<div class="original-image">   <p>original image</p>   <img src="http://lorempixel.com/500/200"> </div>  <div class="image">   <p>object-fit: fill</p>   <img class="object-fit_fill" src="http://lorempixel.com/500/200"> </div>  <div class="image">   <p>object-fit: contain</p>   <img class="object-fit_contain" src="http://lorempixel.com/500/200"> </div>  <div class="image">   <p>object-fit: cover</p>   <img class="object-fit_cover" src="http://lorempixel.com/500/200"> </div>  <div class="image">   <p>object-fit: none</p>   <img class="object-fit_none" src="http://lorempixel.com/500/200"> </div>  <div class="image">   <p>object-fit: scale-down</p>   <img class="object-fit_scale-down" src="http://lorempixel.com/500/200"> </div> 

Result

How the rendered images look (in a browser that supports <code>object-fit</code>)

like image 36
Turdaliev Nursultan Avatar answered Sep 29 '22 12:09

Turdaliev Nursultan