Search code examples
amp-html

Can I use :target


In the AMP Disallowed Styles, they say:

Pseudo-selectors, pseudo-classes and pseudo-elements are only allowed in selectors that contain tag names and those tag names must not start with amp-. Example OK: a:hover.

#thing:target {
  some style
}

Although the AMP test is OK, I have a doubt about such above CSS code. Can I use it?


Solution

  • I guess it's okay to use :targettag as long as it is not starting with amp- as stated in the documentation you provided. This SO answer shows that the developer uses :target pseudoclass.

    #slide-in-menu {
      transform: translateX(-100%);
      transition: transform .2s ease-in-out;
      ... additional required styles ...
    }
    #slide-in-menu:target {
      transform: translateX(0);
    }
    

    ...:target is much more effective and it works well for carousels too.