What is the functionality of screen readers with regards to display flex?

When creating an HTML form, it may be necessary to dynamically change the requiredness of certain fields based on the value of others. In order to visually indicate this requiredness, you might want to add an asterisk to the label preceding each required field. Rather than directly inserting the asterisk in the HTML or using a script, I would like to achieve this through CSS-defined content.

However, adding visual content before an element can be difficult with CSS selector combinators which primarily go forward, towards children and following siblings. One solution could involve toggling a class name in the label when changing the requiredness of the input field, but that approach separates the two actions into distinct instructions.

Therefore, I have adopted a method where I invert the order of labels and input fields, wrapping them in a

display:flex; flex-direction:column-reverse
element. This allows the label text to be displayed before its corresponding field, enabling the use of a next-sibling combinator to add an asterisk to the label of each required field:

label {
  display:inline-flex;
  flex-direction:column-reverse;
}

label>input[required]+span:after
{
  content:"*";
  color:#a21;
}
<label>
  <input type="text" required>
  <span>Field 1</span>
</label>
<label>
  <input type="text">
  <span>Field 2</span>
</label>

My concern is how screen readers will interpret this double inversion - once in the HTML and once in the CSS. Will they read the text before prompting for user input?

I always intend to test these scenarios with a screen reader, but finding the time for it seems to be a challenge!

Answer №1

Screen readers do not prioritize CSS when reading content. They focus on the DOM structure and do not take into consideration any styling applied through CSS. For instance, if you have a horizontal list of links with float:right, visually they may appear from right to left, but screen readers will read them in the order they are laid out in the DOM, ignoring their visual positioning.

It is worth noting that while most screen readers may ignore CSS styles, some might still read text added using the content property. However, relying on this behavior is not advisable as it could change with future updates. In your specific case, current screen readers like JAWS, NVDA, and VoiceOver might say "label one star", but there is no guarantee it will remain consistent. Ultimately, ensuring proper accessibility by setting the required attribute is crucial.

Answer №2

One important point to remember is that screen readers do not consider CSS positioning and follow the order of tags in the DOM.

Referring to H44: Using label elements to associate text labels with form controls, the sequence of label and input elements matters as well.

For all input elements of type text, file, or password, textareas, and select elements on the webpage:

  • Ensure there is a label element describing the control's purpose prior to the input, textarea, or select element

HOWEVER, your scenario presents an unusual case. Your label tag appears before the input tag (in DOM order) while its textual content comes after.

<label>
  <input type="text" required>
  <span>Field 1</span>
</label>

In this situation, it seems like screen readers should still interpret it correctly, announcing "Implicit label Field 1" for both the input and label.

If the label tag precedes the input[type=text], it may not necessarily be considered bad design (although this can vary for input fields like radio or checkbox where position matters).

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

Verification of Email Address Using HTML5

There are claims that with HTML5, there is no longer a need for JavaScript or server-side code to validate user input as a valid email or URL address. I am wondering how I can validate an email after a user enters it and without using JS, how can I displa ...

Tip: Using jQuery to Wrap Characters Based on Count

Within my table, there is a specific cell in each row that contains exactly 13 characters. <td class="need-to-wrap">0123456789012</td> I am looking to wrap characters #10, 11 and 12 with a span element like so: <td class="need-to-wrap"> ...

Center both vertically and horizontally in Bootstrap UI Modal

I'm attempting to create a Bootstrap UI Modal that is centered both vertically and horizontally. While I found this solution that successfully centers the modal vertically, it loses its horizontal centering when applied to my own template with 800px ...

Card flipping functionality is not functional on Internet Explorer browsers (versions 11, 10, and below)

After coming across a tutorial on CSS3 transform by David Desandro, I tried implementing the code but unfortunately, it does not work in Internet Explorer... I noticed that clicking "flip" only results in Card 1 being displayed while card 2 remains hidden ...

serializeArray encounters difficulty in locating certain input elements

I've created this HTML layout: <div class="col-md-12"> <div class="form-group"> <label>Mini description (displaying the latest added destinations and meta description)</label> <textarea class="for ...

Strategies for avoiding the opening of a new tab when clicking on a link in ReactJs

RenderByTenantFunc({ wp: () => ( <Tooltip id="tooltip-fab" title="Home"> <Button className="home-button"> <a href={ ...

Adding pictures to the background image

I am encountering an issue with the code on my Wordpress website Here is a snippet of HTML: <div class="test"> <?php if(has_category( 'update' ) || has_category( 'uncategorized' )) { ?> <img alt="icn" src="& ...

Tips for managing a fixed-positioned element during scrolling and zooming events

I have a situation where I need a fixed-position element to stay at the top-right corner of the viewport. Here is the HTML code: <div class = "header"> <p>This heading has an absolute position</p> </div> And here is the CSS: .he ...

How can I create a CSS animation that fades in text blocks when hovering over an image?

I have been trying to implement CSS3 transitions to create a hover effect where the text and background color fade in when hovering over an image. Despite my attempts with various selectors and transition types, I can't seem to achieve the desired res ...

Stopping a file transfer in case of browser closure or upload cancellation

When working on uploading a file asynchronously using HTML5 in MVC3, a common issue arises when dealing with large files such as 1GB. If the upload process is cancelled or the browser is closed at 50% completion, a 500MB file still gets saved in the target ...

Leveraging $_POST data in embedded CSS styling

Can I use a PHP variable in inline CSS code? In the CSS section below, I have tried to incorporate a PHP variable: <style> p { text-align: center; } img{ -moz-animation:<?php $_POST["rmp"]; ?>s rotateRight infinite li ...

Text animation is not triggered when the focus is removed from the input field without any text present

Having an issue with my search bar When the user clicks on it, it's supposed to expand with a simple animation to allow typing, and should shrink back if the user clicks elsewhere on the page. However, the problem arises when there is no text entered ...

Divide the PHP code into separate files

Can someone help me with a coding dilemma I have? I couldn't find any information on Google or this site about it. A friend of mine who has experience in website development and runs his own business uses an interesting system that is new to me. He ...

What is the best way to incorporate data from my Express server into my HTML table?

I am seeking a way to dynamically add new rows to a table in my HTML file by fetching data from server.js using Express. I am aware that I can use double curly braces "{{example}}" in the HTML file to insert values from the server file using "res.render(vi ...

Expanding columns to reach the full height of the page

Struggling to create three columns within the colmask and threecol div classes, excluding the header and footer. Any suggestions? Check out the website at I've attempted setting the body and html tags to 100% height. I've also experimented with ...

Insert item at the end of the box and move all elements upwards

Hello! I'm experimenting with creating something using javascript's createElement method. My goal is to achieve an effect similar to this image: https://i.stack.imgur.com/itKUK.gif Currently, my code is functional, but the animation goes from to ...

What are the best techniques for creating a responsive UI design that adapts to all screen widths?

I created a UI by following some tutorials, but I'm facing an issue with responsiveness. When I resize the window, the UI doesn't look good or adjust properly. Here is the link to my fiddle: http://jsfiddle.net/X8HV9/1/show/ I am encountering tw ...

Issues with the card display within a Bootstrap carousel

Having encountered challenges with displaying cards in a bootstrap carousel, I am seeking assistance. To view my current (incomplete) project, please refer to the CodePen link below. https://codepen.io/robbiecorcoran/pen/eYMVvEN <!-- SEEKING ASSISTANCE ...

Boss declares, "Display the iFrame with no borders visible."

Good day to everyone, I am currently dealing with an issue on the page: It seems to be working perfectly in all of my browsers except for Internet Explorer. Since I don't have IE, I'm having trouble troubleshooting this. My boss mentioned somet ...

Unable to center label when adjusting TextField height beyond default

I was tasked with reducing the size of the MUI Component TextField by 4px. To achieve this, I modified the root & .MuiOutlinedInput-input. Although it adjusted the height as desired, the label (Email) is no longer vertically centered. I attempted to so ...