Christian Heilmann: Quick tip: conditional form fields with CSS |
As part of a tool I just wrote, I had the issue that one form field was dependent on another and I didn’t want to go the full way and create the field on demand with JavaScript but keep it to CSS. You can see the result here:
A simple way to achieve this visually is to use the :checked pseudo and + selectors as you can see in this Fiddle:
The HTML is the following:
> span> type="checkbox" id="moo"> span> for="moo">Condition> > value for condition: span> type="text" value="20"> > > |
And the important CSS are these few lines:
label + label { padding-left: 1em; opacity: 0.1; transition: 0.5s; } :checked + label + label { opacity: 1; } |
Once you publish something like this, you will get a lot of tweets that this is not accessible. The same happened here, and it doesn’t matter that I wrote about the shortcomings in the first version of this post. The debate is interesting and relevant. So let’s break it down:
It is disappointing that a simple problem like this still needs a lot of DOM manipulation and/or ARIA to really be accessible. Maybe we think too complex – a different way of solving this issue would be to cut this problem into several steps and reload the page in between. Overkill for one field, but for very complex JS-driven forms this might be a much saner solution. And it could result in smaller forms as we don’t need a framework or lots of libraries.
In this – simple – case I wonder what harm there is in the extra form field to be accessible to non-visual users. The label could say what it is. Say the checkbox has a label of “convert to JPG”. A label of “JPG quality” on the text field would make things understandable enough. Your backend could also be intelligent enough to realise that when a JPG quality was set that the user wanted a JPG to be generated. We can be intelligent in our validation that way.
Turns out that adding visibility to the earlier example has the same visual presentation and fixes the tabbing issue:
The HTML is still the following:
> span> type="checkbox" id="moo"> span> for="moo">Condition> > value for condition: span> type="text" value="20"> > > |
And the updated CSS:
label + label { padding-left: 1em; opacity: 0.1; visibility: hidden; transition: 0.5s; } :checked + label + label { opacity: 1; visibility: visible; } |
http://christianheilmann.com/2015/01/08/quick-tip-conditional-form-fields-with-css/
Комментировать | « Пред. запись — К дневнику — След. запись » | Страницы: [1] [Новые] |