Making an image take the height of the div it is inside of
Problem
I want the image on the right (yellow outline) to start shrinking down if the red outline div height shrinks.
Currently the yellow outline image height is taking priority, but I want the red outline div to take priority so the image inside it shrinks.
Example videos provided of the issue happening when the image is present and also how the element behaves when the image is not present.
I would want the element to behave just how it does when the image is not present, but to have the image still there hugging the right corner, taking up just the space that it requires.
##What I've tried
I've tried using various
display:flex
and display:inline-block
variations along with width:100%
, but I haven't ran into a combination that would work right.13 Replies
Code
HTML
CSS
Bump
have you tried the latest reset that kevin released on youtube?
and, possibly, starting over on the image sizes and styles?
I'm unsure about your exact requirements because I cant see the codepen or any example to actually play with but this should do it
If you want anything more specific you are going to have to set some code up somewhere
don't forget to reset the height to
auto
or the image will stretch without keeping it's aspect ratioit shouldnt ever take up more than 10% of the available width.
As I said without a working example there isnt any way to ensure that an edge case wont cause problems.
I have 0 idea if they have a max width on this banner
if this is supposed to be reusable.
what the banner is going to be rendered like when the video fails to load
if they are reserving space anywhere
etc. etc.
yes, but it is still a good idea to set it
and also set a size in the image itself (just in case the css didn't load yet when the image is ready)
do you want to reserve that space if the image doesnt load though?
yes, otherwise the image explodes in size and things look really weird
(and lighthouse complains about it)
what I am saying though is that you probably dont want a random space to be appearing on the banner if there is no image
a minor impact on lighthouse will end with a better UX
well u have alt text to let the user know
Thanks guys, with the help of Carl, I just gave the
img
tag inside the vidOverlayImg
class a width of 100% and it did the trick.
Also a margin-right of 10px is added in the video to the vidOverlayText
class.
I'm fine with how this performs, even if the image doesn't load for the user.ah ,thought you wanted the image to get smaller 🤦 I must have misunderstood
also, there is progressive loading
does it work the same if you change it to max-width?