hanumanDev
hanumanDev

Reputation: 6614

Can an alt attribute be set using CSS?

I'm setting an image using a <div> tag like this:

<div id="logo"></div>

and here's the CSS:

    #logo {
      background: url(../images/logo.png) no-repeat center;
      width: 180px;
      height: 82px;
      float: right; 
      padding: 15px 15px 0 10px;
      position: relative;
    }

How do I set an alt="" attribute for the image?

thanks!

Upvotes: 4

Views: 10367

Answers (2)

Oke Tega
Oke Tega

Reputation: 883

It is impossible to set an <img alt=""> with css. But this can be done with JavaScript Example

<div class="logo><img src="../image.png"></div>

document.getElementByTagName('img').alt = "Anything";

Upvotes: 6

Mitya
Mitya

Reputation: 34608

That's not an image in the HTML sense - which is the only type of image an alt attribute (not tag) can be applied to.

You can't apply attributes of any kind via CSS - it's for styling, not applying mark-up.

Upvotes: 3

Related Questions