Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide alt text using CSS when the image is not present?

Tags:

html

css

Is it possible to hide the alt text using CSS in all the browsers,

I tried with color:transparent, it is working in all browsers except IE.

Is it possible to do it in IE using CSS?. Thanks in advance for any help.

like image 897
Mohamed Hussain Avatar asked Mar 30 '16 10:03

Mohamed Hussain


5 Answers

The kellum method:

.hide-text {
    text-indent: 100%;
    white-space: nowrap;
    overflow: hidden;
}

Also don't have the performance drawback of -9999px method where browser draw a box with size of 9999px.

Further explanation: replacing-the-9999px-hack-new-image-replacement

like image 141
Raynal Gobel Avatar answered Oct 13 '22 11:10

Raynal Gobel


How about using font-size: 0? It works in hiding alt text before image loads or if image src URL is not available.

like image 40
TobinWebsites.com Avatar answered Oct 13 '22 11:10

TobinWebsites.com


You can use text-indent:-9999px

HTML

<img src="images/test.jpg" alt="alternative">

CSS

img{
  text-indent:-9999px
}

Demo

like image 7
Gaurav Aggarwal Avatar answered Oct 13 '22 12:10

Gaurav Aggarwal


Instead of using color:transparent; use display:none;.

But you should not use this technique.

As @Quentin said in the comment:

If the text isn't good enough for humans then it isn't good enough for search engines.

You should not hide the alt text. Google knows this only used for SEO purpose and nothing important and will penalize for such. And you may be blacklisted on Google Search Engine.

So, don't use them just for SEO purpose.

like image 2
Bhojendra Rauniyar Avatar answered Oct 13 '22 10:10

Bhojendra Rauniyar


You could also use the onerror callback to set the display property to none:

<img src="images/test.txt" onerror="this.style.display='none';">
like image 2
Martin Brandl Avatar answered Oct 13 '22 11:10

Martin Brandl