Search code examples
htmlcssimagecontainerswrapper

Why put an <img> inside a container (like a <div>, for instance)?


I am just learning to code and have been looking everywhere for an answer on this one and for some reason cannot find anything.

I noticed that it seems to be common practice to put an image inside of a container or wrapper. For instance, rather just having:

<img src="url"/>

Everyone seems to be in agreement that it needs to be this way:

<div class="container">
<img=src"url"/>
</div>

What is the purpose of wrapping the img inside of a div in this way? It seems to have something to do with "responsive design", but I'm not 100% sure. Is it just so that we have something to size the image relative to, rather than using definite sizing like pixels on the image selector in css? The more I think about it as I write this, the more it seems to be the right answer, but I'm not sure if there's something else I'm missing on this one.

Any insight would be very much appreciated. Thank you.


Solution

  • unfortunately there is no "single" correct answer for this. There can be many reasons as to why one would wrap any element in another element, it is not specific to <img /> tags :)

    In your question I read something like this (converted to real world example):

    I see that it is common practice to put a frame around a photo.

    Where the "frame" would be the wrapper element, and the photo would be the <img />.

    Looking at it this way might make it seem more clear. The photo is the most important part, technically speaking you don't need a frame to show the photo. If you have just a photo, you won't be able to hang it on your wall without damaging it by driving a nail through the top or applying some tape. If you have a frame though, you can make that photo take up any amount of available space within it, you can use the clip to hang it on a wall and if you put multiple photo's in the frame, you can move them all at once since they are in the same frame.

    The reason most people put that image in a "container" is because they get some sort of advantage out of it over using an image alone, this could range from aspect-ratio locks to relative positioning. In some cases, a wrapper is required to achieve certain (notably more complex) animations as well.

    Websites are built out of "logical" pieces that, together, form a website. The individual pieces are all "frames" that "flow" together to create any page layout you see on every website.

    It is merely a structural way of thinking, if the purpose of that image was to be used as a background image for the entire page, a better alternative would be to use CSS background-image property on the <body> tag and not use an <img> element at all. But if the image is meant to be part of a smaller part of your website, it should probably be contained as appropriate.

    This answer is in no way a guide to go by, nor a ruleset or anything like that, they are just the thoughts of another developer. There are countless reasons for wrapping an element and this answer doesn't even cover 0.0000001% of those cases. I'm just saying -- there's no specific reason to do or don't here.