Faux Crop any size image with CSS

Here’s a quick tip on how to crop any sized image through CSS and an extra div. This technique comes in very handy when building websites for clients who don’t have Photoshop, or understand what 360px x 240px means. In an ideal world every image is edited and cropped before it’s uploaded to the site, so that it fits perfectly where it’s supposed to. Unfortunately that’s not how it works and many sites end up with squished, stretched, or highly pixellated images.

The key to this technique is to wrap the image you want to display in a container div, which we’ll give a specific height and width. We’ll then give the image a width of 100% just in case the user uses an image that’s too small. Here’s the code:

The html:

<div class="container">
	<img src="http://your-source.com" alt="alt" />
</div>

And the CSS:

.container {
	width: 418px;
	height: 240px;
	overflow: hidden;
}

.container img {
	width: 100%;
}

The real key there is the overflow: hidden; part. It prevents any image that is larger than the crop area from being displayed. Adding width: 100%; is only helpful when the user uploads an image smaller than the are you want cropped. The image will be enlarged to fit the area – which may result in some pixellation – so if crisp images outweigh the need for them to fit correctly, just skip that part.

Happy cropping! And leave a comment.

29 Comments

  1. Keiko on February 6, 2012 at 8:39 am

    Thanks for the tiaorutl! I’m almost there, but for some reason my images open up in a new window, and do not replace the other image. Please help! Thanks!

  2. Dani on April 16, 2012 at 12:06 am

    THANK YOU. I was looking for something clean and simple for a photo gallery and this is it! Thank you so much!

  3. Rob on June 13, 2012 at 3:07 pm

    You da man!

  4. Mike on August 19, 2012 at 11:29 am

    Thank you so, so much! Such an easy and elegant fix for something that has been driving me mad for the last 4 hours!

    Honestly, people, like yourself who post things like this, really are exceptionally wonderful human beings! 🙂

  5. Arun Thota on September 6, 2012 at 2:28 am

    Excellent technique!

  6. Artemisia Moltabocca (@moltabocca) on November 4, 2012 at 1:04 pm

    Haha! Yes! I have a gallery of images that includes horizontal and vertical images. I wanted a way to crop down the horizontal ones down to a consistent size without having to fire up Photoshop. Now I can delete that annoying \”Gallery Images\” album. I can now crop the actual image. Thanks so much! happydance
    http://costumingdiary.blogspot.com/p/costuming-tutorials.html

  7. Queenie on December 30, 2012 at 9:24 pm

    Thanks so much.
    This is what i was looking for! 🙂

  8. Hemali on January 28, 2013 at 12:18 am

    It Crop image from bottom side. i need to crop image from the top and bottom any idea..?

    • megadr01d on December 31, 2013 at 6:26 pm


      .container {
      width: 418px;
      height: 300px;
      overflow: hidden;
      }
      .container img {
      width: 100%;
      margin-top: -100px; /* -100px will crop from the top so change this value until it\'s around the middle of the image */
      }

    • megadr01d on December 31, 2013 at 6:27 pm

      Or check this example: http://dabblet.com/gist/4711695

  9. Bryan Hoffman on January 28, 2013 at 11:06 am

    @Hemali That\’s not really possible with this technique, and not what it\’s intended for. If you need a specific crop a photo editor is your best bet.

  10. John on February 19, 2013 at 2:07 pm

    Helpful post. Thanks Bryan!

  11. Paul on February 26, 2013 at 12:50 am

    Thanks this was a great tip and I used on a site to do exactly that, now the image stays in the box even though it\’s bigger than the box size – perfect!

  12. Timbo on February 28, 2013 at 8:31 pm

    Awesome, just what I needed. Though like Hemali, I would like to crop the top off too.

    I not an expert, but this seemed to work:

    .container img { width: 100%; position: relative; top: -15px;}

    The above appears to crop the top 15px off the image.

    -Timbo

  13. Bryan Hoffman on March 1, 2013 at 9:00 am

    @Timbo,

    That will certainly work if you want every image to be bumped up 15px. If it\’s just for one image or a set of images, I\’d create a special class and add the positioning to that.

  14. Mos on June 27, 2013 at 5:58 am

    Thank you bro!!! really helped me out with a clean photogallery!

  15. Andrew on February 10, 2014 at 4:25 pm

    NICE!!! In practice I found that this code only works if all of the images are taller than they are wide. I had to add some server code to determine whether the width was greater than the height and then apply the width:100% (or height:100%) as an inline style.

    Am I missing something?

    • Bryan Hoffman on February 11, 2014 at 9:17 am

      Hi Andrew. I\’m not sure what the end goal for your site is but this technique is meant to crop an image to fill a defined area. I wrote it quite a while ago, and while it still gets some a few hits, there are perhaps better ways to manage images these days – Especially when designing responsively or fluidly.

  16. Vanessa on November 9, 2014 at 4:14 pm

    Thanks a ton!

  17. Maqsood Ahmed on January 20, 2015 at 3:02 am

    I have use this tip but that\’s only work for resize not for crop (not reduce size in kb)

    • tsiv on February 9, 2015 at 9:13 am

      It\’s only to move the image inside of a DIV. The image is not altered, only how much image is visible.

  18. Sijo on June 13, 2015 at 4:40 am

    Is it Possible to create full-width image using faux crop. Skewing issues occured..Please help to solve

    • Bryan Hoffman on June 15, 2015 at 2:29 pm

      I wouldn\’t use this technique to display a full width image. Might be best to use it as a background-image and set the background-size as such:

      [css]
      background-size: cover
      [/css]

  19. Newbie Coder on September 17, 2015 at 2:25 pm

    Thanks a lot for this info.. you saved my stressful day… ^_^

  20. Salvatore on November 2, 2015 at 12:04 pm

    Just what I needed to know for a website, thanks so much 🙂

  21. Pham Viet Phu on January 6, 2016 at 1:41 am

    Very simple

  22. webchrea on January 6, 2017 at 7:09 am

    THANKS Glad you found it helpful

  23. Tom on March 10, 2019 at 4:45 am

    Thanks for sharing this post,
    is very helpful article.

Leave a Reply Cancel Reply