How to put ALT in a Background-Image? How to make a Background-Image more accessible and semantic?

Asked

Viewed 697 times

3

It’s a simple but objective question.

We know the importance of tag ALT for semantics, accessibility and even for SEO. But how can I set these tag properties ALT in an image that is defined as background-image?

In short, there is some semantic way to treat a background-imagemin order to make it more accessible and SEO friendly, since the tag ALT is available for tag only <img> and not for a tag <div> for example...

.imagem {
  background-image: url(img/imagem1.jpg);
}
<img src="img/imagem1.jpg" alt="descrição completa da minha imagem">

<div class="imagem"></div>

  • 2

    In theory, if it is background-image it should not need alt, and vice versa. When to use IMG and when to use background?

  • 2

    @Bacco is an interesting topic, the problem is that in some situations to "frame" the image in a more proportional way you need to use some properties that only the background has. Or even to make some animations where we use the background-position and with the properties of an IMG tag would not be possible. There was the doubt of how to make the background more accessible, because it is not always only "decorative", but it was worth the tip, the response of the other!

  • 2

    Then I would really have to see the specific case, because this "is not always decorative" I consider a semantic error, in principle. Okay, it could be a "deliberate error" in the sense that you’re changing semantics for functionality, which I think is valid, but since you’re aware that you’re breaking the rule. Recently I had a case of having to do some "laps" to have a proportional responsive image, but I could solve with IMG (acting in the div below her). Another thing that can reach the point that you asked in the question is by the textual content, and use BG as "illustrative decoration".

  • 1

    @Bacco particularities... while the specifications are not updated we need to adapt, like float:left to do grid with div, or padding to control Aspect-ratio, or <br clear="all">, and so many other ways we need to do... Not always "decorative" images are background :D

  • 3

    I’m not sure, but maybe this is within specs: <div class="imagem" role="img" aria-label="descrição completa da minha imagem"></div>... Fountain

  • @Sam I believe the path will be right there...

  • 3

    @Sam for assistive technologies is yes, I would say it’s an excellent starting point, but you need something to think about indexing as well. There is more than one facet to this whole story (of course a good answer will cover all these scenarios).

  • @Bacco is true.

  • I’m sorry, I bugged a little, but to add an alt to the image would not just put the title="picture text" in your image div? try this.

  • 1

    @Although the Title attribute may be used in these cases it has some limitations on accessibility... https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/title#Accessibility_concerns

Show 5 more comments

2 answers

0

A visual alternative with only HTML and CSS is to use two elements, with background-image above and below, the latter will appear if the top does not load

.imagem {
  position: absolute;
  width: 150px;
  height: 150px;
  top: 10px;
  z-index: 2;
  border: 1px solid black;
}
.imagem-alt {
  position: absolute;
  width: 150px;
  height: 150px;
  top: 10px;
  z-index: 1;
}

#img1 {
  background-image: url('https://via.placeholder.com/150');
  left: 10px;
}
#alt1 {
  left: 10px;
}
#img2 {
  background-image: url('');
  left: 170px;
}
#alt2 {
  left: 170px;
}
<div id="img1" class="imagem"></div>
<span id="alt1" class="imagem-alt">descrição completa da minha imagem</span>

<div id="img2" class="imagem"></div>
<span id="alt2" class="imagem-alt">descrição completa da minha imagem</span>

As mentioned in the comments by @Sam, the accessibility part for people with disabilities can be done by Accessible attributes Rich Internet Applications, however I have no knowledge to answer which of them should be used and how to implement them in the example above

With respect to indexing in search engines, the background is not indexed, it may be possible to add, too, the tag img and make it invisible, but I’m not sure if it would work

  • 1

    It was worth as an experiment, but I don’t consider it usable in a practical way... I even tried to make a "gambit" here tb rss, but before posting I’ll ask for feedback to Anderson :D, I think the best solution would be as Sam said even, then just missing treat the SEO so Google find the image, because accessibility and somehow semantics are at an acceptable level in my view...

0


ALT with background-image

As cited, the property ALT, has benefits, for SEO, semantics and accessibility, being an element used in the marking itself.

When using CSS, there is no way to add the property alt in a background-image and also, it is not appropriate when using the insertion of images from CSS.

According to Christian Heilmann, one background-image, is a property of aesthetic value, not something with content value for the site itself. Images added in CSS, are seen as visual, are not like content on a page, so they are free from alternative texts.

Outras Formas

As @Sam commented, a way around this and having a most accessible element, is to use aria-label, which is a way of specifying a string to an element as an accessible label, for an assistive technology. And the role, despite having some other utilities, it can be used to assign a role to a page element, i.e., insert a semantic value to an element.

<div class="background-image" role="img" aria-label="blah blah blah"></div>

Another way around, is to add the attribute title, which is an attribute to insert a title to an image, but serves as an alternative text for the user. However, this attribute in relation to accessibility, has some inconsistencies and problems. Moreover, according to my research, has no weight of analysis for SEO, because it is an attribute that will only give a title.

<div class="background-image" title="blah blah blah"></div>

There are some other "tricky" ways to do as well, such as adding more elements to appear together or overwriting an image if it doesn’t load, adding small images (really small, to the point where they don’t appear) in elements <img> with alt and then manipulate with other images and some other forms.

SEO

Though they’re in one compliance standard, within my research, I found nothing concerning the attributes role and aria-label, have weight for SEO analysis.

The best way to use SEO engines is to add the tag <img> attribute-ridden alt. Even with functions that some CSS properties provide, if you follow these rules, semantics and SEO, you should use <img> and find some way to "bypass" and achieve the necessary customization.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.