AMP
  • websites

amp-dynamic-css-classes

Introduction

AMP's dynamic CSS classes provided by the amp-dynamic-css-class enable boolean logic for a handful of conditions

Setup

Include the amp-dynamic-css-classes extended component.

<script async custom-element="amp-dynamic-css-classes" src="https://cdn.ampproject.org/v0/amp-dynamic-css-classes-0.1.js"></script>

For the purpose of this sample, we're creating some helper CSS classes that use the CSS rules defined by amp-dynamic-css-classes. These allow us to more succinctly refer to the conditions in our code below.

<style amp-custom>
  :root {
    --space-2: 1rem;   /* 16px */
  }
  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;
  }
  .dynamic-css {
    padding: var(--space-2);
  }
</style>

Referrer

Using these referrer classes can allow you to conditionally include elements. You do not need to include a call to the amp-dynamic-css-classes component to initialize the classes on the body element.

You were referred here or embedded by Pinterest!
You were referred here or embedded by Twitter!
You were referred here or embedded by Google!
You came here directly! Cool :)
You came here directly! Cool :)
<div class="if-pinterest dynamic-css">You were referred here or embedded by Pinterest!</div>
<div class="if-twitter dynamic-css">You were referred here or embedded by Twitter!</div>
<div class="if-google dynamic-css">You were referred here or embedded by Google!</div>
<div class="if-ampbyexample dynamic-css">You came here directly!  Cool :)</div>
<div class="if-localhost dynamic-css">You came here directly!  Cool :)</div>

Viewer

The 'viewer' classes enable you to style the page based on whether it's embedded inside an AMP viewer. Try it out on the Google AMP Viewer. Note this works only on a mobile device or in your browser's mobile emulation mode.

Hey! You're not visiting within the context of an AMP Viewer!
Ahoy! Since you're coming from a viewer.
<div class="if-not-viewer dynamic-css">Hey! You're not visiting within the context of an AMP Viewer!</div>
<div class="if-viewer dynamic-css">Ahoy! Since you're coming from a viewer.</div>
Bạn cần được giải thích thêm?

Nếu bạn vẫn còn thắc mắc sau khi đọc hướng dẫn trên trang này, hãy liên hệ với những người dùng AMP khác để thảo luận về trường hợp sử dụng cụ thể của bạn.

Truy cập Stack Overflow
Một tính năng chưa được giải thích?

Dự án AMP đặc biệt khuyến khích sự tham gia và đóng góp của bạn! Chúng tôi hi vọng bạn sẽ trở thành một người tham gia tích cực trong cộng đồng mã nguồn mở của chúng tôi, nhưng chúng tôi cũng chào mừng các đóng góp đơn lẻ về vấn đề mà bạn đặc biệt quan tâm.

Chỉnh sửa mẫu trên GitHub