Drop-down lists referred to as “simulated” only use some of the default HTML form elements for drop-down lists, namely, the <select> fields and <option> tags. Simulated drop-down lists make use of a useful feature of CSS that makes it possible to customize a part of lists obtained.
There are other more advanced techniques to simulate drop-down lists in their entirety, which give you the tools to fine tune the appearance of choice lists. These techniques are much more complicated than the solution proposed here, and are not discussed in this document.
The basic HTML code for a standard drop-down list:
<form action="index.html" method="post"> <label for="eye-colour"> Which colour are your eyes? <select name="eye-colour" id="eye-colour"> <option value="black">Black</option> <option value="blue">Blue</option> <option value="green">Green</option> <option value="brown">Brown</option> <option value="other">Other</option> </select> </label> <input type="submit" value="Submit information" /> </form>
Without JavaScript, a standard form is shown to the user.
With JavaScript, the behaviour of simulated drop-down lists must be the same as those of standard drop-down lists.
To implement the essentials of this method, you need to respect the following steps:
opacity: 0;
is applied to the <select>
tag when JavaScript is active. This property makes the list invisible by default, but the options are displayed when you click <select>
. The list stays in the same place on the screen, since it is only made invisible by changing the opacity. <select>
. For example, a <span>
can be used. This container is then dynamically populated in JavaScript, with the current value of the drop-down list. The script is called each time the status of the drop-down list changes. <select>
is positioned in the CSS just above the created container, so that when you click on the container it triggers the opening of the drop-down list. Going into more detail, the scripts must handle the following points:
opacity: 0;
is applied to the <select>
tags.<select>
. In our example, a <span> is used.aria-hidden=“true”
attribute is applied to the container, in order to hide it from users of screen readers, who otherwise would have access to the invisible <select>
.<select>
. This class is deleted as soon as the <select>
loses the focus. The CSS uses this class to simulate visually that the focus has been set on the container. <img/>
tag (with the alt
attribute left empty) to indicate that it is a drop-down list. A good accessibility practice is to use a down arrow for this element. The resulting code in JavaScript is as follows:
<form method="post" action="index.html"> <label for="eye-colour"> Which colour are your eyes ? <div class="list-container"> <select id="eye-colour" name="eye-colour"> <option value="black">Black</option> <option value="blue">Blue</option> <option value="green">Green</option> <option value="brown">Brown</option> <option value="other">Other</option> </select> <span class="container-choice" aria-hidden="true">Black<img alt="" src="images/arrow.png"></span> </div> </label> <input type="submit" value="Submit information"> </form>