Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Is it possible to style the placeholder text of a <img> element?

Hello!

Say for example an image link breaks, or an image isn't included/available; the image placeholder text will appear instead.

My problem: It looks terrible.

How can I style it to my liking??

<img src="borken-image.jpg" placeholder="style me">

1 Answer

From what I can tell using a placeholder attribute won't work. However creating a JavaScript function using the 'onerror' attribute could, but it may not be supported in all browsers.

Like this...

function ImgError(source){
source.src = "/images/noimage.gif";
source.onerror = "";
return true;
}

<img src="someimage.png" onerror="ImgError(this);"/>

I personally haven't tried this, and just learned of the 'onerror' attribute recently. Good luck, and I hope it works!