0

I came to know that we cannot have an alt for a css generated image.There are solutions that say by having title atribute we can get the alt effect only on hovering on the image,however when we disable the css we will not able to see that text in place of the image.In my case I need the text to be appeared even when the css is disabled .Is there any workaround for getting the text visible when the css is disabled.

    <span class="myimageclass">
    hi
    </span>
    <style>
    .myimageclass
    {
    height: 100px;
    width: 200px;

    background-image:url('http://cdn.osxdaily.com/wp- 
    content/uploads/2011/10/NSTexturedFullScreenBackgroundColor.png');
    color:red;
    overflow: hidden;
    text-indent: -9999px;



    }
    </style>

Thanks, Balaji.

user2345
  • 37
  • 8
  • 1
    yes alt is only for img tag and you cannot provide that for a CSS generated image. However, you can use javascript to detect the absence of CSS and then enter a text if CSS is disabled. – bbh Jun 04 '15 at 06:21
  • thanks for your reply, so going with javascript is the only alternative. – user2345 Jun 04 '15 at 06:26
  • I am afraid so, because this is CSS generated image, that might be best option now. Once CSS is enabled, you will need another mechanism to display the alt text and javascript can play that role. – bbh Jun 04 '15 at 06:27
  • ok i will try to follow the way you showed, thanks :) – user2345 Jun 04 '15 at 06:28

2 Answers2

2

You can use text-indent and overflow: hidden.

This is not flexible method but I hope you can use it.

.image {
  width: 200px;
  height: 100px;
  display: block; /* it needs for inline elements like span */
  background: url(http://cdn.osxdaily.com/wp-content/uploads/2011/10/NSTexturedFullScreenBackgroundColor.png);
  overflow: hidden;
  text-indent: -9999px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="image js-image">
  Image alt
</div>

<button class="js-button">On/Off styles</button>

<script>
  $('.js-button').click(function() {
    $('.js-image').toggleClass('image');
  });
</script>
Philip Dernovoy
  • 1,169
  • 6
  • 17
1

@balaji, this is already an SO thread that takes care of CSS in a page, you can pick something from here and fine tune for your needs: How to determine if CSS has been loaded?

Community
  • 1
  • 1
bbh
  • 489
  • 3
  • 13