Google AMP - Dynamic CSS Classes

The amp-dynamic-css-classes adds dynamic classes to the body tag. In this chapter, let us learn the details of this tag.

To work with amp-dynamic-css-classes, we need to add following script −

<script asynccustom-element="amp-dynamic-css-classes" 
   src = "">

There are two important classes that are taken care by amp-dynamic-css-classes −

  • amp-referrer-*
  • amp-viewer

Let us discuss each of them in detail.


These classes are set depending on how the users are coming. It means if the user is coming from Google, the referrer class related to Google will be set. The same applies true for Twitter and Pinterest.

The classes are available based on the type of the referrer.

For example, for Google the following classes will be added if the user clicks amp pages from Google search engine.

  • amp-referrer-www-google-com
  • amp-referrer-google-com
  • amp-referrer-com

Similarly there are classes available for Twitter, Pinterest, Linkedin etc.


Amp viewer is basically going to change the amp url to get the details from Google cache. If you search something in Google search, the carousel that is displayed will a have all the amp pages.

When you click them, they are redirected to the url with Google url as the prefix. The amp-viewer class will be set when the page is being viewed by the user in amp- viewer and using dynamic classes.

amp viewer

When you click the amp page the url which you get in the address bar is as follows −
amp viewer


<!doctype html>
<html amp lang = "en">
      <meta charset =  "utf-8">
      <script async src = ""></script>
      <title>Google AMP - Dynamic Css Classes</title>
      <link rel = "canonical" href = "">
      <meta name = "viewport" content = "width = device-width,minimum-scale = 1,initial-scale = 1">
      <style amp-boilerplate>
            -amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:
            -amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:
            -amp-start 8s steps(1,end) 0s 1 normal both;animation:
            -amp-start 8s steps(1,end) 0s 1 normal both
         <style amp-boilerplate>
            body {

      <script async custom-element = "amp-bind" 
         src = "">
      <script async custom-element = "amp-dynamic-css-classes" 
         src = "">
      <link rel = "stylesheet" href = "">
      <style amp-custom>
         body:not(.amp-referrer-pinterest-com) .if-pinterest,
         body:not(.amp-referrer-ampbyexample-com) .if-ampbyexample,
         body:not(.amp-referrer-google-com) .if-google,
         body:not(.amp-referrer-twitter-com) .if-twitter,
         body:not(.amp-referrer-linkedin-com) .if-linkedin,
         body:not(.amp-referrer-localhost) .if-localhost {
            display: none;
         body:not(.amp-viewer) .if-viewer,
            body.amp-viewer .if-not-viewer {
            display: none;
         p {
            padding: 1rem;
      <h3>Google AMP - Dynamic Css Classes</h3>
         <p class = "if-pinterest">You were referred here or embedded by Pinterest!</p>
         <p class = "if-twitter">You were referred here or embedded by Twitter!</p>
         <p class = "if-google">You were referred here or embedded by Google!</p>
         <p class = "if-ampbyexample">You came here directly! Cool :)</p>
         < class = "if-localhost">You came here directly! Cool :)</p>
         <p class = "if-not-viewer">Hey! You are not coming from amp viewer</p>
         <p class = "if-viewer">Hey! From amp viewer.</p>


amp dynamic css classes
Kickstart Your Career

Get certified by completing the course

Get Started