How do you add a control in html?


Definition and Usage

The controls attribute is a boolean attribute.

When present, it specifies that audio/video controls should be displayed.

Controls should include:

  • Play
  • Pause
  • Seeking
  • Volume
  • Fullscreen toggle (for video only)
  • Captions/Subtitles (for video only, when available)
  • Track (for video only, when available)

Applies to

The controls attribute can be used on the following elements:

ElementsAttribute
controls
controls

Examples

Audio Example

An

Try it Yourself »

Video Example

A

Try it Yourself »


Browser Support

The controls attribute has the following browser support for each element:

Element
audio 4.0 9.0 3.5 4.0 10.5
video 4.0 9.0 3.5 4.0 10.5


❮ HTML

Example

A

Try it Yourself »


Definition and Usage

The controls attribute is a boolean attribute.

When present, it specifies that video controls should be displayed.

Video controls should include:

  • Play
  • Pause
  • Seeking
  • Volume
  • Fullscreen toggle
  • Captions/Subtitles (when available)
  • Track (when available)

Browser Support

The numbers in the table specify the first browser version that fully supports the attribute.

Attribute
controls 4.0 9.0 3.5 3.1 11.5

Syntax


❮ HTML


There are some cases where the available native HTML form controls may seem like they are not enough. For example, if you need to perform advanced styling on some controls such as the element. We will also discuss how, when, and whether building your own control makes sense, and what to consider when building a control is a requirement.

Note: We'll focus on building the control, not on how to make the code generic and reusable; that would involve some non-trivial JavaScript code and DOM manipulation in an unknown context, and that is out of the scope of this article.

Design, structure, and semantics

Before building a custom control, you should start by figuring out exactly what you want. This will save you some precious time. In particular, it's important to clearly define all the states of your control. To do this, it's good to start with an existing control whose states and behavior are well known, so that you can mimic those as much as possible.

In our example, we will rebuild the , it should behave the exact same way as the select for all users, from keyboard to mouse to touch to screen reader, and any other input device.

In our example, the missing specifications are obvious so we will handle them, but it can be a real problem for exotic new controls. When it comes to standardized elements, of which the element with the keyboard to look at all the available choices (this is the same as clicking the element:


<div class="select" tabindex="0">
  
  <span class="value">Cherryspan>

  
  <ul class="optList">
    
    <li class="option">Cherryli>
    <li class="option">Lemonli>
    <li class="option">Bananali>
    <li class="option">Strawberryli>
    <li class="option">Appleli>
  ul>
div>

Note the use of class names; these identify each relevant part regardless of the actual underlying HTML elements used. This is important to make sure that we don't bind our CSS and JavaScript to a strong HTML structure, so that we can make implementation changes later without breaking code that uses the control. For example, what if you wish to implement the equivalent of the element later on?

Class names, however, provide no semantic value. In this current state, the screen reader user only "sees" an unordered list. We will add ARIA semantics in a bit.

Creating the look and feel using CSS

Now that we have a structure, we can start designing our control. The whole point of building this custom control is to be able to style it exactly how we want. To that end, we will split our CSS work into two parts: the first part will be the CSS rules absolutely necessary to make our control behave like a element. We include our control and the element before each instance of our custom control. There is a benefit to having this "extra" select even if our JavaScript works as hoped: we will use this select to send data from our custom control along with the rest of our form data. We will discuss this in greater depth later.

<body class="no-widget">
  <form>
    <select name="myFruit">
      <option>Cherryoption>
      <option>Lemonoption>
      <option>Bananaoption>
      <option>Strawberryoption>
      <option>Appleoption>
    select>

    <div class="select">
      <span class="value">Cherryspan>
      <ul class="optList hidden">
        <li class="option">Cherryli>
        <li class="option">Lemonli>
        <li class="option">Bananali>
        <li class="option">Strawberryli>
        <li class="option">Appleli>
      ul>
    div>
  form>
body>

Second, we need two new classes to let us hide the unneeded element: we visually hide the custom control if our script isn't running, or the "real" element - or we have not changed the body class, therefore the body class is still "no-widget", so the elements whose class is "select" must be hidden */ position: absolute; left: -5000em; height: 0; overflow: hidden; }

This CSS visually hides one of the elements, but it is still available to screen readers.

Now we need a JavaScript switch to determine if the script is running or not. This switch is a couple of lines: if at page load time our script is running, it will remove the no-widget class and add the widget class, thereby swapping the visibility of the elements, and to dynamically add the DOM tree representing the custom control after every

element matches the role grid, and the
    element matches the role list. Because we use a
      element, we want to make sure the listbox role of our control will supersede the list role of the
        element. To that end, we will use the role presentation. This role is designed to let us indicate that an element has no special meaning, and is used solely to present information. We will apply it to our
          element.

          To support the listbox role, we just have to update our HTML like this:

          
          <div class="select" role="listbox">
            <span class="value">Cherryspan>
            
            <ul class="optList" role="presentation">
              
              <li role="option" class="option">Cherryli>
              <li role="option" class="option">Lemonli>
              <li role="option" class="option">Bananali>
              <li role="option" class="option">Strawberryli>
              <li role="option" class="option">Appleli>
            ul>
          div>
          

          Note: Including both the role attribute and a class attribute is not necessary. Instead of using .option use the [role="option"] attribute selectors in your CSS .

          The aria-selected attribute

          Using the role attribute is not enough. ARIA also provides many states and property attributes. The more and better you use them, the better your control will be understood by assistive technologies. In our case, we will limit our usage to one attribute: aria-selected.

          The aria-selected attribute is used to mark which option is currently selected; this lets assistive technologies inform the user what the current selection is. We will use it dynamically with JavaScript to mark the selected option each time the user chooses one. To that end, we need to revise our updateValue() function:

          function updateValue(select, index) {
            const nativeWidget = select.previousElementSibling;
            const value = select.querySelector('.value');
            const optionList = select.querySelectorAll('[role="option"]');
          
            // We make sure that all the options are not selected
            optionList.forEach((other) => {
              other.setAttribute('aria-selected', 'false');
            });
          
            // We make sure the chosen option is selected
            optionList[index].setAttribute('aria-selected', 'true');
          
            nativeWidget.selectedIndex = index;
            value.innerHTML = optionList[index].innerHTML;
            highlightOption(select, optionList[index]);
          };
          

          It might have seemed simpler to let a screen reader focus on the off-screen select and ignore our stylized one, but this is not an accessible solution. Screen readers are not limited to blind people; people with low vision and even perfect vision use them as well. For this reason, you can not have the screen reader focus on an off-screen element.

          Here is the final result of all these changes (you'll get a better feel for this by trying it with an assistive technology such as NVDA or VoiceOver):

          If you want to move forward, the code in this example needs some improvement before it becomes generic and reusable. This is an exercise you can try to perform. Two hints to help you in this: the first argument for all our functions is the same, which means those functions need the same context. Building an object to share that context would be wise.

          An alternative approach: Using radio buttons

          In the above example, we reinvented a has natively.

          On the plus side, this control is fully accessible to a screen reader and fully navigable via the keyboard. However, this control isn't a does.

          We'll leave adding this missing functionality as a reader exercise.

          Conclusion

          We have seen all the basics of building a custom form control, but as you can see it's not trivial to do. Before creating your own customized control, consider whether HTML provides alternative elements that can be used to adequately support your requirements. If you do need to create a custom control, it is often easier to rely on third-party libraries instead of building your own. But, if you do create your own, modify existing elements, or use a framework to implement a pre-baked control, remember that creating a usable and accessible form control is more complicated than it looks.

          Here are a few libraries you should consider before coding your own:

          • jQuery UI
          • AXE accessible custom select dropdowns
          • msDropDown

          If you do create alternative controls via radio buttons, your own JavaScript, or with a 3rd party library, ensure it is accessible and feature-proof; that is, it needs to be able to work better with a variety of browsers whose compatibility with the Web standards they use vary. Have fun!

          See also

          Learning path

          Advanced Topics

          How do you use controls in HTML?

          HTML Form Controls.
          Text Input Controls..
          Checkboxes Controls..
          Radio Box Controls..
          Select Box Controls..
          File Select boxes..
          Hidden Controls..
          Clickable Buttons..
          Submit and Reset Button..

          What is a control element HTML?

          The controls attribute is a boolean attribute. When present, it specifies that audio/video controls should be displayed. Controls should include: Play. Pause.

          What are the control tags that we use in HTML?

          Description.

          Which is used to control HTML and CSS?

          HTML HTML stands for Hyper Text Markup Language and it is the language that is used to define the structure of a web page. HTML is used along with CSS and Javascript to design web pages. ... HTML..