<img> vs <image> tag in HTML
Yes and no. As you point out <image>
has been a synonym for <img>
for a long time. I believe it was an early Netscape browser that first did this, possibly to compensate for user error, or possibly because there was dispute at the time whether the element should actually be called <image>
or <img>
.
Anyway, as pst points out, once it was implemented in a browser that dominated the market of the time, web pages came to rely on it. Its persistence is then down to commercial pressure on the browser manufacturers. If all the major browsers support it, then Browser A decides that although it supported it in Version V, it won't support it in version V+1, as soon as version V+1 is released, they get lots of messages saying "Site S is broken in your latest browser. You browser is rubbish. I'm going to switch to browser B".
The HTML5 parsing spec requires that the <image>
tag is mapped to the img
element at the tree construction stage, so there can never be any justification for using it.
I would be less concerned about browsers, than other HTML consumers, such as the lesser known search engines. I believe that the image
for img
synonym is not widely known, and the many such tools would therefore fail to pick up <image>
as referencing an image resource.
html tag <img> and <image>
See MDN:
The obsolete HTML Image element (
<image>
) is an obsolete remnant of an ancient version of HTML lost in the mists of time; use the standard<img>
element instead. Seriously, the specification even literally uses the words "Don't ask" when describing this element.
When to use IMG vs. CSS background-image?
Proper uses of IMG
- Use
IMG
if you intend to have
people print your page and you want the image to be included by default.
—JayTee - Use
IMG
(withalt
text) when the image has an important semantic meaning, such as a warning icon. This ensures that the meaning of the image can be communicated in all user-agents, including screen readers.
Pragmatic uses of IMG
- Use
IMG
plus alt attribute if the image
is part of the content such as a logo or diagram or person (real person, not stock photo people).
—sanchothefat - Use
IMG
if you rely on browser scaling to render an image in proportion to text size. - Use
IMG
for multiple overlay images in IE6. UseIMG
with az-index
in order
to stretch a background image to fill its entire window.
Note, this is no longer true with CSS3 background-size; see #6 below.- Using
img
instead ofbackground-image
can dramatically improve performance of animations over a background.
When to use CSS background-image
- Use CSS background images if the
image is not part of the content.
—sanchothefat - Use CSS background images when
doing image-replacement of text eg. paragraphs/headers.
—sanchothefat - Use
background-image
if you intend to have
people print your page and you do not want the image to be included by default.
—JayTee - Use
background-image
if you need to improve download times, as
with CSS sprites. - Use
background-image
if you need for only a portion of the image to be visible, as with CSS sprites. - Use
background-image
withbackground-size:cover
in order to stretch a background image to fill its entire window.
What is the difference between using img tag src attribute in HTML to display image and styling it in CSS?
Let us see by comparison.
It isn't enough to simply supply a background-image
property, because the img
element has no inherent height
or width
and the background-image
property can't provide that automatically in the same way that setting the src
attribute does. So, without declared dimensions, the CSS-applied image doesn't show.
#my-pic { background-image: url(//placehold.it/150);}
<img src="//placehold.it/150" /><img id="my-pic" src="" />
Do I need a / at the end of an <img> or <br> tag, etc.?
The /
is only required for XHTML & XML.
If you're using a HTML5 doctype, then there's no need to terminate self-closing tags in this way.
This applies to <img src="img.png" />
, <br />
, <hr />
etc.
I.e. Just use <img src="img.png">
, <br>
and <hr>
.
If you need an empty element (like a div
), don't use <div />
, instead use <div></div>
. This is important since in HTML5, the slash is ignored and <div />
is interpreted as <div>
without a closing tag.
What is the difference between <img> tag and new Image().src?
You can create DOM nodes mainly in two ways:
- You put them in the HTML using tags like
<img src="foo.jpg">
- You create them dynamically from Javascript using for example
document.createElement
If the nodes are fixed then static creation is better because allows a designer to create and edit a page directly without having to know Javascript. If however they're created dynamically depending on the user actions on the page then you cannot place them in the HTML and you're forced to use Javascript.
Somethings you may need to just act on an existing DOM element and you may see code like
<img id="pic" src="foo.jpg">
in the HTML (note the id=
part)
and then you can access it from code using for example
document.getElementById("pic").src = "bar.jpg";
Is it better to use an IMG element for a header logo or a background image, why?
Why do this instead of an img element with alt text? Is there some semantic reason? Some accessibility reason? SEO reason?
It's useful when you want to use CSS media queries and display a different logo size depending on the screen resolution.
Regarding accessibility, that would be reasons like permitting users to view alternative text when images are disabled.
Related Topics
Embed Youtube Code Is Not Working in HTML
Create a Basic Mailchimp Signup Form Using Their API
How to Make a 45 Degree Responsive Ribbon with Folded Corner
How to Display Excel Sheet in HTML Page
Chrome Auto Formats Input=Number
Setting The Height of a Select in Ie
How to Position a Table at The Center of Div Horizontally & Vertically
Bootstrap 3 Grid, Does It *Really* Matter How Many Columns Are in a Row
Why Does Perspective Changes Fixed Position in CSS
A True Sticky Footer with a Fixed Header
How to Get a Flexbox to Have a Center Fixed and a Bottom Fixed Children Together
Can HTML5 Websockets Connect 2 Clients (Browsers) Directly Without Using a Server? (P2P)
CSS to Keep Element at "Fixed" Position on Screen
How to Horizontally Center an Absolute Positioned Element Inside a 100% Width Div