Question

As per the default behavior, alt attribute is rendered first time just before the image rendering. I am displaying 25 images in a grid so it looks bit awkward as all alt attributes are displayed first.

Is it possible to hide alt attributes in Firefox?

Note: alt attributes contain dynamic names in my case.

Was it helpful?

Solution

The way to prevent alt attribute values from being displayed is to remove the attribute.

The meaning of an alt attribute (not tag) is that it specifies an alternative, a substitute for the image, in situations where the image is not displayed. So if you want to hide it when the image has not yet been loaded, you are asking for behavior that contradicts the very meaning of the attribute.

You can however make the alt text invisible (with the usual CSS Caveats) on Firefox by setting e.g.

img { background: white; color: white; }

in CSS. This implies that the alt texts are invisible also in case the browser never gets the image, or the browser has been configured not to display images.

OTHER TIPS

After trying all the other methods here, I found this method works best which makes the text transparent until the image loads:

.yourClass img {
    color: transparent;
}

From the reference of all the above answers, I figured out best one is to use

img:-moz-loading {
  visibility: hidden;
}

Suppose there is no image and we use color as white or transparent then alt attribute no more use so, we need this attribute if there is no image to show which image here to load and to show alternative text to display.

In addition to setting to:

img { 
  background: white; 
  color: white;
}

I like to disable Firefox's default image behavoir as well:

img:-moz-loading {
  visibility: hidden;
}

Could you place the dynamic names in the title attribute? You could try a black background or black background image; maybe Firefox still uses a black text color. Maybe img { color: white; } would do?

If you don't mind adding a little extra, here it is:

<img src = "283414_2114217089554_728204_nn.jpg" onload="this.setAttribute('alt','this is the alt of my image');" />

Hope that helps... :))

Rather than worrying about the alt function, you can give all your images a common class, say image-to-show and create a loading div absolutely positioned over this image. So, when the page loads, you only show the loading div, with a loading gif, something like this:

// show loading image
$('.loader').show();

Once the image is loaded, you can hide the div and show the image.

// main image loaded ?
$('.image-to-show').load(function(){
  // hide/remove the loading image
  $('.loader').hide();
});

You can further enhance this code by using specific image ID's. Another, cleaner way to do it would be to set data-loading to true for the images that are loading and once the images are loaded, set $('.image-to-show').data('loading', false)

There are multiple ways of tackling this, let me know if you need further clarification.

I'd start by adding this CSS, which will hide all images with alt text (not display: none because we want to undo this and we won't know what to undo to):

img[alt] {
    width: 0;
    height: 0;
}

And then showing it once it's all loaded (this uses jQuery):

$(document).ready(function() {
    $('img[alt]').on('load', function() {
        this.style.width = 'auto';
        this.style.height = 'auto';
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top