Forms: "for" attribute when wrapping inputs in labels

I'm going to be creating a form which has multiple instances of the same inputs, due to many of those inputs being relegated to a separate 'modal' html at mobile. Because of this, I won't be applying IDs to the inputs, but instead using class. I've been told, for ARIA reasons, it would still be best to wrap my inputs in labels.. However, because my inputs won't have IDs, I'm confused what the use of these labels will be, since the label "for" attribute only points to IDs, which I won't have. With regard to ARIA, will wrapping an input in a label be enough to signal to the ARIA user the purpose of a particular input, even if they're not connected by "for" /ID ?
35 Replies
WillsterJohnson
WillsterJohnsonβ€’2y ago
<label>
<span>Text</span>
<input type=... />
</label>
<label>
<span>Text</span>
<input type=... />
</label>
is similar to
<label for="thing">Text</label>
<input id="thing" type=... />
<label for="thing">Text</label>
<input id="thing" type=... />
By putting the input inside the label, an association is created between them much like you can do with for= and the input ID. If you're putting inputs inside labels, don't use the for attribute on the label. My personal preference is the nesting because it creates a clear visual hierarchy in the markup, but use whichever you prefer when you don't have any constraints
thethingisback
thethingisbackOPβ€’2y ago
gotcha thank you !
WillsterJohnson
WillsterJohnsonβ€’2y ago
np!
thethingisback
thethingisbackOPβ€’2y ago
so then is the label just left blank? sorry if it's not getting an id and I'm wrapping it I mean
WillsterJohnson
WillsterJohnsonβ€’2y ago
In that setup you would put your label text in the span;
<label>
<span>Username</span>
<input type="text" />
</label>
<label>
<span>Username</span>
<input type="text" />
</label>
Something like that ^
thethingisback
thethingisbackOPβ€’2y ago
ohh I see okay that works then what if I'm making a search bar that has placeholder text in them identifying what the input is for.. would I still need the <span>? Since visually there would not be any extra html outside of the search bar input I mean I would end up needing to hide that span. So like
<label> <input type="search" placeholder = "Filter by title" /> </label>
WillsterJohnson
WillsterJohnsonβ€’2y ago
In that case I would do something like
<label>
<span class="sr-only">Filter by title</span>
<input type="search" placeholder="Filter by title" />
</label>
<label>
<span class="sr-only">Filter by title</span>
<input type="search" placeholder="Filter by title" />
</label>
.sr-only {
position: absolute;
left: -1000vw;
}
.sr-only {
position: absolute;
left: -1000vw;
}
Just to ensure that the input remains labelled clearly. I'm not 100% on if placeholder text is read by screenreaders or if it's a visual UI thing, but best practice says using a label makes sure you're covered
ErickO
ErickOβ€’2y ago
a big rule of a11y is to not rely on placeholders for displaying information if you want to be strict (and I sure will) having only the placeholder, even with the sr-only solution, is wrong
WillsterJohnson
WillsterJohnsonβ€’2y ago
btw for code blocks on discord type it like this; ```html <!-- you can change that "html" to "css", "js", "ts", or any other language name for more accurate syntax highlighting --> ``` also see #πŸ‘‹welcome as it's got a lot more about formatting which is pretty useful
ErickO
ErickOβ€’2y ago
how did you escape the code
thethingisback
thethingisbackOPβ€’2y ago
thanks yea, I was gonna ask about that
ErickO
ErickOβ€’2y ago
backslash yeh?
WillsterJohnson
WillsterJohnsonβ€’2y ago
# **yeah** *backslash* ~~escapes~~ __any__ `discord` formatting, same as markdown
thethingisback
thethingisbackOPβ€’2y ago
So was @willsterjohnson 's solution wrong?
ErickO
ErickOβ€’2y ago
not wrong...incomplete perhaps in this situation in particular I'd say is mostly fine I'd have an issue if your placeholder was for example...a format, say you have an input for a birthday and your placeholder is mm/dd/yyyy when the placeholder tells the user HOW to fill up the info, then 100% f that for this tho...it's alright
WillsterJohnson
WillsterJohnsonβ€’2y ago
for a text input it's fine to have the same label text as placeholder text, so long as you know you're allowed to have them be different. You might wanna label something "username" with the placeholder "your_name_here" or something, as long as there's a label that correctly describes the input, the placeholder is more of a UX problem than an a11y problem. Also if you're talking about a text input with placeholder="mm/dd/yyyy"... ew once for that and ew again for putting the month before the day and the year at the end
thethingisback
thethingisbackOPβ€’2y ago
for the record, i won't have any date inputs, but good to know
ErickO
ErickOβ€’2y ago
no the placeholder is an a11y issue, primarily for cognitive disabilities, hence why it's a bigger issue when your placeholder gives info on how to fill up that input for something as simple as "search" is fine tho the sr-only label is a must tho for sure
thethingisback
thethingisbackOPβ€’2y ago
i just want my wrapped label to somehow serve its ARIA purpose, which I'm assuming if is has a span couple with the input inside of it, that would be enough for ARIA to recognize it
ErickO
ErickOβ€’2y ago
a visible label can be removed
thethingisback
thethingisbackOPβ€’2y ago
coupled*
ErickO
ErickOβ€’2y ago
yes, this solution works as it should
thethingisback
thethingisbackOPβ€’2y ago
idk what sr-only stands for either
ErickO
ErickOβ€’2y ago
screen reader only just a class name, you can call it whatever but it's to hide it visually but screen readers can still detect it
WillsterJohnson
WillsterJohnsonβ€’2y ago
sr-only is kinda standard amongst devs, the name can be read to mean "this class is being used to hide things visually without hiding them from the document flow that assistive technology uses"... so instead of typing all that we use sr-only and most of us have just accepted this unofficial standard name
thethingisback
thethingisbackOPβ€’2y ago
gotcha ok good information, thank you guys very much wait I'm still confused as to what to do about the <span> inside the <label>.. I don't want to see span text smushed up next to my inputs or I guess that's what that absolute positioning was for ok
WillsterJohnson
WillsterJohnsonβ€’2y ago
yeah the sr-only thing is for when you dont want to see the span. If you do wanna see it, but you don't like how it's positioned, you can use display: flex or display: grid on the <label> and adjust the styles to your preference
thethingisback
thethingisbackOPβ€’2y ago
so the class alone is enough to keep it hidden? Without applying any settings to the class? I'm seeing it work, I've never heard of an element working that way before. I would've figured I'd have needed to set something to it to not be visible. From what I've seen online, it looks like it's a Bootstrap class, but I'm not even running Bootstrap and it's still working?
WillsterJohnson
WillsterJohnsonβ€’2y ago
let's break down the CSS quickly
.sr-only {
position: absolute;
left: -1000vw;
}
.sr-only {
position: absolute;
left: -1000vw;
}
the absolute position takes the element outside of the normal flow of the document; by using properties like left, right, top, bottom, and inset, we can say something like left: 20px; top: 50px, which tells the browser to always keep this element 20 pixels from the left edge, and 50 pixels from the top edge of the screen. I would recommend opening up codepen and playing around with absolute positioning if you're not already familiar with it. So we know that left: X units means "always make this X units away from the left edge", so when we put a negative value in there, we're placing the element on the opposite side of the left edge; ie, off of the screen. We use a ridiculously large number here because it doesn't cost anything to do so, and it makes absolutely certain that no matter what the user does, it's not gonna show up on screen. the sr-only class is entirely unrelated to bootstrap, though it's possible that they will have implemented it or something similar. No bootstrap is needed though, just the few lines of CSS at the top of this message.
WebMechanic
WebMechanicβ€’2y ago
the sr-only class is entirely unrelated to bootstrap, though it's possible that they will have implemented it or something similar.
it's also called .visually-hidden iirc One's the Bootstrap name, the other is based off the HTML5 Boilerplate project. Can't recall which one πŸ™‚
vince
vinceβ€’2y ago
Curious why you used a span with .sr-only instead of aria-label on the label? New to accessibility so they could be completely different usecases
WebMechanic
WebMechanicβ€’2y ago
ARIA attributes do nothing and that could've been any element, like b or ruby. It's moved "off-screen" so it doesn't matter, but a non-semantic element like span is a good choice for such trickery. In this particular case using ARIA would even be counter productive. aria-hidden would hide the element from screen readers which is exactly the opposite of what the CSS solution does, which only hides it visually on the page but keeps it accessible for "screen readers only". ARIA attributes are hints you as a developer provide to the browser so it can tell AT about what's going on inside the markup and teach stupid elements like div and span to be smart about what they might do and contain. With ARIA you can also redefine the inherent semantic meaning various HTML elements have built in. Accessible websites are not based on ARIA alone. HTML5 can do a lot. It's when you use JS and complex interactions, where ARIA attributes created and modified from that very JS code can help to explain what's happening. ARIA used the wrong way can make a site even less accessible for some users of assistive technology than no ARIA at all. It's an additional vocabulary to spice up vanilla HTML5 where it lacks built in accessibility features and semantics, or where custom code and custom elements dynamically changes the page contents an implement new interactions. That's why there's a need for this addition to create "Accessible Rich Internet Applications".
vince
vinceβ€’2y ago
Cool, really great insight! But I'm still a bit confused. What if we removed the span entirely, and instead added an aria-label. So instead of
<label>
<span class="sr-only">Filter by title</span>
<input type="search" placeholder="Filter by title" />
</label>
<label>
<span class="sr-only">Filter by title</span>
<input type="search" placeholder="Filter by title" />
</label>
We use:
<label aria-label="Filter by title">
<input type="search" placeholder="Filter by title" />
</label>
<label aria-label="Filter by title">
<input type="search" placeholder="Filter by title" />
</label>
Maybe you already answered this in your answer, but I wasn't able to grasp it. Would that not parse similarly in a screen reader? I have NVDA, this should be a good exercise
WebMechanic
WebMechanicβ€’2y ago
sorry, there's often weeks where I don't come here. You probably checked how this "sounds" with NVDA. To me, the point of having a label element in the first place is to also increase the hittable area, sth. that's built into the label/input relationship, and other elements don't do. In your second example, there's no benefit in using a label at all if it's tightly wrapped arpund the input (no padding), might as well be a span. In both cases there's only the placeholder visible which often is problematic, too, 'cos it's typically rendered in a dimmed, low-contrast font color, some find hard to read;.If it's not dimmed ppl might think the field already contains text and skip over, only to get presented with bizzare error message for not entering all data when they submit the form. I don't believe there is any viable reason to not have a visible and clickable label around an input element. Moving it off-sight is just some imagined "aesthetics" by designers that serves no real use whatsoever. There is no screen where you also enter data that can't also hold a couple extra pixels above the input to put a well visible label to and help target the field it belongs to. Form design is hard and can quickly become an A11Y and UX minefield if done wrong. Many forms might look pretty, but that's their whole achievement; they often still lack usability. This is my rough outline of form controls
<label><b>The label text</b>
<input type="..." />
<small class="hint">Explain what data is expected</small>
<em class="notice">Message explaining _what_ went wrong and _how_ to fix it.</em>
</label>
<label><b>The label text</b>
<input type="..." />
<small class="hint">Explain what data is expected</small>
<em class="notice">Message explaining _what_ went wrong and _how_ to fix it.</em>
</label>
Typically no placeholder, "hint" and "notice" appear as required. I don't need to explain "firstname", so no "hint" needed etc. "notice" is toggled by validation with aria attributes that serve as CSS selectors. With flexbox or grid, all five different element types elements can be positioned at will and come with reasonable default styles.
vince
vinceβ€’2y ago
Hey no problem! Thank you so much for the detailed response as always πŸ™‚
Want results from more Discord servers?
Add your server