Addressing the issue of reversed keyboard navigation order for radio buttons caused by the flex-direction styling

Scenario:

We have a set of radio button cards grouped together, which we want to arrange differently for small and large screens. I am looking for a solution that allows us to achieve this without duplicating the HTML code and maintain accessibility.

Preferred layout for large screens:

|Large|Medium|Small|

Preferred layout for small screens:

|Small|
|Medium|
|Large|

Expected accessibility behavior:

In a typical radio group, users would expect to navigate through options using the tab key to select the first option and then arrow keys to move between options.

Approaches attempted:

I've experimented with utilizing display:flex and adjusting the flex-direction based on screen breakpoints (either row or

column-reverse</code). Additionally, I added <code>tabindex
attributes to the radio inputs. While this fixed the tab navigation, arrow key functionality was reversed.

Here's a simplified example:

<html lang="en">
    <head>
      <meta charset="utf-8">
    </head>

    <body>
    
        <p>Standard view:</p>
        <div style="display:flex;">
            <div>
                <input type="radio" id="featured" name="tier2" value="featured" tabindex="1">
                <label for="featured">Featured</label><br>
            </div>
            
            <div>
                <input type="radio" id="standard" name="tier2" value="standard" tabindex="2">
                <label for="standard">Standard</label><br>
            </div>
            
            <div>
                <input type="radio" id="basic" name="tier2" value="basic" tabindex="3">
                <label for="basic">Basic</label>
            </div>
        </div>
    
        <p>Mobile view:</p>
        <div style="display:flex; flex-direction:column-reverse">
            <div>
                <input type="radio" id="featured" name="tier" value="featured" tabindex="3">
                <label for="featured">Featured</label><br>
            </div>
            
            <div>
                <input type="radio" id="standard" name="tier" value="standard" tabindex="2">
                <label for="standard">Standard</label><br>
            </div>
            
            <div>
                <input type="radio" id="basic" name="tier" value="basic" tabindex="1">
                <label for="basic">Basic</label>
            </div>
        </div>
            
    </body>
</html>

Inquiries:

  • Are there other attributes similar to tabindex that dictate the arrow key navigation order?
  • Is there an alternative method to achieve this without redundant HTML?

Appreciate the insights!

Answer №1

Concise Answer

Avoid using attributes like tabindex and flex.

Instead, tailor the mobile version of your HTML and utilize JavaScript to rearrange the component's DOM order upon page load for larger screens.

Detailed Response

Are there alternatives to tabindex for managing arrow key functionality?

Unfortunately, no suitable alternative exists.

Furthermore, employing tabindex can disrupt the logical tab order of your application, so it is best avoided in nearly all cases!

Is there a method that doesn't entail duplicating html?

Regrettably, there isn't a seamless solution without duplicating HTML content that effectively works across various screen readers and browsers.

However, several options are available:

  1. Modify HTML on the server-side - Dynamically load the component via AJAX based on screen width considerations.

  2. Modify HTML on page load - Begin with a mobile-centric layout as the base structure and adjust item arrangement using JS if the screen size surpasses a defined breakpoint.

The crucial aspect is ensuring the correct DOM order, which alleviates potential complexities associated with manipulating it via CSS or tabindex properties.

Both approaches yield similar outcomes but present distinct advantages and drawbacks:

Server-side Modification (Option 1)

Pro - Faster page loading speed with performance benefits from lazy item loading.

Pro - Streamlined and tailored HTML content necessary for different views.

Pro - Simplified maintenance by editing raw HTML files on the server without intricate reordering processes.

Con - Risk of failed JavaScript result in item not rendering at all.

Con - Additional network call may offset initial performance gains; assessing impact is essential.

Page Load Modification (Option 2)

Pro - Minimal addition of JavaScript for desktop reordering when adopting mobile-first approach.

Pro - Failed JavaScript results in rendered item display, though possibly out of desired sequence.

Con - Maintenance challenges arise as list grows in complexity.

Con - Balancing JavaScript size with additional data transmission and screen size checks may impede performance slightly.

My Preferred Choice?

If faced with this scenario, I would opt for Option 2.

The rationale behind this decision lies in prioritizing minimal JS overhead over full AJAX calls, especially emphasizing performance optimization.

This choice assumes the form is not immediately visible ("above the fold"); in such instances, selecting Option 1 mitigates Cumulative Layout Shift concerns, preventing user irritation due to list reordering during page loading.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The HTML background color is refusing to change

I've been struggling to tweak the background color of a specific page. I attempted setting the HTML as the background color, using !important, but unfortunately, it didn't yield the desired result. The same goes for trying to set the background c ...

Modify the pseudo element when a different class is active

I've encountered an issue while attempting to modify the after pseudo element when the .active class is applied. Despite my efforts, it doesn't seem to be functioning as intended. My current project setup involves using scss with Vue. I have tri ...

What causes block elements to act like inline elements?

:target { border: 2px solid #D4D4D4; background-color: #e5eecc; } .navigation li { list-style-type: none; float: left; padding: 1em; } <ul class="navigation"> <li> <a href="#help"> Help </a> </li> <li> & ...

Trigger sound when it is fully loaded using HTML5 and JavaScript

Looking for a way to make your HTML5 audio file automatically play as soon as it loads on the page? I've got you covered. Thanks in advance! ...

Utilizing React Classes Based on Conditions

I'm attempting to add the class active if commentBadgeActive is true. The Scholar image should transition from grayscale to colored. <Paper className={classes.paper}> <div className={classes.profile}> < ...

Placing Bootstrap nav-items outside of the justify-content-center alignment

Having trouble positioning a button in the nav container to the right side of the screen while keeping all other nav items centered using justify content center. I've tried the following solutions: Using margin-left:auto on the button, but it shifts ...

Check the radio box corresponding to the adjacent label

As a hobby, I have been exploring ways to automate questionnaires that I used to manually deal with for years. It has always intrigued me how best to streamline this process, and now I am taking the opportunity to indulge in my passion and enhance my JavaS ...

Website issues being displayed by Internet Explorer

Greetings! Let me begin by saying how wonderful this page is - I have already found many answers here before. Please forgive any errors in my English :) In my efforts to optimize my website for SEO and increase its speed, I made several changes. The site ...

CSS to target every second visible tr element using the :nth-child(2n)

My table has a unique appearance (shown below) thanks to the application of CSS nth-child(2n). tr:nth-child(2n) {background-color: #f0f3f5;} https://i.sstatic.net/1AnDi.png I made some elements hidden on the vID, ID, and MO_Sub tr. <tr style="displa ...

The SVG path is not aligned properly with the container's viewbox dimensions

Recently, I came across an icon provided by a plugin I am using called calendar. However, I wanted to enhance my icons set with a new addition - the twitter icon. svg { display: inline-block; height: 16px; width: 16px; margin: 2px 0; ...

Displaying "Loading..." with jQuery in Bootstrap 4 while utilizing the data-loading-text feature

I am attempting to create a loading effect for a button, similar to the example shown on codepen. My project utilizes bootstrap 4 (beta 2) and Jquery 3.2.1. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> &l ...

What could be causing the shadowbox not to display in Internet Explorer?

I am currently working on fixing a shadowbox issue in Internet Explorer. The page where the shadowbox is located can be found at this link: Here is the HTML code: <div class="hero-image"> <a href="m/abc-gardening-australia-caroli ...

Organizing AngularJS Data by Initial Letter with the <select></select> HTML Element

I'm trying to figure out how to filter an ng-repeat function based on the first letter of each option. For example, I want to filter a set of array or string so that only options starting with "A" or "B" are displayed. Can anyone help me with this? H ...

Design a clickable div element embedded within an interactive article using HTML5

Currently, I am facing an issue with placing clickable div elements inside an article tag in HTML5. The problem is that when I try to click the divs, it registers as a click on the article itself. Below is an example of my code: HTML: <article id=&apo ...

Unusual gaps of white space appearing between React components

Currently developing a small web application with multiple components. The backend functionality is all set, but encountering an unusual styling issue. Specifically, noticing a white space appearing between each component without any clear reason. This ga ...

What is the process of integrating SCSS into an Angular2 Seed Project?

Is there a recommended method for incorporating SCSS into an Angular2 Seed Project? Are there any informative tutorials or reference sites available? I attempted to implement SCSS following instructions from this link https://github.com/AngularClass/angu ...

Using JavaScript or another programming language to relocate files on a desktop

Is there a way to move pictures from the desktop to a folder called "pictures" using JavaScript? If not, perhaps through Ajax, PHP, or HTML? Is it possible to achieve this task by any means? Edit: I prefer not to do it on my server for web users. Is it f ...

Adding a CSS class to a LinkButton upon clicking in an ASP.NET application

Let's say I have 3 link buttons on a webpage, <asp:LinkButton ID="LB1" runat="server" CssClass="regular" OnClick="LB1_Click"> Today </asp:LinkButton> <asp:LinkButton ID="LB2" runat="server" CssClass="regular" OnClick="LB2_Click"> ...

Adjust the alignment of text on both ends of the HTML5 canvas

Is there an easier way to align text on both sides of a canvas element besides using CSS? The link below might provide some insight: https://github.com/nnnick/Chart.js/issues/114#issuecomment-18564527 I'm considering incorporating the text into the d ...

What is the best approach for managing routing in express when working with a static website?

Whenever a user navigates to mydomain.com/game, I aim for them to view the content displayed in my public folder. This setup functions perfectly when implementing this code snippet: app.use('/game', express.static('public')) Neverthel ...