<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mihaela Miches</title>
    <description>The latest articles on DEV Community by Mihaela Miches (@mihaelamiches).</description>
    <link>https://dev.to/mihaelamiches</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F220950%2F4056242a-8b54-49a2-ade0-c331dd012b51.png</url>
      <title>DEV Community: Mihaela Miches</title>
      <link>https://dev.to/mihaelamiches</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mihaelamiches"/>
    <language>en</language>
    <item>
      <title>Labelling form inputs</title>
      <dc:creator>Mihaela Miches</dc:creator>
      <pubDate>Wed, 26 Feb 2020 09:15:15 +0000</pubDate>
      <link>https://dev.to/mihaelamiches/labelling-form-inputs-4b31</link>
      <guid>https://dev.to/mihaelamiches/labelling-form-inputs-4b31</guid>
      <description>&lt;h2&gt;
  
  
  Semantic HTML and accessibility
&lt;/h2&gt;

&lt;p&gt;Most of the times, when it comes to HTML and accessibility, all boils down to semantic HTML, that is using the correct HTML elements for their purpose as much as possible. This ensures that screen readers can infer as much information about the intent behind your page structure and announce the content in a meaningful way for the users. &lt;/p&gt;

&lt;h3&gt;
  
  
  Going the extra mile
&lt;/h3&gt;

&lt;p&gt;There are scenarios where you'll need to put extra work to make sure the content is accessible to everyone. An instance of this are form inputs and their associated labels.&lt;/p&gt;

&lt;p&gt;Let's take as an example a text input:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;All users, whether they're using a screen reader or not will have a hard time figuring out what information are they supposed to enter in the input. The natural thing is to add a label next to the input:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;label&amp;gt;&lt;/span&gt;First Name:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That gives context to sighted users but is not of much help when we use a screen reader. Voice over is not able to infer the context in which we use the input.&lt;/p&gt;

&lt;p&gt;Let's make it clear for the screen readers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"first-name"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;First Name:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"first-name"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The input element is identified by an &lt;code&gt;id&lt;/code&gt; which we can use to tell the label to which element it's associated using the &lt;code&gt;for&lt;/code&gt; attribute.&lt;/p&gt;

&lt;h3&gt;
  
  
  Other methods of labeling form controls
&lt;/h3&gt;

&lt;p&gt;Using the &lt;code&gt;for&lt;/code&gt; attribute to associate a label with the form control is not the only way to achieve accessible form controls.&lt;/p&gt;

&lt;p&gt;Just as well, you could set the &lt;code&gt;aria-labelledby&lt;/code&gt; attribute to the &lt;code&gt;id&lt;/code&gt; of the label&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"first-name"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;First Name:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;aria-labelledby=&lt;/span&gt;&lt;span class="s"&gt;"first-name"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Using an &lt;code&gt;aria-label&lt;/code&gt; attribute or wrapping the input element by a label will also do the job.&lt;/p&gt;

</description>
      <category>a11y</category>
      <category>html</category>
    </item>
  </channel>
</rss>
