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.
In theory, if it is background-image it should not need alt, and vice versa. When to use IMG and when to use background?
– Bacco
@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!
– hugocsl
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".
– Bacco
@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– hugocsl
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
@Sam I believe the path will be right there...
– hugocsl
@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
@Bacco is true.
– Sam
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.
– Renan
@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
– hugocsl