So after a long time of searching, I finally found out how to crop an image without distorting/squashing an image using overflow: hidden;
.
Now my next problem; How would I have the image show a part I want, meaning, when using the overflow:hidden
it shows the image from the top of it rather than the middle or bottom. How can I adjust that and show the image from the bottom or middle? To help give a better understanding, please view the images below which I created in photoshop. Image description in order: default image, what css does in default with overflow: hidden, what I want (middle pov), what I want (bottom pov).
Thanks.
Edit: My layout is: parent div with the image div as the child. Parent div’s height defined at 600px and width at 100%. And height and width of image div defined as 100%.
3
Answers
Here is my answer/solution for anyone that comes across this post.
In which way are you using this image?
If you’re using this as a background image the solution is much simpler and would simply involve using background positioning. If you’re using this as an image pulled in using an img tag you can try the below to manipulate the image.
Be aware that this won’t work on every browser.
Assuming your desired width/height and
overflow: hidden
is applied to an outer containing div, you can add something like:This would move the displayed area of the image down 50% of the container height (
top: 50%
), then back up 50% of the image height (transform: translateY(-50%)
), which ends up centering it inside the container.You can adjust these values to achieve different positioning, or add in
left:
andtransform: translateX()
to adjust the horizontal axis.