I have a site with an image at the top of the page. The site is fully responsive.
For the desktop view, I have a specific image I load for the header on the site. For the mobile view, I have a different image.
In the CSS, I use something like:
.headerimage { background:url(/images/header-desktop.png) no-repeat top left; }
@media only screen and (max-width: 480px) {
.headerimage { background:url(/images/header-mobile.png) no-repeat top left; }
}
And in the HTML it’s just <div class='headerimage'></div>
My question is: how can I add ALT text (like I would to an IMG) tag so my site is fully accessible for screen readers, I get the SEO benefits of alt text, etc.? Obviously I can switch to using an IMG tag but that would require some serious hacks to keep it responsive once I have two IMG tags to deal with.
3
Answers
You can’t without resorting to Javascript AFAIK.
Using only CSS, your best option to have some description of the background image in your code is to use the title attribute in the DIV tag.
See here :
CSS background image alt attribute
CSS or Javascript – display fallback text if background image not loaded
HTML 5 introduces the picture element which allows you to specify a series of images with media queries.
Add a broken image, a hack.
Pseudo elements work for broken images. Read here
Use the pseudo elements to show other images on breakpoints.
Fiddle