Search code examples
htmlalt

Does HTML attribute 'alt' work with numbers?


In this example a value of alt attribute is not demonstrated until it contains a number. So, this line does not work:

<img src="non-existent.jpg" alt="W3Schools.com" width="104" height="142">

but that one works:

<img src="non-existent.jpg" alt="WSchools.com" width="104" height="142">

Could you please explain why?

I use Google Chrome and Windows 8.1. Thank you in advance.


Solution

  • string present in alt is displayed only when the image fails to load, or not available. Even if a number is there alt will show it.

    What you are mentioning about not getting displayed is under one condition

    If the text fits the width of the image, it will be displayed, else it will not be visible.

    Both of your provided code works.

    <img src="non-existent.jpg" alt="W3Schools.com" width="104" height="142"> // works
    
    <img src="non-existent.jpg" alt="WSchools.com" width="104" height="142"> // works 
    
    <img src="non-existent.jpg" alt="W3535Schools.com" width="104" height="142"> //works
    

    Check this out

    Example