Foundation フォーム


Foundation のデフォルトフォーム設定

フォームコントロールは、Foundation によって自動的にいくつかのグローバルなスタイリングを受け取ります:

全ての <textarea><select> および テキスト <input> 要素は、 100% の幅、マージンとパディング、影およびホバー効果を持っています:

<form>
  Input:
  <input type="text" placeholder="Name">

  Textarea:
  <textarea rows="4" placeholder="Address"></textarea>

  Select:
  <select>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
  </select>
</form>

結果:

Input: Textarea: Select:
Try it Yourself »

ラベル

ラベルを定義するには、<label> 要素をフォームコントロールの周りにラップし、その for 属性と id 属性を一緒にバインドします。その後、ユーザが入力フォーカスを取得するために、ラベルや入力フィールドの いずれもクリックできるようになります:

<form>
  <label for="name">Input
    <input type="text" placeholder="Name" id="name">
  </label>

  <label for="adr">Label
    <textarea rows="4" placeholder="Address" id="adr"></textarea>
  </label>

  <label for="num">Select
    <select id="num">
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
    </select>
  </label>
</form>

結果:

Try it Yourself »

ラベルを .right クラスで右揃えにします:

<label class="right">

結果:

Try it Yourself »

Fieldset

Foundation は、HTML <fieldset> 要素を以下の方法でスタイルします:

<form>
  <fieldset>
    <legend>Fieldset Legend</legend>
    <label>Name
      <input type="text" placeholder="First Name..">
    </label>
    <label>Email
      <input type="text" placeholder="Enter email..">
    </label>
  </fieldset>
</form>

結果:

Fieldset Legend
Try it Yourself »

エラー状態

label、input や textarea に対する赤色のエラースタイルを指定するために、.error クラスを使用します:

<form>
  <label class="error">Error
    <input type="text" placeholder="Name..">
  </label>
  <small class="error">Wrong input</small>

  <textarea rows="4" placeholder="Address"></textarea>
  <small class="error">Wrong input</small>
</form>

結果:

Wrong input Wrong input
Try it Yourself »
Note 実際にエラー状態を効果に結び付けるには、ユーザが誤った入力をしたときに、この状態を更新するために、 JavaScript を使用する必要があります。