I know you can do this:
<img style="position: absolute;" src="test.png" alt="Test image" width="50" height="50" />
I don't use this first method, because I know external stylesheets are meant to seperate the CSS from the HTML code. And I like to keep it that way.
<img id="foobar" src="test.png" alt="Test image" width="50" height="50" />
Sometimes I use this method, but when I look at some of the professional HTML coding of big sites like Facebook, Instagram or Twitter I see that they use a lot of container div
s, which makes me unsure whether I'm doing it right or not.
<div id="foobar">
<img src="test.png" alt="Test image" width="50" height="50" /> //use 'src' in place of 'sc'
</div>
I found that I mostly use this method for some reason I actually don't really know. But in this case I just add styling to the div
and not directly to the img
. Or when I do, I directly add styling to the img
element by selecting it with #foobar img{ ... }
in the CSS.
<div id="foo">
<img id="bar" src="test.png" alt="Test image" width="50" height="50" />
</div>
Usually I do it this way if a container is just necessary to get the job done, where I would have some styling on the img
and some on the div#foo
element.
I know there probably are more ways, but it's mainly these last two methods I'm not too sure about when to use them. I know there are other HTML elements out there but I just took a div
and img
for demonstration.
With that being said, I would like to know what are the pros and cons of each and which method should be a good practise?