網站導覽

v2.1.0
表單

下拉選單

原生下拉選單,選項清單採用與介面一致的樣式。

本頁內容

直接將 .select 套用到原生 <select>。瀏覽器會保留鍵盤操作、輸入文字快速定位選項的功能及表單值。這個類別會為控制項套用內凹樣式並加上低調的箭頭,兩種主題下都清楚可見。瀏覽器支援可自訂的下拉選單時,選項清單會採用與介面其他部分一致的樣式。

下拉選單

<label class="field">  Framework <select class="select">    <option class="select-option">Astro</option>    <option class="select-option">Next.js</option>    <option class="select-option">SvelteKit</option>    <option class="select-option" disabled>Nuxt</option>  </select></label>
<label className="field">  Framework <select className="select">    <option className="select-option">Astro</option>    <option className="select-option">Next.js</option>    <option className="select-option">SvelteKit</option>    <option className="select-option" disabled>Nuxt</option>  </select></label>
<label class="field">  Framework <select class="select">    <option class="select-option">Astro</option>    <option class="select-option">Next.js</option>    <option class="select-option">SvelteKit</option>    <option class="select-option" disabled>Nuxt</option>  </select></label>
<template>  <label class="field">    Framework <select class="select">      <option class="select-option">Astro</option>      <option class="select-option">Next.js</option>      <option class="select-option">SvelteKit</option>      <option class="select-option" disabled>Nuxt</option>    </select>  </label></template>

#選項清單

使用 appearance: base-select 後,原本由作業系統繪製的選項清單會改由瀏覽器以 ::picker(select) 呈現。這個浮出面板會以控制項為錨點,碰到檢視區邊緣時會翻轉方向。樣式表會套用彈出面板的樣式,面板圓角半徑等於選項列的圓角半徑加上兩者之間的 4px 間距。請在每個 <option> 上加上 select-option:

  • 指標停留或以方向鍵移到的選項列會套用膠囊標籤的底色。
  • 目前選取的選項會在尾端顯示由 ::checkmark 繪製的勾號。
  • 控制項的箭頭由 ::picker-icon 繪製,清單開啟時會翻轉。

瀏覽器若不支援可自訂的下拉選單,會忽略這些樣式,照常顯示平台原生清單。可沿用相同的標記,保留 select-option 也不影響原生行為。

#含圖示的選項

為每個選項提供文字標籤和值。可以在文字旁放置裝飾圖示。可自訂的選擇控制項會透過 <selectedcontent> 把選取項目顯示在收合的控制項中。其他瀏覽器的原生選擇器仍會顯示文字標籤,並提交相同的值。

通知方式

<form class="flex flex-col items-start gap-md">  <label class="field">    Delivery method <select class="select" name="delivery">      <button type="button"><selectedcontent></selectedcontent></button>      <option class="select-option" value="email">        <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <rect x="2" y="4" width="12" height="8" rx="1" /><path d="M2.5 5L7.8824 8.9144A0.2 0.2 0 0 0 8.1176 8.9144L13.5 5" />        </svg> Email      </option>      <option class="select-option" value="sms">        <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">          <path d="M3.5 2.5L12.5 2.5A1 1 0 0 1 13.5 3.5L13.5 10A1 1 0 0 1 12.5 11L7.777 11A1 1 0 0 0 7.2625 11.1425L4.0145 13.0913A1 1 0 0 1 2.5 12.2338L2.5 3.5A1 1 0 0 1 3.5 2.5Z" />        </svg> Text message      </option>    </select>  </label>  <button type="submit" class="btn btn-sm">Save</button>  <output aria-live="polite"></output></form><script>const form = document.querySelector('form')form.addEventListener('submit', event => {  event.preventDefault()  form.querySelector('output').value = new FormData(form).get('delivery')})</script>

#凸起樣式

在工具列或篩選列中,使用 select-raised 讓下拉選單以凸起的控制項樣式與按鈕並排。

凸起樣式

<select class="select select-raised select-sm" aria-label="Sort">  <option class="select-option">Newest first</option>  <option class="select-option">Oldest first</option>  <option class="select-option">Name</option></select><button type="button" class="btn btn-sm">Export</button>
<>  <select className="select select-raised select-sm" aria-label="Sort">    <option className="select-option">Newest first</option>    <option className="select-option">Oldest first</option>    <option className="select-option">Name</option>  </select>  <button type="button" className="btn btn-sm">Export</button></>
<select class="select select-raised select-sm" aria-label="Sort">  <option class="select-option">Newest first</option>  <option class="select-option">Oldest first</option>  <option class="select-option">Name</option></select><button type="button" class="btn btn-sm">Export</button>
<template>  <select class="select select-raised select-sm" aria-label="Sort">    <option class="select-option">Newest first</option>    <option class="select-option">Oldest first</option>    <option class="select-option">Name</option>  </select>  <button type="button" class="btn btn-sm">Export</button></template>

#尺寸

內凹和凸起樣式的下拉選單都提供 32、38 和 44px 三種高度,與按鈕和輸入欄位一致。

尺寸

<select class="select select-sm" aria-label="Small">  <option class="select-option">Small</option></select><select class="select" aria-label="Medium">  <option class="select-option">Medium</option></select><select class="select select-lg" aria-label="Large">  <option class="select-option">Large</option></select>
<>  <select className="select select-sm" aria-label="Small">    <option className="select-option">Small</option>  </select>  <select className="select" aria-label="Medium">    <option className="select-option">Medium</option>  </select>  <select className="select select-lg" aria-label="Large">    <option className="select-option">Large</option>  </select></>
<select class="select select-sm" aria-label="Small">  <option class="select-option">Small</option></select><select class="select" aria-label="Medium">  <option class="select-option">Medium</option></select><select class="select select-lg" aria-label="Large">  <option class="select-option">Large</option></select>
<template>  <select class="select select-sm" aria-label="Small">    <option class="select-option">Small</option>  </select>  <select class="select" aria-label="Medium">    <option class="select-option">Medium</option>  </select>  <select class="select select-lg" aria-label="Large">    <option class="select-option">Large</option>  </select></template>

#停用

停用

<select class="select" aria-label="Region" disabled>  <option class="select-option">Asia Pacific</option></select>
<select className="select" aria-label="Region" disabled>  <option className="select-option">Asia Pacific</option></select>
<select class="select" aria-label="Region" disabled>  <option class="select-option">Asia Pacific</option></select>
<template>  <select class="select" aria-label="Region" disabled>    <option class="select-option">Asia Pacific</option>  </select></template>

#類別

類別說明
select原生 select 控制項,外觀為帶有折角箭頭的凹槽
select-option可自訂選項清單中的一列。指標移入或以鍵盤移至該列時,該列會套用膠囊標籤的底色。目前選取的選項會以勾號標示。
select-raised適合放在工具列或篩選列中與按鈕並排的凸起 select。未指定尺寸時,高度為 38px。搭配 select 使用
select-sm高度為 32px,預設高度為 38px(與按鈕同高)搭配 select 使用
select-lg高度為 44px,預設高度為 38px(與按鈕同高)搭配 select 使用

#原始碼

@master/ui/components/select.css
/* * Select: a native <select> drawn as a well, or as a raised control in toolbars. * Where the browser supports appearance: base-select, the option list becomes a floating sheet in the same * material: options are rows with a concentric corner and the current one carries a check. Elsewhere the * select keeps the platform's own list. */@layer components.ui {  .select {    --select-height: 2.375rem;    --select-padding-x: .75rem;    --select-font-size: .875rem;    --select-radius: var(--radius-control);    -webkit-tap-highlight-color: transparent;    /* A neutral mid-grey chevron reads on moonstone and obsidian alike. */    --select-indicator: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='%23888' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5' transform='rotate(90 8 8)'/%3E%3C/svg%3E");    min-width: 0;    min-height: var(--select-height);    max-width: 100%;    margin: 0;    padding-block: 0;    padding-inline: var(--select-padding-x) calc(var(--select-padding-x) + 1.25rem);    border: 0;    border-radius: calc(var(--select-radius) * var(--corner-scale));    corner-shape: var(--corner-shape);    background: var(--select-indicator) no-repeat right calc(var(--select-padding-x) - .125rem) center / 14px, var(--color-surface-inset);    color: var(--color-text-strong);    box-shadow: var(--shadow-pressed);    font: 400 var(--select-font-size)/1.45 var(--font-family-sans);    font-feature-settings: inherit;    text-overflow: ellipsis;    appearance: none;    cursor: pointer;    transition-property: box-shadow, background-color;    transition-duration: var(--duration-quick);    transition-timing-function: var(--easing-glide);    @media (hover: hover) {      &:hover:not(:focus-visible, :disabled) {        background-color: var(--color-surface-inset-hover);      }    }    /*     * Focused, or open. :open stays inside :is(), which forgives a browser that does not know it; a :not(:open)     * on the hover rule would drop that rule there. This rule matches hover's weight and follows it instead,     * so an open select keeps its raised face under the pointer.     */    &:is(:focus-visible, :open):not(:disabled) {      outline: none;      background-color: var(--color-surface-raised);      box-shadow: inset 0 0 0 .5px var(--color-line-mark), 0 0 0 .5px var(--color-line-mark), 0 0 0 3.5px var(--color-focus-halo);    }    /*     * An error keeps its boundary while focused or open, and the halo takes the danger tone, as a field's does.     * :user-invalid reports a required select left on its empty option once the reader has tried, without script.     */    &:is([aria-invalid="true"], :user-invalid) {      box-shadow: var(--shadow-pressed), inset 0 0 0 .5px var(--color-danger), 0 0 0 .5px color-mix(in oklab, var(--color-danger) 45%, transparent);      &:is(:focus-visible, :open):not(:disabled) {        box-shadow: inset 0 0 0 .5px var(--color-danger), 0 0 0 .5px var(--color-danger), 0 0 0 3.5px color-mix(in oklab, var(--color-danger) 15%, transparent);      }    }    &:disabled {      color: var(--color-text-disabled);      cursor: not-allowed;    }    /* Raised: a control on a sheet, for toolbars and filters, matching the button beside it. */    &.select-raised {      background: var(--select-indicator) no-repeat right calc(var(--select-padding-x) - .125rem) center / 14px, linear-gradient(180deg, var(--color-surface-raised), var(--color-surface-raised-low)) var(--color-surface-raised-low);      box-shadow: var(--shadow-raised);      /* Its face is a gradient: swap backgrounds instantly, as a button does. */      transition-property: box-shadow;      font-weight: var(--font-weight-control);      @media (hover: hover) {        &:hover:not(:focus-visible, :disabled) {          box-shadow: var(--shadow-raised-hover);        }      }      &:is(:focus-visible, :open):not(:disabled) {        box-shadow: var(--shadow-raised);      }      &:focus-visible {        @apply --focus-ring;      }      /* Raised and wrong: the face keeps its depth, under the pointer too, and its edge takes the danger color. */      &:is([aria-invalid="true"], :user-invalid):not(:disabled) {        box-shadow: var(--shadow-raised), inset 0 0 0 .5px var(--color-danger), 0 0 0 .5px color-mix(in oklab, var(--color-danger) 45%, transparent);      }      &:disabled {        background-color: var(--color-surface-inset);        background-image: var(--select-indicator);        box-shadow: var(--shadow-flat);      }    }    /*     * The chevron stands at the end of the words, whichever way they run. It follows the raised face, whose     * background shorthand would otherwise put the chevron back on the right.     */    &:dir(rtl) {      background-position: left calc(var(--select-padding-x) - .125rem) center, 0 0;    }    /* A select is as tall as the button and the field beside it: 32, 38 and 44px. */    &.select-sm {      --select-height: 2rem;      --select-padding-x: .625rem;      --select-font-size: .8125rem;      --select-radius: var(--radius-control-sm);    }    &.select-lg {      --select-height: 2.75rem;      --select-padding-x: .875rem;      --select-font-size: 1rem;      --select-radius: var(--radius-control-lg);    }    @media (forced-colors: active) {      outline: 1px solid FieldText;      outline-offset: -1px;      /* As heavy as the focused rule above, whose outline: none would otherwise leave focus unseen. */      &:is(:focus-visible, :open):not(:disabled) {        outline: 2px solid Highlight;        outline-offset: -2px;      }      /* The danger edge and the halo are shadows, which forced colors drop: the outline says it instead, as a field's does. */      &:is([aria-invalid="true"], :user-invalid):not(:disabled) {        outline-color: Mark;      }      &:disabled {        outline-color: GrayText;      }    }  }  /*   * The customizable select. The control becomes a flex row whose chevron is ::picker-icon, and the list is   * ::picker(select): a popover the browser anchors to the control and flips at the viewport edge.   */  @supports (appearance: base-select) {    .select {      display: inline-flex;      align-items: center;      justify-content: space-between;      gap: .5rem;      padding-block: 0;      padding-inline: var(--select-padding-x) calc(var(--select-padding-x) - .125rem);      background: var(--color-surface-inset);      appearance: base-select;      &::picker-icon {        content: '';        flex: none;        width: 14px;        height: 14px;        background: var(--color-text-muted);        mask: var(--select-indicator) center / contain no-repeat;        transition: rotate var(--duration-base) var(--easing-glide);      }      &:open::picker-icon {        rotate: x 180deg;      }      &:disabled::picker-icon {        background: var(--color-text-disabled);      }      &.select-raised {        @apply --surface-sheet;        &:disabled {          background: var(--color-surface-inset);        }      }      /* The list: a floating sheet 6px from the control, whose corner is a row's corner plus its 4px inset. */      /* The list is as wide as its longest option up to a reading width, and never wider than the viewport. */      &::picker(select) {        max-inline-size: min(24rem, calc(100vw - 1rem));        margin: 6px 0;        padding: 4px;        /* Scrolling to the end of a long list stops there, as a menu's does, and never scrolls the page under it. */        overscroll-behavior: contain;        border: 0;        border-radius: calc(var(--radius-control-sm) * var(--corner-scale) + 4px);        corner-shape: var(--corner-shape);        background: var(--color-surface-floating);        color: var(--color-text-strong);        box-shadow: var(--shadow-floating);        font: 400 var(--select-font-size)/1.2 var(--font-family-sans);        font-feature-settings: inherit;        scrollbar-width: thin;        scrollbar-color: var(--color-line-strong) transparent;        appearance: base-select;        opacity: 0;        scale: .97;        transform-origin: top;        transition-property: opacity, scale, overlay, display;        transition-duration: var(--duration-quick);        transition-timing-function: var(--easing-glide);        transition-behavior: allow-discrete;      }      &:open::picker(select) {        opacity: 1;        scale: 1;        @starting-style {          opacity: 0;          scale: .97;        }      }      @media (prefers-reduced-motion: reduce) {        &::picker-icon {          transition: none;        }        &::picker(select), &:open::picker(select) {          scale: none;          @starting-style {            scale: none;          }        }      }      @media (forced-colors: active) {        &::picker-icon {          background: FieldText;        }        /* Written after the disabled chevron, which is as heavy, so it takes a system color too. */        &:disabled::picker-icon {          background: GrayText;        }        /* An outline, not a border: the list keeps its size and its 4px inset. */        &::picker(select) {          outline: 1px solid CanvasText;          outline-offset: -1px;        }      }    }    /*     * An option in the list. The row under the pointer or the keys takes a chip tone; the current one a check.     * Keyboard focus adds an ink outline inside the row, as in a menu, so focus and hover stay different facts.     */    .select-option {      display: flex;      align-items: center;      gap: .5rem;      min-height: 2rem;      /* A long option wraps inside the list, as a listbox's does; a row of one line keeps its 32px. */      padding: .25rem .5rem;      border-radius: calc(var(--radius-control-sm) * var(--corner-scale));      corner-shape: var(--corner-shape);      outline: none;      background: transparent;      color: var(--color-text-strong);      white-space: normal;      overflow-wrap: anywhere;      cursor: pointer;      &::checkmark {        content: '';        order: 1;        flex: none;        width: 14px;        height: 14px;        margin-inline-start: auto;        background: currentColor;        mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M3 8L6.3532 11.3532A0.2 0.2 0 0 0 6.6412 11.3479L13 4.5'/%3E%3C/svg%3E") center / contain no-repeat;      }      &:focus-visible:not(:disabled) {        background: var(--color-surface-chip);        outline: 2px solid var(--color-focus);        outline-offset: -2px;      }      @media (hover: hover) {        &:hover:not(:disabled) {          background: var(--color-surface-chip);        }      }      /* Pressed: a step deeper than the pointer's tone. */      &:active:not(:disabled) {        background: var(--color-surface-inset-hover);      }      &:disabled {        color: var(--color-text-disabled);        cursor: not-allowed;      }      @media (pointer: coarse) {        min-height: 2.75rem;      }      @media (forced-colors: active) {        &::checkmark {          background: CanvasText;        }        /* A highlighted row's check takes the row's text color, as its words do. */        &:focus-visible:not(:disabled) {          background: Highlight;          color: HighlightText;          &::checkmark {            background: HighlightText;          }        }        @media (hover: hover) {          &:hover:not(:disabled) {            background: Highlight;            color: HighlightText;            &::checkmark {              background: HighlightText;            }          }        }        &:disabled {          color: GrayText;        }      }    }  }}