64

I have a dark blue page and when the image is loading (or missing) the ALT text is black and difficult to read (in FF).

Could I style it (with CSS) to be white?

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551

7 Answers7

88

Setting the img tag color works

img {color:#fff}

http://jsfiddle.net/YEkAt/

body {background:#000022}
img {color:#fff}
<img src="http://badsrc.com/blah" alt="BLAH BLAH BLAH" />
mplungjan
  • 169,008
  • 28
  • 173
  • 236
MikeM
  • 27,227
  • 4
  • 64
  • 80
16

Sure you can!

http://jsfiddle.net/VfTGW/

I do this as a fallback for header logo images, I think some versions of IE will not abide. Edit: Or Chrome apparently - I don't even see alt text in the demo(?). Firefox works well however.

img {
  color: green;
  font: 40px Impact;
}
<img src="404" alt="Alt Text">
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
  • 1
    As of 2018, I'm seeing it fine in Chrome too. One thing I notice is that text-decoration isn't applicable in any browser I've checked. – Jacob C. Feb 28 '18 at 01:06
5

You cant style the alt attribute directly in css. However the alt will inherit the styles of the item the alt is on or what is inherited by its parent:

    <div style="background-color:black; height: 50px; width: 50px; color:white;">
    <img src="ouch" alt="here i am"/>
    <div>

In the above example, the alt text will be black. However with the color:white the alt text is white.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
Jake Dempsey
  • 6,264
  • 1
  • 30
  • 25
3

as this question is the first result at search engines

There are a problem with the selected -and right by the way- solution, is that if you want to add style that will apply to images like ( borders for example ) .

for example :

img {
  color:#fff;
  border: 1px solid black;
  padding: 5px;
  background-color: #ccc;
}
<img src="http://badsrc.com/blah" alt="BLAH BLAH BLAH" /> <hr />
<img src="https://cdn4.iconfinder.com/data/icons/miu-square-flat-social/60/stackoverflow-square-social-media-128.png" alt="BLAH BLAH BLAH" />

as you can see, all of images will apply the same style


there is another approach to easily work around such an issue, using onerror and injecting some special class to deal with the interrupted images :

.invalidImageSrc {
  color:#fff;
  border: 1px solid black;
  padding: 5px;
  background-color: #ccc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

<img onerror="$(this).addClass('invalidImageSrc')" src="http://badsrc.com/blah" alt="BLAH BLAH BLAH" /> <hr />
<img onerror="$(this).addClass('invalidImageSrc')" src="https://cdn4.iconfinder.com/data/icons/miu-square-flat-social/60/stackoverflow-square-social-media-128.png" alt="BLAH BLAH BLAH" />
hassan
  • 7,812
  • 2
  • 25
  • 36
2

In Firefox and Chrome (and possibly more) we can insert the string ‘( .... )’ into the alt text of an image that hasn’t loaded.

img {
  font-style: italic;
  color: #c00;
}

img:after {
  content: " (Image - Right click to reload if not loaded)";
}

img::after {
  content: " (Image - Right click to reload if not loaded)";
}
<img alt="Alt text - " />
mplungjan
  • 169,008
  • 28
  • 173
  • 236
searching9x
  • 1,515
  • 16
  • 16
0

Yes, image alt text can be styled using any style property you use for regular text, such as font-size, font-weight, line-height, color, background-color,etc. The line-height (of text) or vertical-align (if display:table-cell used) could also be used to vertically align alt text within an image element or image wrapping container, i.e. div.

To prevent accessibility issues regarding contrast, and inheriting the browser's default black font color when you've set a dark blue background-color, always set both the color of your font and its background-color at the same time.

for some more useful info, visit Alternate text for background images or The Ultimate Guide to Styled ALT Text in Email

-3

You can use img[alt] {styles} to style only the alternative text.

katsampu
  • 395
  • 3
  • 3
  • 5
    This does not focus on the alternative text, but instead on any image element that contains alt-attribute. – Mikko Tapionlinna Jun 05 '14 at 12:57
  • 1
    You can use a class on image or a regular expression on src to target the specific image. Simple css my friend. – katsampu Jun 18 '14 at 09:57
  • 3
    katsampu, the original question asked about targeting the alt text, In your answer you are talking about "style only the alternative text". This statement is incorrect. This does not target the text, but the image element around it. If I set img[alt]{border:1px solid red;padding:10px;} those changes effect the image itself, not the alt text. Some changes may be inherited into the alt text too (depending on browser implementation). But it does not only style the alt text like you said. It styles only image elements that have alt text attribute. See the difference? – Mikko Tapionlinna Jun 19 '14 at 09:58