tags:

views:

46

answers:

1

For the following image html:

<img src="/some.png" alt="something" />

In CSS, you can target an img element with a specific alt value:

img[alt="something"] {
  width: 5em;
}

How can I select all img elements with text in the alt attribute? I was thinking of something like:

img[alt="*"]

but that doesn't seem to work. Any ideas?

+6  A: 

You can target elements which have an alt attribute like this

img[alt]

I believe this will also match those with an empty alt attribute however. I don't think you can specifically target those with a nonempty value. You could write a rule to match those with empty values and undo ignore the rules which would otherwise be applied (via !important) although that gets messy.

Michael Mior
Yes, that will do the job. You do not need to filter out attributes as by specifications no empty attributes are valid
Dmitri Farkov
Agreed @Dmitri. Although I do often see people leaving empty alt attributes to avoid warnings, so I thought I would mention. However, I think your point of it being against standards is more important.
Michael Mior