Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Responsive CSS: Can I force rendering of alt text?

I'm putting together some Responsive CSS for a website I'm building and I'm curious if I can use CSS to force images to render as alt text instead of images. We are displaying the logos of cosponsors but because of their variable size it's hard to fit them confidently into the responsive design. For that reason we'd like to store the company name as alt text and render that instead. Of course we could place the name in a separate element and toggle the visibility using CSS but using alt text seems DRYer.

like image 745
busoni34 Avatar asked Mar 06 '13 17:03

busoni34


People also ask

How do you automate alt text?

Turn automatic alt text onSelect File > Options > Accessibility. Check that Automatically generate alt text for me is selected under Automatic Alt Text.

Do screen readers read alt text?

Alt-text does not affect how an image is displayed but provides the content in an alternative text-based format. A screen reader will read this aloud and the person using the device is given context and meaning about how the image relates to the rest of the page.

Why is alt text important for accessibility?

Also referred to as an alt description or an alt attribute, alt text is critical for those with disabilities because it describes the function and appearance of a photo or graphic they cannot see. PDF documents often contain images that convey critical information to the reader.

Why do we use alt text?

Alternative (Alt) Text is meant to convey the “why” of the image as it relates to the content of a document or webpage. It is read aloud to users by screen reader software, and it is indexed by search engines. It also displays on the page if the image fails to load, as in this example of a missing image.


1 Answers

You could store that in a data-attribute rather than the alt text, and then do something like this:

<span class='responsive' data-alt='foo'>
    <img src='http://www.ponyfoo.com/img/thumbnail.png' alt='' />
</span>

@media only screen and (max-width: 300px) {
    .responsive:before {
        content: attr(data-alt);
    }
    .responsive img {
        display: none;
    }
}

The reason you can't do this just with CSS and an img tag is that img tags is because they are replaced elements, which means pseudo doesn't work with them, and therefore, using :before doesn't work with them.

Another approach, taking this into account would be the following:

<span class='responsive'>foo</span>

.responsive {
    background-image: url('http://www.ponyfoo.com/img/thumbnail.png');
    text-indent: -9999em;
    overflow: hidden;
    width: 180px;
    height: 180px;
    display: block;
}

@media only screen and (max-width: 300px) {
    .responsive {
        background-image: none;
        text-indent: initial;
        overflow: initial;
    }
}

If you ask me, I like the second approach a lot more.

like image 58
bevacqua Avatar answered Sep 19 '22 09:09

bevacqua