Adding alternative text to background images is a crucial step in ensuring their accessibility. Providing alt text allows screen

One challenge I am facing is that my website displays images as background images using background-size: cover to fill the element completely and crop out any excess parts of the image. However, these images are not just decorative; they are essential for conveying information on the page. To make them accessible to screen readers and assistive technologies, they need alt text descriptions.

I'm wondering what the best approach is to add alt descriptions to background images in a semantic way.

article {
  position: relative;
  width: 320px;
  margin: 5rem auto;
}

figure {
  width: 100%;
  height: 180px;
  /* not accessible */
  background-image: url('http://www.fillmurray.com/300/300');
  background-size: cover;
  background-position: center;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />

<article class="panel panel-default">
  <header class="panel-heading">
    <h4 class="panel-title">Title of Article</h4>
  </header>
  <div class="panel-body">
    <figure class="article-image"></figure>
  </div>
  <footer class="panel-footer">
    <a class="btn btn-default" href="#">Read</a>
  </footer>
</article>

Answer №1

Latest Info for 2024

The utilization of the CSS property object-fit has now become widespread across various browsers, eliminating the need to transform an image into a background in order to apply resizing options like cover and fill. Consequently, it is recommended to employ a standard <img> element (or <picture>, etc) with an alt attribute instead. The previous method described below is considered outdated.


Past Solution

To ensure proper accessibility of a background image, the most semantically correct approach involved using both a background image along with a regular img tag simultaneously.

  1. Insert the img within the element containing the background image.
  2. Conceal the visibility of the img so that sighted users perceive only the background image while users relying on assistive technologies can still access the actual img.

Note: Merely setting the image to display: none; would hide it from assistive technologies as well, which contradicts the intended purpose. An alternative strategy is required.

If utilizing Bootstrap, there exists a convenient built-in class designed specifically for this scenario: .sr-only. For those not using Bootstrap, the styles for such a class can be manually added to their own stylesheet:

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

Implementing this technique based on the example provided yields the following structure:

article {
  position: relative;
  width: 320px;
  margin: 5rem auto;
}

figure {
  width: 100%;
  height: 180px;
  /* not accessible */
  background-image: url('http://www.fillmurray.com/300/300');
  background-size: cover;
  background-position: center;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />

<article class="panel panel-default">
  <header class="panel-heading">
    <h4 class="panel-title">Title of Article</h4>
  </header>
  <div class="panel-body">
    <figure class="article-image">
      <!-- include the img tag but visually hide it with .sr-only -->
      <img class="sr-only" alt="Bill Murray" src="http://www.fillmurray.com/300/300" />
    </figure>
  </div>
  <footer class="panel-footer">
    <a class="btn btn-default" href="#">Read</a>
  </footer>
</article>

Answer №2

The W3C offers a helpful example for this situation - just assign a role="img" to the div and include an aria-label with your description.

For more details, visit:

Answer №3

In order to make them accessible to screen readers and other assistive technologies, images must have alt text.

It's important to note that some users may rely on assistive technologies other than screen readers. Using the sr-only CSS class alone is not sufficient to ensure accessibility for all users.

For example, individuals with visual impairments may prefer to hide images and show their text alternatives instead.

Since Edge 16, the object-fit property works for images in 92% of browsers, eliminating the need for a fallback solution for most modern browsers. However, a fallback can still be provided for older browsers if necessary.

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

Fade image and background using Jquery on click event

I've been browsing various sources and utilizing different techniques I've come across (primarily on this platform) to guide me in my progress so far. However, I fear that I may have unintentionally backed myself into a corner by choosing an inco ...

The custom error message for invalid input remains unchanged even after the box is selected

Currently working on implementing an input (checkbox) that displays a custom message when it is considered invalid (unchecked). However, even after the user checks the box, the message remains displayed and the box is still considered invalid. I suspect th ...

Animation using jQuery is functional on most browsers, however, it seems to

After creating an animation to simulate an opening door using jQuery, I discovered that it works perfectly on Firefox 24, Chrome 28, and IE 8. However, Safari presents a problem - the door opens but then the "closed" door reappears at the end of the animat ...

Display a section of the picture at maximum width

Can anyone help with displaying only a portion of an image at full width (100%) without any overflow issues? The code I've tried is not working: .intro_sea { position: absolute; width: 101%; clip-path: inset(30% 50% 0 0); margin: 0; paddi ...

Attempting to align three images in a row on a webpage with clickable links placed below using HTML and CSS

Shown below is a snippet of the HTML code I am currently working with: div.Navbar { margin-left: 10px; margin-right: 10px; } .icons { height: 300px; } <div class="NavBar"> <img src="images/ThumbNails/Conflict.jpg" class="icons" style="f ...

How can we prevent the restoration of size effects in jQuery UI versions 1.9 and above?

After implementing jQuery UI 1.9 or newer, I noticed that the size effect returns to its original state. Below is a snippet of my code: http://jsfiddle.net/mQCxY/ $(function () { $('.circle').click(function () { $(this).effect("size ...

Simple method for displaying or hiding divs depending on the status of a selected radio button using CSS only

I am currently working on showing or hiding div elements based on the status of a radio button. These divs are located after the input buttons in a different section of the page, and I am facing challenges in referencing them accurately. This code is for ...

Retrieve the position with respect to the browser viewport

My current scenario involves two divs, with one nested within the other. The task at hand is to determine the position of the child div in relation to the browser window. Specifically, I aim to detect when the user scrolls down and have the child div fade ...

"Enhancing user experience with jQuery hover effects on table

This is an example of the HTML structure: <tr class="row-1 row-first"> <td class="col-1 col-first"> <div class="views-field views-field-field-logo-image"></div> <div class="views-field views-field-field-logo-title"> ...

Unable to locate image on Angular server while running on Raspberry Pi

After setting up my Raspberry pi using Angular to create a simple website with the basic ng serve command, I encountered an issue when trying to add my own image. When I attempt to use src to link to my image file, it is not being found: <img src="air. ...

The Angular ng-bind directive does not reflect changes in value updates

I am trying to dynamically update the content of a span element to reflect the current position of the element. The issue I am facing is that Angular does not update the value of the ng-bind attribute when I change the element's position. Here is my ...

Adding a PHP file into an HTML page using JavaScript include

Recently, I was assigned to collaborate with a third-party vendor who displays movie times on their platform. We were given the opportunity to co-brand our website on their platform by creating a wrapper for our site. This wrapper would allow the movie tim ...

How can you make a dynamic 360 image that responds to mouse movements using three.js?

Is it possible to achieve a three.js effect similar to the one shown here? We have come across solutions that involve drag&drop for camera angle control, but we are interested in having the mouse position dictate where the camera points. For instance, ...

Step-by-step guide to activating vertical scrolling for select tiers in a multi-layered fly-out navigation

Currently working on a multi-level flyout menu to be integrated into a daterange picker for selecting time periods. Check out the jsfiddle link here: https://jsfiddle.net/6t72hd4x/1/ I attempted to set overflow-y: auto; in the .inner-list class to handle ...

Styling a list within a div using CSS

I have a CSS file that looks like this: .h_bg{ height:100%; background-size:auto 100%; background-position:center center; background-repeat:no-repeat; position:relative; } .h_bg h1{ width: 100%; position:absolute; line-heig ...

sleek lateral scrolling reminiscent of k2.pl

I'm in the process of creating a website with a horizontal scroll similar to k2.pl. Here's what I have accomplished so far using jQuery animate and scroll: // Obtaining the X and Y axis and moving the entire page accordingly $('.scroll&apo ...

The CSS module is disappearing before the Framer Motion exit animation finishes when the path changes

Source Code Repository: https://github.com/qcaodigital/cocktail_curations Live Site: I recently deployed my NextJS project to Netlify and everything seems to be working fine, except for one issue. Each page has an exit animation (currently set to change ...

Can I use a custom font in an HTML5 canvas?

Has anyone had success importing a custom font for use in HTML5 canvas? I've been trying to do this by loading the font file on my computer, but all my attempts have failed so far. The canvas keeps showing the default font instead of the one I want to ...

Deactivate the Autofill feature on Google Toolbar

Dealing with the Google Toolbar's autofill feature has been a constant frustration in my web development journey over the years. I have resorted to creating a timer control to monitor changes, as the developers overlooked firing change events on contr ...

Tips for utilizing innerHeight for a div during both window loading and resizing tasks?

I'm currently working on calculating the top/bottom padding of a div (.content) based on its height, and updating it upon loading and resizing the window. The goal is to have it centered nicely next to another div (.character) positioned beside it. I ...