HTML img tag: title attribute vs. alt attribute?
Asked Answered
S

12

129

I was browsing Amazon and I noticed that when searching "1TB" if you hover the mouse cursor over the stars rating image, you only see the score if using IE. If you are using another browser then the score won't show.

A rating of 3.8 and a rating of 4.2 both show up as 4 stars. Of course a 3.8 stars vs 4.2 stars (76% vs 84% score) could make a difference!

This is because the standard way of displaying alt text is only when the user turns off graphics or when the browser is "read out" (e.g browser for users who are visually impaired). IE however, shows it on hover.

So I think if Amazon is to show it regardless of the user's browser, then title should be used in addition to alt. Would you agree?

Sterne answered 16/5, 2009 at 13:0 Comment(0)
L
70

I'd go for both. Title will show a nice tooltip in all browsers and alt will give a description when browsing in a browser with no images.

That said, I'd love to see some stats of how many "surfers" out there going to a "store" to browse merchandise actually have images turned off or are using a browser that doesn't support images. I think the days where 90% of the population is using a 28k modem to connect to the InterWeb is looooong over.

Leftwich answered 16/5, 2009 at 13:6 Comment(5)
Using alt isn't just about having something to show when images are surpressed for bandwidth reasons. Some browsers designed for the visually impaired for example will use alt extensively.Delphinedelphinia
...but: sometimes images don't load; and there are non-visual browsers for the blind; speech features in safari read the page, including alt attributes from images; search engine optimization; etc. lots of good reasons not to assume 100% image display.Photoengraving
@Anthony,ferocious - agreed. There are various hmmm "edge cases" where providing alt is very helpful (and i'm not suggesting to drop it) - However if there is "meaningful info" to provide the user in the form of a tooltip, then there most certainly should be a title attribute - since that is what it is there for. When I say "edge case" above, I'm believing that the total % of users accessing with JAWS or similar is quite low compared to Firefox,Chrome,IE,Opera,Safari,Konqueror etc.Leftwich
ALT tags are SEO friendly. Should be placed same, as the titles.Monoploid
If you had a disability and required a screen reader, I'm pretty sure you wouldn't appreciate being referred to as an "edge case".Starryeyed
M
188

They are used for different things. The alt attribute is used instead of the image. If the image can't be shown, and in screen readers.

The title attribute is shown along with the image, typically as a hover tooltip.

One should not be used "instead" of the other. Each should be used properly, to do the things they were designed to do.

Makeup answered 16/5, 2009 at 13:8 Comment(6)
And note since HTML5 alt attribute is mandatory. In some countries if you do a project for a state institution it is even ILLEGAL not to use it. Whereas title as jalf stated is just a "design" thing.Congregate
And I dont belive - I know that screen readers read alt instead of image.Congregate
@Congregate The HTML5 specs suggest and indicate a lot of things about the alt attribute w3.org/html/wg/drafts/html/master/… however it does not go as far as to say it is "mandatory". Specifically when an image is purely decoration an alt attribute value is actually not expected at all.Leftwich
@Leftwich not using alt attribute in HTML5 means that your HTML5 is not valid. If the image is purely decoration - it should not be applied as an <img> tag - you should use CSS-styled div instead ;)Congregate
@Congregate you're reading more into the spec than is specified. They strongly suggest that you include an alt attribute but there are exceptions w3.org/html/wg/drafts/html/master/… and no rigid enforcement that you are required to have an alt attribute. No where in the spec does it indicate that it is "mandatory" or "required". - in fact they list several cases where there is either no alt attribute specified, or it is intentionally left blank.Leftwich
@Leftwich Well they are mixing two things in the manual - the style and the content - therefore CSS was made. "the image offers no information" = no content => CSS. Even the spec recommends so. And firstly, I was refering to their own validator which does not consider the page valid unless you specify at least an empty alt attribute.Congregate
L
70

I'd go for both. Title will show a nice tooltip in all browsers and alt will give a description when browsing in a browser with no images.

That said, I'd love to see some stats of how many "surfers" out there going to a "store" to browse merchandise actually have images turned off or are using a browser that doesn't support images. I think the days where 90% of the population is using a 28k modem to connect to the InterWeb is looooong over.

Leftwich answered 16/5, 2009 at 13:6 Comment(5)
Using alt isn't just about having something to show when images are surpressed for bandwidth reasons. Some browsers designed for the visually impaired for example will use alt extensively.Delphinedelphinia
...but: sometimes images don't load; and there are non-visual browsers for the blind; speech features in safari read the page, including alt attributes from images; search engine optimization; etc. lots of good reasons not to assume 100% image display.Photoengraving
@Anthony,ferocious - agreed. There are various hmmm "edge cases" where providing alt is very helpful (and i'm not suggesting to drop it) - However if there is "meaningful info" to provide the user in the form of a tooltip, then there most certainly should be a title attribute - since that is what it is there for. When I say "edge case" above, I'm believing that the total % of users accessing with JAWS or similar is quite low compared to Firefox,Chrome,IE,Opera,Safari,Konqueror etc.Leftwich
ALT tags are SEO friendly. Should be placed same, as the titles.Monoploid
If you had a disability and required a screen reader, I'm pretty sure you wouldn't appreciate being referred to as an "edge case".Starryeyed
G
12

alt and title are for different things, as already mentioned. While the title attribute will provide a tooltip, alt is also an important attribute, since it specifies text to be displayed if the image can't be displayed. (And in some browsers, such as firefox, you'll also see this text while the image loads)

Another point that I feel should be made is that the alt attribute is required to validate as an XHTML document, whereas the title attribute is just an "extra option," as it were.

Gymnasiarch answered 16/5, 2009 at 21:46 Comment(0)
P
7

That's because they serve different purposes and they both should be used not just one over the other.

The "alt" is for what you guys already said, so you can see what's the image it's all about if the image can't be displayed (for whatever reason), it also allows visually impaired people to understand what's the image about.

The "title" attribute is the correct one to show the tooltip with a title for the image.

Plerre answered 16/5, 2009 at 13:9 Comment(0)
M
6

In my opinion should the alt text always describe what is visible in the picture, for the case that the image is not displayed.

alt = text [CS] For user agents that cannot display images, forms, or applets, this attribute specifies alternate text. The language of the alternate text is specified by the lang attribute.

w3.org

Milburt answered 16/5, 2009 at 13:7 Comment(0)
E
4

I believe alt is required for strict XHTML compliance.

As others have noted, title is for tooltips (nice to have), alt is for accessibility. Nothing wrong with using both of them, but alt should always be there.

Envelopment answered 16/5, 2009 at 21:37 Comment(0)
M
3

ALT Attribute

The alt attribute is defined in a set of tags (namely, img, area and optionally for input and applet) to allow you to provide a text equivalent for the object.

A text equivalent brings the following benefits to your web site and its visitors in the following common situations:

  • nowadays, Web browsers are available in a very wide variety of platforms with very different capacities; some cannot display images at all or only a restricted set of type of images; some can be configured to not load images. If your code has the alt attribute set in its images, most of these browsers will display the description you gave instead of the images
  • some of your visitors cannot see images, be they blind, color-blind, low-sighted; the alt attribute is of great help for those people that can rely on it to have a good idea of what's on your page
  • search engine bots belong to the two above categories: if you want your website to be indexed as well as it deserves, use the alt attribute to make sure that they won't miss important sections of your pages.

Title Attribute

The objective of this technique is to provide context sensitive help for users as they enter data in forms by providing the help information in a title attribute. The help may include format information or examples of input.

Example 1: A pulldown menu that limits the scope of a search
A search form uses a pulldown menu to limit the scope of the search. The pulldown menu is immediately adjacent to the text field used to enter the search term. The relationship between the search field and the pulldown menu is clear to users who can see the visual design, which does not have room for a visible label. The title attribute is used to identify the select menu. The title attribute can be spoken by screen readers or displayed as a tool tip for people using screen magnifiers.

<label for="searchTerm">Search for:</label>
<input id="searchTerm" type="text" size="30" value="" name="searchTerm">
<select title="Search in" id="scope">
    ...
</select> 

Example 2: Input fields for a phone number
A Web page contains controls for entering a phone number in the United States, with three fields for area code, exchange, and last four digits.

<fieldset>
    <legend>Phone number</legend>
    <input id="areaCode" name="areaCode" title="Area Code" type="text" size="3" value="" >
    <input id="exchange" name="exchange" title="First three digits of phone number" type="text" size="3" value="" >
    <input id="lastDigits" name="lastDigits" title="Last four digits of phone number" type="text" size="4" value="" >
</fieldset> 

Example 3: A Search Function A Web page contains a text field where the user can enter search terms and a button labeled "Search" for performing the search. The title attribute is used to identify the form control and the button is positioned right after the text field so that it is clear to the user that the text field is where the search term should be entered.

<input type="text" title="Type search term here"/> <input type="submit" value="Search"/>

Example 4: A data table of form controls
A data table of form controls needs to associate each control with the column and row headers for that cell. Without a title (or off-screen LABEL) it is difficult for non-visual users to pause and interrogate for corresponding row/column header values using their assistive technology while tabbing through the form.

For example, a survey form has four column headers in first row: Question, Agree, Undecided, Disagree. Each following row contains a question and a radio button in each cell corresponding to answer choice in the three columns. The title attribute for every radio button is a concatenation of the answer choice (column header) and the text of the question (row header) with a hyphen or colon as a separator.

Img Element

Allowed attributes mentioned at MDN.

  • alt
  • crossorigin
  • decoding
  • height
  • importance (experimental api)
  • intrinsicsize (experimental api)
  • ismap
  • referrerpolicy (experimental api)
  • src
  • srcset
  • width
  • usemap

As you can see title attribute is not allowed inside img element. I would use alt attribute and if requires I would use CSS (Example: pseudo class :hover) instead of title attribute.

Marion answered 28/2, 2019 at 9:32 Comment(1)
TITLE is a global attribute: developer.mozilla.org/en-US/docs/Web/HTML/Global_attributesMagnetron
W
2

You should not use title attribute for the img element. The reasoning behind this is quite simple:

Presumably caption information is important information that should be available to all users by default. If so present this content as text next to the image.

Source: http://blog.paciellogroup.com/2010/11/using-the-html-title-attribute/

HTML 5.1 includes general advice on use of the title attribute:

Relying on the title attribute is currently discouraged as many user agents do not expose the attribute in an accessible manner as required by this specification (e.g. requiring a pointing device such as a mouse to cause a tooltip to apear, which excludes keyboard-only users and touch-only users, such as anyone with a modern phone or tablet).

Source: http://www.w3.org/html/wg/drafts/html/master/dom.html#the-title-attribute

When it comes to accessibility and different screen readers:

  • Jaws 10-11: turned off by default
  • Window-Eyes 7.02: turned on by default
  • NVDA: not supported (no support options)
  • VoiceOver: not supported (no support options)

Hence, as Denis Boudreau adequately put it: clearly not a recommended practice.

Wimble answered 15/8, 2013 at 9:33 Comment(0)
E
2

alt attribute is for visually impaired users that would use a screen reader. If alt is missing from ANY image tag, the entire url for the image will be read. If the images are for part of the design of the site, they should still have the alt but they can be left empty so the url does not have to be read for every part of the site.

Equine answered 1/10, 2018 at 21:21 Comment(0)
S
2

I would ALWAYS go with both the alt and the title attributes. They are each used for different purposes: alt for image replacement on missing images, slow image downloads, or in assistive technology, while title is for rollover interactivity and additive image description.

In addition, in HTML5 you should start using the new HTML5 picture element wrapped in figure with full WPA-ARIA attributes for greater accessibility, as well as support of assistive technologies, screen readers, and the like. Because this element is not supported in many older browsers...BUT degrades gracefully...I recommend the following HTML design pattern now for images in HTML:

<figure aria-labelledby="picturecaption2">
    <picture id="picture2">
        <source srcset="image.webp" type="image/webp" media="(min-width: 800px)" />
        <source srcset="image.gif" type="image/gif" />
        <img id="image2" style="height:auto;max-width: 100%;" src="image.jpg" width="255" height="200" alt="image:The World Wide Web" title="The World Wide Web" loading="lazy" no-referrer="no-referrer" onerror="this.onerror=null;" />
    </picture>
    <figcaption id="picturecaption2"><small>"My Cool Picture" [<a href="http://creativecommons.org/licenses/" target="_blank">A License</a>] , via <a href="https://commons.wikimedia.org/wiki/" target="_blank">Wikimedia Commons</a></small></figcaption>
</figure>

The code above has many extra "goodies" beside alt and title, including ARIA attributes, support for WebP, a media query supporting higher resolution imagery, and a nice fallback pattern supporting older image formats. It shows a fully decorated image example that uses new technologies while still supporting old ones with progressive design patterns.

Many developers have been using this pattern now for over 20 years to deal with IE and other issues. So this is not new knowledge. Its just been rediscovered by new developers that didn't bother to learn from the past.

REMEMBER...ALWAYS SUPPORT THE OLD BROWSERS!

Seven answered 27/2, 2021 at 6:6 Comment(0)
V
1

No, alt is better because its purpose is to provide an "alternate" text in the event that the image cannot be view (whether it be that the image is missing or that the browser itself is incapable of displaying it).

Voss answered 16/5, 2009 at 13:2 Comment(1)
so i think you don't care whether it is 3.8 stars or 4.2 stars?Sterne
H
0

The MVCFutures for ASP.NET MVC decided to do both. In fact if you provide 'alt' it will automatically create a 'title' with the same value for you.

I don't have the source code to hand but a quick google search turned up a test case for it!

    [TestMethod]
    public void ImageWithAltValueInObjectDictionaryRendersImageWithAltAndTitleTag() {
        HtmlHelper html = TestHelper.GetHtmlHelper(new ViewDataDictionary());
        string imageResult = html.Image("/system/web/mvc.jpg", new { alt = "this is an alt value" });
        Assert.AreEqual("<img alt=\"this is an alt value\" src=\"/system/web/mvc.jpg\" title=\"this is an alt value\" />", imageResult);
    }
Henchman answered 31/7, 2009 at 1:18 Comment(1)
So it acts just like IE7 and below, which is considered bad behavior. robertnyman.com/2009/05/07/…Scour

© 2022 - 2024 — McMap. All rights reserved.