69

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?

7 Answers 7

90

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" />

1
  • 8
    I used to do this when I was like 9 with <font color="#FFFFFF"><img ...></font>. Man those were the days.
    – BoltClock
    Commented Aug 18, 2011 at 3:59
17

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">

1
  • 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.
    Commented Feb 28, 2018 at 1: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.

4

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" />

3
  • does the OP had asked for a 'pure' css solution ?
    – hassan
    Commented Aug 21, 2017 at 9:17
  • also do you see that I had mentioned that's my solution is a work-around ?
    – hassan
    Commented Aug 21, 2017 at 9:18
  • 4
    why not ? for sure someone on the planet will make a use of your alternative solution :-)
    – hassan
    Commented Aug 21, 2017 at 9:20
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 - " />

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.

3
  • 5
    This does not focus on the alternative text, but instead on any image element that contains alt-attribute. Commented Jun 5, 2014 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
    Commented Jun 18, 2014 at 9: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? Commented Jun 19, 2014 at 9:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.