Images used in Google AMP page is similar to how it is used in a standard html page, but only difference is the way the tag name is used with some additional properties. This chapter discusses these in detail.
Observe the syntaxes shown below −
<img src = ”example.jpg” width = ”300” height = ”250” alt = ”Example” ></img>
<amp-img src = "example.jpg" alt = "Example" height = "300" width = "250" ><//amp-img>
Note that the tag from img is changed to amp-img.
The reason behind changing img to amp-img is to have more control on the page layout and the network request made to load the image. Amp adds lazy loading to the image resource and prioritizes the loading as per other resources available on the page.
Observe the following code for a better understanding −
<!doctype html> <html amp lang = "en"> <head> <meta charset = "utf-8"> <script async src = "https://cdn.ampproject.org/v0.js"></script> <title>Google AMP - Image</title> <link rel = "canonical" href = "http://example.ampproject.org/articlemetadata.html"> <meta name = "viewport" content = "width = device-width, minimum-scale = 1,initialscale = 1"> <style amp-boilerplate> body{ -webkit-animation: -amp-start 8s steps(1,end) 0s1 normal both;-moz-animation: -amp-start 8s steps(1,end) 0s 1 normal both;-msanimation: - amp-start 8s steps(1,end) 0s 1 normal both;animation: -amp-start 8s steps(1,end) 0s 1 normal both } @-webkit-keyframes -ampstart{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -ampstart{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -ampstart{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -ampstart{from{visibility:hidden}to{visibility:visible}}@keyframes amp-start{from{visibility:hidden}to{visibility:visible}} </style> <noscript> <style amp-boilerplate> body{ -webkit-animation:none; -moz-animation:none; -msanimation:none; animation:none } </style> </noscript> </head> <body> <h1>Google AMP - Image Example</h1> <amp-img alt = "Beautiful Flower" src = "images/flower.jpg" width = "246" height = "205"> </amp-img> </body> </html>
When you executed the code shown above, you will find the result as shown below −
You can also make the image responsive by adding property layout = ”responsive” to amp-img tag as shown below
Observe the following code for a better understanding −
<amp-img alt = "Beautiful Flower" src = "images/flower.jpg" width = "246" height = "205" layout = "responsive"> </amp-img>
When you executed the code shown above, you will find the result as shown below −