HTML img tag: title attribute vs. alt attribute?

后端 未结 11 746
悲&欢浪女
悲&欢浪女 2020-12-07 12:01

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 u

相关标签:
11条回答
  • 2020-12-07 12:32

    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.

    0 讨论(0)
  • 2020-12-07 12:34

    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.

    0 讨论(0)
  • 2020-12-07 12:37

    No, I think alt is better because the purpose of that attribute is to provide "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).

    0 讨论(0)
  • 2020-12-07 12:38

    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, but alt should always be there.

    0 讨论(0)
  • 2020-12-07 12:46

    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);
        }
    
    0 讨论(0)
提交回复
热议问题