15

How to target a specific location on the image to be cropped using css or javascript, simple way without big scripts,

Picture before :
enter image description here


I want the highlighted location on the following image to be viewed :

enter image description here

Not the exact highlighted though, just trying to explain it doesnt has to be from the very top, i want to select specific image scales, AND how to resize is after cropping ?

4
  • You can resize it by changing width and height of the container Commented Oct 8, 2012 at 21:45
  • @adzaz that actually ruins the position Commented Oct 8, 2012 at 21:49
  • 1
    Unfortunately that's how it works, every time you change the width and height, you'll have to adjust the position. Commented Oct 8, 2012 at 21:52
  • You can crop images in the brwoser side with javascript, now. Checkout stackoverflow.com/questions/12728188/… Commented Mar 29, 2017 at 13:57

2 Answers 2

19

Update 2022-05-27: A new property object-view-box will soon make this a lot simpler: https://ishadeed.com/article/css-object-view-box/


One approach is to use an element with overflow: hidden that has the image as a child, which itself is absolutely positioned within the context of the original element. The result being, the size of the overflow: hidden element masks the image.

Here's an example of the approach:

HTML

<div id='crop-the-cats'>
    <img src='https://i.sstatic.net/ArS4Q.jpg'>
</div>​

CSS

#crop-the-cats {
    width: 100px;
    height: 80px;
    overflow:hidden;   
    position:relative;
}

#crop-the-cats img {
    position: absolute;
    top: -60px;
    left: -70px;
}

​See http://jsfiddle.net/Da9CT/

Another approach is to use the image as the background of the image and reposition it using background-position:

HTML

<div id='crop-the-cats'></div>​

CSS

#crop-the-cats {
    width: 100px;
    height: 80px;
    background-image: url(https://i.sstatic.net/ArS4Q.jpg);
    background-position: -50px -60px;
}

​See http://jsfiddle.net/Da9CT/2/

Sign up to request clarification or add additional context in comments.

3 Comments

well thats a great answer 1++, How can i resize the image size after doing this ?
@Osa If you mean the container, just change the size of #crop-the-cats: jsfiddle.net/Da9CT/4. If you mean the cropped image itself, simply apply width and/or height rules to #crop-the-cats img: jsfiddle.net/Da9CT/3
You can also position the image without using position: absolute by using margin-top and margin-left instead of top and left.
5

You can't crop image using javascript / css but you can position it inside an element with overflow hidden: http://jsbin.com/ebenem/1/edit

Let me know if that helps!

1 Comment

Yes you can crop image using javascript, now. Checkout: stackoverflow.com/questions/12728188/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.