Html Controls and User Interface Best Practices

Author

Reads 1.2K

Code on a Screen
Credit: pexels.com, Code on a Screen

Html controls play a crucial role in creating engaging user interfaces. A well-designed control can make or break the user experience.

To create a visually appealing interface, use a consistent color scheme and typography. This will help to establish a brand identity and make your application more recognizable.

Html controls can be customized to fit your application's specific needs. For example, buttons can be styled to match your brand's color scheme and font.

Consistency is key when it comes to user interface design. Using html controls consistently throughout your application will help to create a cohesive look and feel.

Related reading: Html Application

HTML Controls

HTML controls are used to create form fields that shrinkwrap their content and grow as more text is entered. This works with input types that accept direct text input, such as text and url, input type file, and textarea elements.

You can use HTML controls to collect different types of data, including text, dates, and numbers. For example, radio buttons are typically used to accept gender information, while checkboxes are often used to allow users to select multiple options.

Here are some common types of HTML controls:

  • Text Input Controls
  • Checkboxes Control
  • Radio Buttons Control
  • Select Box Control
  • File Select Box
  • Button Control
  • Hidden Form Control
  • Datetime Controls
  • Date Control
  • Month Control
  • Week Control
  • Time Control
  • Number Control
  • Range Control
  • Email Control
  • URL Control

Button and Select Options

Credit: youtube.com, Learn HTML buttons in 5 minutes! 🔘

Button elements are used to create clickable buttons, and they can be used to trigger a variety of actions. A button can be a submit button, a reset button, or a button that links to another page.

A submit button is used to send data to a server, and it's typically used in forms. The type attribute of a submit button is set to "submit".

Select Box

Select Box is a versatile control that allows users to select one or more options from a drop-down list. It's a convenient way to provide users with multiple choices without cluttering the interface.

A select box can be created using HTML code, as shown in an example, which illustrates how to create a form with a drop-down box.

Select boxes are particularly useful when you need to offer users a range of options to choose from, making it easier for them to make informed decisions.

Take a look at this: Html and Css Dropdown Menu

Credit: youtube.com, How to Customize Select Option in CSS | Custom Select Box

The HTML code for a select box typically includes a list of options, each represented by an option tag, which is used to define the available choices.

By using a select box, you can keep your interface clean and organized, while still providing users with the flexibility to make their own choices.

Validation and Error Handling

Custom validation error messages can be presented to users by using the Constraint Validation API on input elements. This allows you to display a specific error message when a field fails to validate.

To use custom validation error messages, you need to use JavaScript to check the validity of the input element every time its value is changed. This can be done by running the checkValidity() method via the input event handler.

If the value is invalid, an invalid event is raised, and the invalid event handler function is run. Inside this function, you can work out why the value is invalid and set a custom validity error message.

Note that constraint validation doesn't remove the need for validation on the server side. Invalid values can still be sent by older browsers or by bad actors.

For more insights, see: Html Form Input Validation

Client-Side Validation

Credit: youtube.com, JavaScript Data Validation and Error Handling for Forms

Client-Side Validation is a crucial aspect of ensuring that users enter valid data into your application. It happens on the client-side, meaning it runs on the user's web browser.

You can use the Constraint Validation API to present custom error messages when a field fails to validate. This API is available on input elements and their related counterparts.

To achieve this, you can use JavaScript to check the valid state of the input element every time its value is changed. This is done by running the checkValidity() method via the input event handler.

Here's a simple way to think about it: if the value is invalid, an invalid event is raised, and the invalid event handler function is run. Inside this function, you can work out whether the value is invalid because it's empty, or because it doesn't match the pattern.

The example shows that if the input value is invalid when the submit button is pressed, one of the custom error messages will be shown. If it's valid, it will submit as expected.

Curious to learn more? Check out: Html Form Validation Javascript

Credit: youtube.com, Form validation using Javascript on the client side for beginners

It's essential to note that custom validity has to be cancelled every time the input event is raised. This is done by invoking setCustomValidity() with an empty string value. If you don't do this, and a custom validity was previously set, the input will register as invalid, even if it currently contains a valid value on submission.

Allowlisting

Allowlisting is a less common use case that can be resolved within the scope of this API change. It allows a website to specify the exhaustive list of controls to show.

To implement allowlisting, the list of keywords will have to only contain allowlisted elements, without a no prefix. This means that controls like "play", "timeline", and "volume" can be included in the list.

The feature can be implemented and specified incrementally, allowing only for blocklisting of some elements to be supported first. This incremental approach will allow developers to test and refine the allowlisting feature before adding more functionality.

Here's an interesting read: Html Video Controls

Design and Styling

Credit: youtube.com, Styling HTML Form Controls

Design and Styling is where the magic happens. You can style your HTML controls to match your brand's unique look and feel.

To add a allowlist/blocklist ability to native controls, you can use the new content attribute controlslist, which is a space-separated list of keywords that will be reflected as controlsList. This will only take effect if the controls attribute is set.

The input element can be targeted by type with attribute selectors, making it easy to style specific types of input fields. For example, you can use the pseudo-class :checked to style a checkbox label when the checkbox is checked.

Here's a list of some useful pseudo-classes for styling form controls:

Css

When styling form elements, you can use CSS selectors to target specific types of inputs. The input element can be targeted by type with attribute selectors.

The :enabled pseudo-class targets any currently enabled element that can be activated or accept focus. This is useful for styling elements that are currently interactive.

A different take: Image Styling Html

Credit: youtube.com, Form Styling Essentials | The basics to modern CSS tips & tricks

You can also use the :disabled pseudo-class to target elements that are currently disabled. This is useful for styling elements that are currently non-interactive.

The :read-only pseudo-class targets elements that are not editable by the user. This is useful for styling elements that are currently read-only.

The :read-write pseudo-class targets elements that are editable by the user. This is useful for styling elements that are currently interactive.

Here are some pseudo-classes that are relevant to the input element:

The :default pseudo-class targets form elements that are the default in a group of related elements. This is useful for styling default values.

The :checked pseudo-class targets checkbox and radio input types that are currently checked. This is useful for styling selected options.

The :indeterminate pseudo-class targets checkbox elements whose indeterminate property is set to true by JavaScript, radio elements, and progress elements in an indeterminate state. This is useful for styling elements that are in a mixed state.

Worth a look: Html Default Image

Pseudo Classes Example

Credit: youtube.com, Learn CSS pseudo-classes in 7 minutes! ☟

Pseudo classes are a powerful tool in design and styling. They allow us to apply different styles to elements based on their state or interaction with the user.

We can use pseudo classes to style form elements, such as checkboxes. For example, we can style a checkbox label based on whether the checkbox is checked or not. In this case, we can change the color and font-weight of the label that comes immediately after a checked input.

Pseudo classes can also be used to create interactive effects, like changing the color of a link when it's hovered over. This can enhance the user experience and make the interface more engaging.

By using pseudo classes, we can create a more dynamic and responsive design that adapts to the user's actions.

Background

Web developers have a limited control over the native HTMLMediaElement controls, which is currently managed through the boolean controls attribute.

Historically, custom controls have been implemented by adding elements on top of the video, but this approach can be challenging to implement and maintain.

Credit: youtube.com, Background Styling

The native user agent controls are getting richer, but sometimes they can look out of place or not work properly.

For example, fullscreen and remote playback buttons might not always make sense, and browser-added download buttons can fail to work as intended.

Native controls help user agents build a tailored experience based on the user's environment, device, and habits, something that custom controls can't always achieve.

Additional reading: Html Social Media Buttons

Design

In the proposed design, a new attribute called controlslist is to be added, which will allow developers to set native controls directly from HTML content. This attribute will only have an effect if the controls attribute is set.

The controlslist attribute will be a space-separated list of keywords, which will be reflected as controlsList. This means developers can use this attribute to customize the native controls on their web pages.

A Boolean attribute called incremental is also available, which tells the user agent to process the input as a live search. This is particularly useful for search boxes, where the user agent can send search events to the HTMLInputElement object representing the search box as the user edits the value of the field.

Focused shot of HTML and CSS code on a monitor for web development.
Credit: pexels.com, Focused shot of HTML and CSS code on a monitor for web development.

The incremental attribute is supported by WebKit and Blink extensions, which means it's available in Safari, Opera, Chrome, and other browsers that use these extensions. This attribute allows developers to update search results in real-time as the user edits the search.

Here's a list of non-standard attributes that are available on some browsers:

The orient attribute, for example, defines the orientation of the range slider, with values including horizontal and vertical. This attribute is supported by Firefox and can be used to create vertical form controls in a modern approach.

For your interest: Html Tag B

Accessibility and Compatibility

Accessibility and Compatibility is a top priority for HTML controls. The controlsList feature is designed to be flexible, allowing user agents to ignore unsupported attributes, such as controlslist="foo bar".

This means that even if a user agent doesn't support the controlsList feature, it won't break the entire HTML control, ensuring a smooth user experience.

Backward Compatibility

Backward compatibility is a crucial aspect of accessibility and compatibility. The good news is that it's preserved in many cases.

Credit: youtube.com, XWiki: Improving web accessibility with respect to backward compatibility | Lucas Charpentier

A user agent that doesn't support a certain feature will simply ignore it, so you don't have to worry about breaking anything. This means you can focus on making your content accessible without sacrificing compatibility.

For example, if you include a controlsList attribute with a value that's not supported, the user agent will just ignore it. This is a great way to future-proof your content without compromising on accessibility.

This approach ensures that your content remains usable even if a user agent doesn't support the latest features. It's a win-win situation that benefits both users and developers.

IDL and Reflection

IDL and Reflection is a crucial aspect of accessibility and compatibility. The controlsList property reflects the controlslist attribute as a DOMTokenList with a list of supported tokens.

This means that developers can easily access and manipulate the list of supported tokens, making it easier to implement accessibility features.

The IDL (Interface Definition Language) provides a standardized way to define the structure and behavior of web components, ensuring consistency and predictability.

For instance, the controlsList property allows developers to specify the list of supported tokens for a particular web component, making it easier to implement accessibility features.

A different take: Html Value Property

Lamar Smitham

Writer

Lamar Smitham is a seasoned writer with a passion for crafting informative and engaging content. With a keen eye for detail and a knack for simplifying complex topics, Lamar has established himself as a trusted voice in the industry. Lamar's areas of expertise include Microsoft Licensing, where he has written in-depth articles that provide valuable insights for businesses and individuals alike.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.