AMP by Example
Playground

Introduction
Introduction

Components
Components

Advanced
Advanced

Samples & Templates
Samples & Templates

AMP Ads
AMP Ads

Edit in Playground

amp-selector

Edit on Github

Introduction

amp-selector lets the user choose from a list of options. The contents of the options isn't limited to just text.

Show Hidden Code Hide Code
<!doctype html>
<html ⚡>
<head>
  <meta charset="utf-8">
  <link rel="canonical" href="https://ampbyexample.com/components/amp-selector/">
  <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
  <script async src="https://cdn.ampproject.org/v0.js"></script>

Setup

Import the amp-selector component.

  <script async custom-element="amp-selector" src="https://cdn.ampproject.org/v0/amp-selector-0.1.js"></script>
Show Hidden Code Hide Code
  <script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
  <style amp-boilerplate>body{-webkit-animation:-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}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{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;-ms-animation:none;animation:none}</style></noscript>
<style amp-custom>
amp-selector {
  padding: 8px;
  width: 100%;
  line-height: 0;
}
amp-selector:not([disabled]) [option][selected]:not([disabled]) {
  outline-color: red;
}
/* disables build-in browser highlights */
amp-selector *:focus {
  outline: none;
}
amp-selector[disabled],
amp-selector [disabled] {
  opacity: 0.5;
}
amp-selector li {
  line-height: 24px;
  margin: 8px;
}
.divider {
  display: inline-block;
  border-right: 2px solid #333;
  margin: 0 16px;
  height: 40px;
}
</style>
</head>
<body>

Single Select

amp-selector defaults to single selection. The option is required for all selectable elements, for example, the divider in the following sample cannot be selected.

  <amp-selector layout="container">
    <amp-img src="/img/landscape_sea_300x199.jpg" width="60" height="40" option="1"></amp-img>
    <amp-img src="/img/landscape_desert_300x200.jpg" width="60" height="40" option="2"></amp-img>
    <div class="divider"></div>
    <amp-img src="/img/landscape_ship_300x200.jpg" width="60" height="40" option="3"></amp-img>
    <amp-img src="/img/landscape_village_300x200.jpg" width="60" height="40" option="4"></amp-img>
  </amp-selector>

Multi Select

Add the attribute multiple to enable multi selection.

  <amp-selector layout="container" multiple>
    <amp-img src="/img/landscape_sea_300x199.jpg" width="60" height="40" option="1"></amp-img>
    <amp-img src="/img/landscape_desert_300x200.jpg" width="60" height="40" option="2"></amp-img>
    <amp-img src="/img/landscape_ship_300x200.jpg" width="60" height="40" option="3"></amp-img>
    <amp-img src="/img/landscape_village_300x200.jpg" width="60" height="40" option="4"></amp-img>
  </amp-selector>

Attributes

The following attributes are supported for select elements:

  • disabled: disable an element.
  • selected: pre-select an element.
  <amp-selector layout="container" multiple>
    <amp-img src="/img/landscape_sea_300x199.jpg" width="60" height="40" option="1"></amp-img>
    <amp-img src="/img/landscape_desert_300x200.jpg" width="60" height="40" option="2" selected></amp-img>
    <amp-img src="/img/landscape_ship_300x200.jpg" width="60" height="40" option="3"></amp-img>
    <amp-img src="/img/landscape_village_300x200.jpg" width="60" height="40" option="4" disabled></amp-img>
  </amp-selector>

Disabled

Add the attribute disabled to disable the whole selector.

  <amp-selector layout="container" disabled>
    <amp-img src="/img/landscape_sea_300x199.jpg" width="60" height="40"></amp-img>
    <amp-img src="/img/landscape_desert_300x200.jpg" width="60" height="40"></amp-img>
    <amp-img src="/img/landscape_ship_300x200.jpg" width="60" height="40"></amp-img>
    <amp-img src="/img/landscape_village_300x200.jpg" width="60" height="40"></amp-img>
  </amp-selector>

Forms

Inside a form, amp-selector behaves like a radio-button/checkbox group and submits the selected value(s) against the name of the amp-selector.

  <form action="#" method="get" target="_top">
    <amp-selector layout="container" name="form-select" multiple>
      <ul>
        <li option="a">Option A</li>
        <li option="b">Option B</li>
        <li option="c">Option C</li>
        <li option="na" selected>N/A</li>
      </ul>
    </amp-selector>
    <input type="submit" value="Select" class="button button-primary">
  </form>
  • Option A
  • Option B
  • Option C
  • N/A

Next up:

</body>
</html>