Apologies if this post is redundant, but I couldn’t find the answer I’m looking for.
I’m working on the JS project “Build a Pokémon Search App Project”, and I’ve noticed that the Pokémon images have alot of empty space around them.
What I’m trying to do is have the image “container” remain in the same place and with same size relative to the HTML / CSS elements surrounding it, but for the content at the center of the image (the Pokémon, in this case) to appear bigger while cropping out some of the empty space that surrounds it in the original image. Here’s a visual example:
The image on the left is the default Pikachu provided by the fCC API for this project. The image on the right is a version of the one on the left that I manually cropped and then uploaded to my HTML project. Both have the same size (the light blue square) but the one on the right is cropped/zoomed in. How can I automatically do this with CSS (or JavaScript?) for all the images in the project?
This would resolve my issue wonderfully, but for some reason, when I add flexbox to the div in which my image is contained, the image shrinks to the size of the div, even if it was set to a width greater than 100%. This only happens when I add
Try following all the instructions given. That is:
1- make the display of the container flex and assign intrinsic width and height to the image
2- align it to center (using the justify-content: center should work and the align-items: center)
3- hide overflow on container
4- set container width and height to half of the image size
Ps. Or use a clip-path as I suggested and then scale the image to whatever size works.
Adding pixel values to the image causes further problems, as it causes the div containing the image, as well as the div containing that div, to increase in size, and since the outer div is part of a grid, the other grid items increase proportionally in size as well.
As for clip-path, it produced the same problem, wherein I have to increase the size of the image before clipping it in order to achieve the zoomed-in look I’m aiming for, and then attempting to center it with flexbox causes the size increase to magically disappear.
I should note, however, that I’ve realized that what I’m trying to do is not a good idea in the first place (at least in this case). This is because there are some Pokémon that are much bigger, and therefore have very little empty space around them, and cropping these like I’m trying to do would just cut out parts of the actual Pokémon.
Yes the img px values will do that but the overflow hidden will hide that provided everything has a fixed value. It takes some finessing to get it right.