Html Inert Global Attribute for Improved User Experience

Author

Reads 227

Detailed view of HTML code on a computer screen, ideal for tech and software development themes.
Credit: pexels.com, Detailed view of HTML code on a computer screen, ideal for tech and software development themes.

The Html Inert Global Attribute is a game-changer for improving user experience. By using this attribute, developers can make dynamic content inert, preventing it from being interacted with until it's fully loaded.

This attribute works by adding a unique ID to the content, which is then referenced in a JavaScript function that removes the inert attribute once the content is loaded. For example, in an article about Html Inert, it was shown that using the inert attribute on a dynamic list of items prevented the user from interacting with the list until all items were loaded.

By using the inert attribute, developers can provide a better user experience by preventing users from interacting with incomplete or dynamic content. This can help to reduce errors and improve overall user satisfaction.

Accessibility Issues

Accessibility is crucial when using the inert attribute. By default, there is no visual way to tell whether an element or its subtree is inert, so it's essential to provide clear visual and non-visual cues about content inertness.

Inert sections not being obviously inert can lead to frustration and bad user experience, especially if users are zoomed in to a small section of content or can't view the content at all.

Consider reading: Visual Studio Html

You might like: Html Table Title

Offscreen or Hidden DOM Elements

Credit: youtube.com, Enhanced UI control and Responsible Accessibility with the HTML INERT Property

Offscreen or hidden DOM elements can be a challenge for accessibility, especially with elements like a drawer that add elements to the DOM that are not always visible to the user.

You can ensure that while the drawer sub-elements are offscreen, a keyboard user cannot accidentally interact with it using the inert attribute.

Inert helps prevent keyboard users from interacting with offscreen elements, which can be a game-changer for accessibility.

For example, if you have a navigation menu that slides in from the side, you can use inert to prevent keyboard users from interacting with the menu while it's still offscreen.

By using inert, you can create a more seamless experience for users with disabilities, who often rely on keyboard navigation.

It's a small but significant detail that can make a big difference in the user experience.

Curious to learn more? Check out: Set up Html Mail Using Word

Non-Interactive DOM Elements

Non-Interactive DOM Elements can be a challenge, especially during page load or while a form is submitting. It's essential to indicate the state of the UI and "trap" the focus to the part of the page that is interactive.

Credit: youtube.com, CSS - inert | JSer - Front-End Interview questions

A common scenario is when a UI design is visible or partially visible, but clearly non-interactive. This can be during page load, while a form is submitting, or if a dialog overlay is open, for example.

To provide the best experience for users, you should indicate the state of the UI. By doing so, you'll help users understand what's going on and where they can interact.

For your interest: Html Visible

Blocked Interactions

Inert blocks focus and click events by default, which can be problematic for assistive technologies.

This means that tabbing and discoverability are also blocked, as well as page search and text selection in the element.

Screen readers may ignore elements that are blocked, which can lead to inconsistent user experiences.

Common examples of blocked interactions include modal dialogs, focus-trapping menus, and side navigation.

Related reading: Focus Html Element Js

Blocked Interactions and Movement

Focus trapping is a crucial aspect of UI accessibility, ensuring that screen reader focus is on interactive UI elements.

Broaden your view: Ui Core Html

Credit: youtube.com, 4. World Interaction - Movement Blocking

Blocking elements such as a modal dialog, focus-trapping menu, or side nav can limit rogue screen readers from reaching behind a page overlay.

This also helps prevent accidental submissions, like when a form is still processing the first submission.

A carousel with non-active items is another example of blocked interactions and movement.

Non-applicable form content, like disabling the "Shipping address" fields when the "Same as billing address" checkbox is checked, is also a common occurrence.

The inert attribute blocks focus and click events by default, which can also block tabbing and discoverability for assistive technologies.

Here are some common blocked interactions and movements:

  • Focus and click events
  • Tabbing
  • Discoverability
  • Page search
  • Text selection

By understanding these blocked interactions and movements, you can design more accessible and user-friendly interfaces.

Prevent Prior Focus

Making an element inert by disabling access via keyboard or mouse is fine, but it might have had focus before this, and that focus will persist.

The :focus-within CSS selector is a powerful tool to determine if a subtree has focus.

Team of professionals collaborating in an office with digital interface on background screen.
Credit: pexels.com, Team of professionals collaborating in an office with digital interface on background screen.

This selector can tell us whether a subtree has focus, even if it's a child of another element. It can pierce into any Shadow Root to tell us whether anything in there is focused by the user.

The CSS selector :focus-within is more powerful than JavaScript functions that can only tell us whether a single element has focus.

The document.activeElement property might seem like it would work, but it breaks down in a world of Shadow DOM. It will point to the very top of the chain, rather than the actual focused element.

This is especially true when making part of a Web Component inert, but that Web Component is contained by several others.

Additional reading: Html Number Selector

Visually Indicate Elements

Visually indicating elements is crucial for users who can't see all parts of a page at once. This includes users of screen readers, small devices, or with magnifiers, and even users just using particularly small windows.

Credit: youtube.com, Inert Polyfill -- A11ycasts #02

Inert elements by default have no visual indication of being inert. It's recommended to clearly mark what parts of the DOM are active and which are inert.

Not all users can see all parts of a page at once, which can lead to frustration if inert sections are not obviously inert. This is especially true for users of screen readers, small devices, or with magnifiers.

For individual controls, the disabled attribute is probably more appropriate than the inert attribute for visually indicating inactivity.

See what others are reading: B Tag Html

Mobile Screen Readers

Mobile screen-readers have a unique challenge when it comes to modals. They can navigate behind modals if they're not implemented correctly.

Using inert on all other siblings of the modal can prevent this issue. This is especially important if you can't add aria-modal="true".

Bootstrap's modal component in version 4 didn't work as expected with mobile screen-readers. It was only when I added aria-modal="true" that it started working fine.

Readers also liked: Modal Html

Credit: youtube.com, Understanding Assistive Technology: Mobile Screen Readers

In fact, the default demo in version 4 was missing the aria-modal="true" attribute, which is why I could reach the content behind it on my mobile device. That's also mentioned in a related GitHub issue.

Version 5 of Bootstrap's modal component added the role="dialog" and aria-modal="true" attributes automatically. This is a good example of how to implement modals correctly for mobile screen-readers.

If you can't add aria-modal="true", using inert on all other siblings of the modal is the way to go. This ensures that mobile screen-reader users can't reach behind the modal.

Use Cases

Inert can be useful beyond dialogs. It's not just for preventing users from interacting with content behind dialogs and modals.

Modals aren't the only use case for inert. Other scenarios that need to disable interactivity behind exposed user interface include navigation that pops out over parts of a page.

Drawer navigation is a good example of this. It's like a modal but not really, and it can occur when a user presses a navigation menu item.

Inert can also be useful for off-screen menus and content in accordions that use height or opacity instead of visibility.

Suggestion: Html Navigation

Big Challenge

A man navigates through challenging whitewater rapids, demonstrating skill and excitement in kayaking.
Credit: pexels.com, A man navigates through challenging whitewater rapids, demonstrating skill and excitement in kayaking.

The Big Challenge with HTML Inert is that it can't prevent an element from gaining focus, as focus events like focus and focusin are non-cancelable.

You have to instruct the browser before the user attempts to click on an element, which can be tricky to manage.

The browser can't be intercepted to "stop" the event, making it a big challenge to work with HTML Inert.

Expand your knowledge: Vscode Open Html in Browser

Code and Functionality

The `inert` attribute makes coding accessible modal windows a whole lot easier. It disables keyboard and screen reader focus of an element and its children, which is a game-changer.

To achieve this, you need to disable all functionality behind the dialog, including buttons, links, and even static elements like plain text. This must be done for as many assistive technologies as possible, such as screen readers, keyboards, voice recognition, and everything else.

The `inert` attribute makes it easy to trap focus inside the dialog, similar to driving in a roundabout. You can only exit the dialog by exiting it, and you must drive around the roundabout until you need to exit.

A woman wearing virtual reality goggles interacting with a digital interface inside a modern space.
Credit: pexels.com, A woman wearing virtual reality goggles interacting with a digital interface inside a modern space.

Here are the key steps to implement the `inert` attribute:

  1. Add the `inert` attribute to the element you want to disable.
  2. Make sure to disable all interactive elements within the dialog.
  3. Use JavaScript to manipulate the DOM and apply the `inert` attribute dynamically.

Note that `inert` is supported by all major browsers on desktop and mobile, except Firefox, but you can add a polyfill to remedy this situation.

Walter Brekke

Lead Writer

Walter Brekke is a seasoned writer with a passion for creating informative and engaging content. With a strong background in technology, Walter has established himself as a go-to expert in the field of cloud storage and collaboration. His articles have been widely read and respected, providing valuable insights and solutions to readers.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.