ナビゲーション

v2.1.0
フォーム

ラジオボタン

複数の選択肢から 1 つを選ぶ、小さな円形のコントロール。ネイティブのラジオボタンに適用します。

このページの内容

ラジオボタンは小さな円形のコントロールで、選択すると色が付き、中央に点が表示されます。丸いラジオボタンは一つだけ選べることを、四角いチェックボックスは複数選べることを示します。ネイティブの<input type="radio">に.radioを指定し、ラベルのテキストと一緒に<label class="check">で囲むと、行全体を操作できます。

ラジオボタン

<fieldset class="fieldset">  <legend class="fieldset-legend">Visibility</legend>  <label class="check">    <input type="radio" class="radio" name="visibility" value="public" checked> Public  </label>  <label class="check">    <input type="radio" class="radio" name="visibility" value="team"> Team only  </label>  <label class="check">    <input type="radio" class="radio" name="visibility" value="private"> Private  </label></fieldset>
<fieldset className="fieldset">  <legend className="fieldset-legend">Visibility</legend>  <label className="check">    <input type="radio" className="radio" name="visibility" value="public" defaultChecked /> Public  </label>  <label className="check">    <input type="radio" className="radio" name="visibility" value="team" /> Team only  </label>  <label className="check">    <input type="radio" className="radio" name="visibility" value="private" /> Private  </label></fieldset>
<fieldset class="fieldset">  <legend class="fieldset-legend">Visibility</legend>  <label class="check">    <input type="radio" class="radio" name="visibility" value="public" checked> Public  </label>  <label class="check">    <input type="radio" class="radio" name="visibility" value="team"> Team only  </label>  <label class="check">    <input type="radio" class="radio" name="visibility" value="private"> Private  </label></fieldset>
<template>  <fieldset class="fieldset">    <legend class="fieldset-legend">Visibility</legend>    <label class="check">      <input type="radio" class="radio" name="visibility" value="public" checked> Public    </label>    <label class="check">      <input type="radio" class="radio" name="visibility" value="team"> Team only    </label>    <label class="check">      <input type="radio" class="radio" name="visibility" value="private"> Private    </label>  </fieldset></template>

#グループ

同じnameを持つラジオボタンは一つのグループになります。一つを選ぶと他の選択が解除され、矢印キーでグループ内の選択を移動できます。グループをfieldsetで囲み、legendに選択肢が答える質問を記述します。

#横並び

短い選択肢が少数なら、横に並べられます。どのラベルがどのラジオボタンに対応するかがわかるよう、選択肢同士の間隔は、ボタンとラベルの間隔より広く取ります。

横並び

<fieldset class="fieldset">  <legend class="fieldset-legend">Billing period</legend>  <div class="flex flex-wrap gap-lg">    <label class="check"><input type="radio" class="radio" name="period" checked> Monthly</label>    <label class="check"><input type="radio" class="radio" name="period"> Yearly</label>  </div></fieldset>
<fieldset className="fieldset">  <legend className="fieldset-legend">Billing period</legend>  <div className="flex flex-wrap gap-lg">    <label className="check">      <input type="radio" className="radio" name="period" defaultChecked /> Monthly    </label>    <label className="check"><input type="radio" className="radio" name="period" /> Yearly</label>  </div></fieldset>
<fieldset class="fieldset">  <legend class="fieldset-legend">Billing period</legend>  <div class="flex flex-wrap gap-lg">    <label class="check"><input type="radio" class="radio" name="period" checked> Monthly</label>    <label class="check"><input type="radio" class="radio" name="period"> Yearly</label>  </div></fieldset>
<template>  <fieldset class="fieldset">    <legend class="fieldset-legend">Billing period</legend>    <div class="flex flex-wrap gap-lg">      <label class="check"><input type="radio" class="radio" name="period" checked> Monthly</label>      <label class="check"><input type="radio" class="radio" name="period"> Yearly</label>    </div>  </fieldset></template>

#無効状態

無効にしたラジオボタンは薄く表示され、ラベルには無効状態の文字色が適用されます。選択肢を個別に無効にすることも、fieldsetを無効にしてグループ全体を無効にすることもできます。

無効状態

<fieldset class="fieldset">  <legend class="fieldset-legend">Region</legend>  <label class="check"><input type="radio" class="radio" name="region" checked> Tokyo</label>  <label class="check"><input type="radio" class="radio" name="region"> Frankfurt</label>  <label class="check">    <input type="radio" class="radio" name="region" disabled> Virginia, at capacity  </label></fieldset>
<fieldset className="fieldset">  <legend className="fieldset-legend">Region</legend>  <label className="check">    <input type="radio" className="radio" name="region" defaultChecked /> Tokyo  </label>  <label className="check"><input type="radio" className="radio" name="region" /> Frankfurt</label>  <label className="check">    <input type="radio" className="radio" name="region" disabled /> Virginia, at capacity  </label></fieldset>
<fieldset class="fieldset">  <legend class="fieldset-legend">Region</legend>  <label class="check"><input type="radio" class="radio" name="region" checked> Tokyo</label>  <label class="check"><input type="radio" class="radio" name="region"> Frankfurt</label>  <label class="check">    <input type="radio" class="radio" name="region" disabled> Virginia, at capacity  </label></fieldset>
<template>  <fieldset class="fieldset">    <legend class="fieldset-legend">Region</legend>    <label class="check"><input type="radio" class="radio" name="region" checked> Tokyo</label>    <label class="check"><input type="radio" class="radio" name="region"> Frankfurt</label>    <label class="check">      <input type="radio" class="radio" name="region" disabled> Virginia, at capacity    </label>  </fieldset></template>

#コントロールの選び方

ラジオボタンは、すべての選択肢を1行に一つずつ、まとめて表示します。単一選択には、用途に応じて以下のコントロールも使えます。

  • セグメントコントロールは、ビューの切り替えなど、短い選択肢を2〜5個、1行に並べる場合に使います。
  • セレクトは、選択肢が収まりきらない場合や、表示スペースが限られる場合に使います。
  • チョイスは、選択肢ごとに見出しと説明文が必要な場合に使います。
  • チップは、選択すると表示内容がすぐに切り替わるフィルターに使います。

#アクセシビリティ

  • グループ内のすべてのラジオボタンに同じnameを指定してください。共有の指定がないと、選択は排他的にならず、矢印キーでも選択肢間を移動できません。
  • グループにはタブ順上の停止位置が一つあります。Tabを押すと選択中のラジオボタンにフォーカスが移り、未選択の場合は先頭に移ります。そこから矢印キーで移動できます。
  • 妥当な既定の選択肢がある場合は、あらかじめ選択しておきます。未選択の状態で始めると、利用者が一度選択した後に未選択へ戻すことはできません。

#クラス

クラス説明
radioネイティブの type="radio" 入力。円形のくぼみ

#ソース

@master/ui/components/radio.css
/* * Radio: a native input drawn as a small round well that fills with ink and shows a dot when chosen. * A circle says "one of these", where a checkbox's square says "any of these". * Wrap it with its text in a .check label, which checkbox.css defines, to make the whole row a target. */@layer components.ui {  .radio {    --check-size: 1.125rem;    position: relative;    display: inline-grid;    flex: none;    place-items: center;    width: var(--check-size);    height: var(--check-size);    /* Centres the control on the first line of a label that wraps. */    margin: max(0px, calc((1lh - var(--check-size)) / 2)) 0;    padding: 0;    border: 0;    border-radius: 999px;    corner-shape: round;    /* A small control needs a firmer edge than a surface to be found: one step, still half a pixel. The well is the checkbox's. */    @apply --check-well;    vertical-align: middle;    appearance: none;    /* Standing alone, as a switch does: a pointer, no double-tap zoom and no grey flash around the well. */    cursor: pointer;    touch-action: manipulation;    -webkit-tap-highlight-color: transparent;    transition-property: background-color, box-shadow;    transition-duration: var(--duration-quick);    transition-timing-function: var(--easing-glide);    /* The dot: a third of the circle, in the ink of the page behind it, at any size the well takes. */    &::after {      content: '';      width: calc(var(--check-size) / 3);      height: calc(var(--check-size) / 3);      border-radius: 999px;      corner-shape: round;      opacity: 0;      scale: .4;      background: var(--color-text-inverse);      /* The dot leaves at once and arrives with a short settle. */      transition: opacity var(--duration-press) linear, scale var(--duration-press) linear;    }    /* Forced colors draw the well with system colors; the hover's tokens, which are more specific, would show through. */    @media (hover: hover) and (forced-colors: none) {      &:hover:not(:checked, :disabled) {        @apply --check-well-hover;      }    }    &:checked {      @apply --check-ink;      &::after {        opacity: 1;        scale: 1;        transition: opacity var(--duration-quick) var(--easing-glide), scale var(--duration-base) var(--easing-glide);      }    }    &:focus-visible {      @apply --focus-ring;    }    /*     * Invalid: a required group left without a choice once the reader has tried to send the form, or aria-invalid.     * Every well of the group takes the danger edge a field takes; the field's error beside them says why.     */    &:is([aria-invalid="true"], :user-invalid):not(:checked, :disabled) {      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);    }    &:disabled {      opacity: .5;      cursor: not-allowed;    }    @media (prefers-reduced-motion: reduce) {      &::after {        scale: 1;      }    }    /* Draw with system colors so the mark survives forced colors. */    @media (forced-colors: active) {      forced-color-adjust: none;      background: Field;      box-shadow: inset 0 0 0 1px FieldText;      &::after {        background: HighlightText;      }      &:checked {        background: Highlight;        box-shadow: none;      }      &:disabled {        opacity: 1;        box-shadow: inset 0 0 0 1px GrayText;      }      /* The danger edge is a theme color: the system's mark color says it here, as a field's outline does. */      &:is([aria-invalid="true"], :user-invalid):not(:checked, :disabled) {        box-shadow: inset 0 0 0 2px Mark;      }      /* Chosen and disabled: the fill takes the disabled system color, as a checkbox's does. */      &:disabled:checked {        background: GrayText;      }      &:focus-visible {        outline-color: Highlight;      }    }  }}