13

I can't seem to find an answer to this, although some are close.

I have an image that I want to take out the middle of (crop it), a bit like this:

enter image description here

So it's perfectly in the middle and with the same aspect ratio.

All I have managed to do, is crop an image like so:

enter image description here

..where it's connected to the edges.

So basically, I want to have a div of fixed size, with an image inside. This image needs to be zoomed in and centred, with the overflow hidden.

How can I do this?

user2397282
  • 3,798
  • 15
  • 48
  • 94
  • Have a look at this answer in the dupe link: https://stackoverflow.com/questions/493296/css-display-an-image-resized-and-cropped/48535434#48535434 – Asons Jan 31 '18 at 06:08

1 Answers1

27

HTML:

<div>
  <img src="https://picsum.photos/600/500" />
</div>

CSS:

div{
  position: relative;
  width: 300px;
  height: 300px;
  overflow: hidden;
}

img{
  position: absolute;
  top: -9999px;
  left: -9999px;
  right: -9999px;
  bottom: -9999px;
  margin: auto;
}

CODEPEN link: http://codepen.io/bra1N/pen/NArjNN

Juho Vepsäläinen
  • 26,573
  • 12
  • 79
  • 105
ristapk
  • 1,342
  • 3
  • 15
  • 27
  • Wow! How is this even possible? It works perfectly! And was the only solution that worded for me among the dozens I have checked! – FrancescoMussi Jan 22 '19 at 14:59