Home >Web Front-end >JS Tutorial >Accessibility (a) Rules - 3

Accessibility (a) Rules - 3

Barbara Streisand
Barbara StreisandOriginal
2024-11-18 20:32:02763browse

Images

It's not just about adding alt text to an image; it's much more than that.

Decorative images

  • If an image doesn't add additional context or information (decorative image) that allows the user to better understand the context then it should be hidden from assistive technology (AT), such as a screen reader.

  • Use any of these methods to hide images from AT:

    • An empty or null text alternative (alt)
    • Applying ARIA
    • Add the image as a CSS background
<!-- All of these choices lead to the same result. -->
<img src=".../Ladybug.jpg" role="presentation">
<img src=".../Ladybug.jpg" role="none">
<img src=".../Ladybug.jpg" aria-hidden="true">
  • When in doubt, add descriptions to images.

Empty or null alt

  • Do not set the image alt attribute to "" unless the image is purely decorative.
Attribute Example Screen Reader Behavior Use Case
alt="" (Empty) Ignores the image completely Decorative or non-informative images
Missing alt May read the filename/URL Not recommended; implies negligence

Informative images

  • If an image conveys a concept, idea, or emotion you should include programmatic alternative text describing the purpose of the image.

  • Add a highly detailed description of the image wherever possible.

e.g.

<img src=".../Ladybug_Swarm.jpg" alt="A swarm of red ladybugs is resting 
on the leaves of my prize rose bush.">

Accessibility (a) Rules - 3

  • If the image is an (inline), add role="img".

  • Since elements do not support the alt attribute, use alternative coding methods to provide a description.

Method Use Case Pros Cons
</td> <td>Short, brief descriptions</td> <td>Simple, widely supported</td> <td>Limited in length</td> </tr> <tr> <td>aria-label</td> <td>Brief descriptions</td> <td>Quick, inline</td> <td>Best for short text</td> </tr> <tr> <td>aria-labelledby</td> <td>Complex descriptions using <title> and <desc></td> <td>Comprehensive, flexible</td> <td>More verbose</td> </tr> <tr> <td><figcaption></td> <td>Visible description in a figure context</td> <td>Visible and accessible</td> <td>Not ideal for all SVGs</td> </tr> </tbody> </table></div> <h3> Functional images </h3> <ul> <li><p>Any image with a functional purpose (e.g., a logo that links to the home page, a magnifying glass icon used as a search button) should include appropriate alt text.</p></li> <li><p>Alt text should describe the <strong>image’s action</strong>, not its visual aspects.</p></li> <li><p>If the image is both informative and actionable you can add alternative descriptions to each element—but it is not a requirement. e.g.<br> </p></li> </ul> <pre class="brush:php;toolbar:false"><!-- All of these choices lead to the same result. --> <img src=".../Ladybug.jpg" role="presentation"> <img src=".../Ladybug.jpg" role="none"> <img src=".../Ladybug.jpg" aria-hidden="true"> </pre> <h3> Complex images </h3> <ul> <li><p>If an image requires more explanation than a decorative, informational, or functional image include infographics, maps, graphs/charts, and complex illustrations use any of these methods to add alternative descriptions</p></li> <li><p>Link out to a resource or provide a jump link to a longer explanation later on the page. e.g.<br> </p></li> </ul> <pre class="brush:php;toolbar:false"><img src=".../Ladybug_Swarm.jpg" alt="A swarm of red ladybugs is resting on the leaves of my prize rose bush."> </pre> <ul> <li>Append the aria-describedby attribute to the <img> element then link the image to an ID containing a longer description. e.g. </li> </ul> <pre class="brush:php;toolbar:false"><div title="Navigate to the homepage"> <a href="/"> <img src=".../Ladybug_Logo.png" alt="Lovely Ladybugs for your Lawn"/> </a> </div> </pre> <h3> Alternative text best practices </h3> <ul> <li><p>Advised to cap alternative text to 150 characters or less to avoid reader fatigue.</p></li> <li><p>Avoid using words like <strong>"image of"</strong> or <strong>"photo of"</strong> in the description, as the screen reader will identify these file types.</p></li> <li><p>When naming images, be as consistent and accurate as possible. Image names are a fallback when the alternative text is missing or ignored.</p></li> <li><p>Avoid using non-alpha characters (for example, #, 9, &) </p></li> <li><p>Use dashes between words rather than underscores in image names or alternative text.</p></li> <li><p>Use proper punctuation whenever possible. Without it, the image descriptions will sound like one long, never-ending, run-on sentence.</p></li> <li><p>Write alternative text like a human and not a robot. Keyword stuffing does not benefit anyone—people using screen readers will be annoyed, and search engine algorithms will penalize them.</p></li> </ul> <p>The above is the detailed content of Accessibility (a) Rules - 3. For more information, please follow other related articles on the PHP Chinese website!</p></div><div class="nphpQianMsg"><a href="javascript:void(0);">css</a> <a href="javascript:void(0);">less</a> <a href="javascript:void(0);">NULL</a> <a href="javascript:void(0);">Resource</a> <a href="javascript:void(0);">if</a> <a href="javascript:void(0);">for</a> <a href="javascript:void(0);">include</a> <a href="javascript:void(0);">using</a> <a href="javascript:void(0);">Length</a> <a href="javascript:void(0);">Attribute</a> <a href="javascript:void(0);">cap</a> <a href="javascript:void(0);">append</a> <a href="javascript:void(0);">background</a> <a href="javascript:void(0);">idea</a><div class="clear"></div></div><div class="nphpQianSheng"><span>Statement:</span><div>The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn</div></div></div><div class="nphpSytBox"><span>Previous article:<a class="dBlack" title="What are the Maximum Dimensions for Canvas Elements in Different Browsers?" href="https://m.php.cn/faq/1796689270.html">What are the Maximum Dimensions for Canvas Elements in Different Browsers?</a></span><span>Next article:<a class="dBlack" title="What are the Maximum Dimensions for Canvas Elements in Different Browsers?" href="https://m.php.cn/faq/1796689284.html">What are the Maximum Dimensions for Canvas Elements in Different Browsers?</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>Related articles</h2><em><a href="https://m.php.cn/article.html" class="bBlack"><i>See more</i><b></b></a></em><div class="clear"></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-6t+ed+2i-1n-4w" data-ad-client="ca-pub-5902227090019525" data-ad-slot="8966999616"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><ul class="nphpXgwzList"><li><b></b><a href="https://m.php.cn/faq/1609.html" title="An in-depth analysis of the Bootstrap list group component" class="aBlack">An in-depth analysis of the Bootstrap list group component</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1640.html" title="Detailed explanation of JavaScript function currying" class="aBlack">Detailed explanation of JavaScript function currying</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1949.html" title="Complete example of JS password generation and strength detection (with demo source code download)" class="aBlack">Complete example of JS password generation and strength detection (with demo source code download)</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/2248.html" title="Angularjs integrates WeChat UI (weui)" class="aBlack">Angularjs integrates WeChat UI (weui)</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/2351.html" title="How to quickly switch between Traditional Chinese and Simplified Chinese with JavaScript and the trick for websites to support switching between Simplified and Traditional Chinese_javascript skills" class="aBlack">How to quickly switch between Traditional Chinese and Simplified Chinese with JavaScript and the trick for websites to support switching between Simplified and Traditional Chinese_javascript skills</a><div class="clear"></div></li></ul></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5902227090019525" data-ad-slot="5027754603"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><footer><div class="footer"><div class="footertop"><img src="/static/imghwm/logo.png" alt=""><p>Public welfare online PHP training,Help PHP learners grow quickly!</p></div><div class="footermid"><a href="https://m.php.cn/about/us.html">About us</a><a href="https://m.php.cn/about/disclaimer.html">Disclaimer</a><a href="https://m.php.cn/update/article_0_1.html">Sitemap</a></div><div class="footerbottom"><p> © php.cn All rights reserved </p></div></div></footer><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body><!-- Matomo --><script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="https://tongji.php.cn/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '9']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script><!-- End Matomo Code --></html>