【6日でできるHTML&CSS入門】Bootstrapのボタン・フォーム

 現代のWebサイト開発では、ユーザー入力を受け取るフォームが欠かせません。Bootstrapを使えば、誰でも見栄えの良い・使いやすいフォームをすぐに作成できます。ここでは、Bootstrap 5を利用したフォームの作り方を実例とともに詳しく解説します。

1.Bootstrapフォームの基本

1.1. シンプルなフォームの実行例

インターネット接続が必要です。CDN経由でBootstrapのCSS/JSを読み込みます。

ファイル名: lesson54_1.html

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <title>Bootstrap フォームサンプル</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- Bootstrap 5 CDN -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <div class="container mt-5">
    <h1 class="mb-4">会員登録フォーム</h1>
    <form method="GET" action="#">
      <div class="mb-3">
        <label for="name" class="form-label">お名前</label>
        <input type="text" class="form-control" id="name" name="name" placeholder="例)山田太郎">
      </div>
      <div class="mb-3">
        <label class="form-label">性別</label><br>
        <div class="form-check form-check-inline">
          <input class="form-check-input" type="radio" name="gender" id="male" value="男" checked>
          <label class="form-check-label" for="male">男</label>
        </div>
        <div class="form-check form-check-inline">
          <input class="form-check-input" type="radio" name="gender" id="female" value="女">
          <label class="form-check-label" for="female">女</label>
        </div>
      </div>
      <div class="mb-3">
        <label for="email" class="form-label">メールアドレス</label>
        <input type="email" class="form-control" id="email" name="email" placeholder="sample@example.com">
      </div>
      <div class="mb-3">
        <label for="password" class="form-label">パスワード</label>
        <input type="password" class="form-control" id="password" name="password" placeholder="パスワード">
      </div>
      <button type="submit" class="btn btn-primary">登録する</button>
    </form>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

1.2. サンプル出力イメージ


2.タグ・クラス解説

タグ/クラス説明・用途
<form>フォームの開始・終了
form-control入力欄(input, email, passwordなど)のスタイル
form-labelラベル(入力項目名)のスタイル
form-checkチェックボックスやラジオボタンの枠
form-check-inputチェックボックスやラジオボタン本体
form-check-labelチェックボックスやラジオボタンのラベル
btn btn-primaryBootstrap標準の青いボタン
container mt-5フォーム全体の幅と余白
mb-3項目ごとの下マージン

3.よく使うフォーム部品とBootstrapクラス

フォーム部品Bootstrapクラス解説・効果例
テキスト入力form-control入力欄が太くなり、角丸や余白がついて見やすい。
ラジオボタンform-check-input標準より大きめでクリックしやすいラジオボタン
チェックボックスform-check-input同上
ラベルform-label項目名の上に配置
ボタンbtn btn-○○標準色や強調色で視認性の高いボタン

まとめ

  • Bootstrapを使えばHTMLにクラスを追加するだけで美しいフォームが実現できます。
  • 上記のサンプルはそのまま動作し、レイアウトや色のカスタマイズも容易です。
  • 入力項目の追加・変更もform-controlなどのクラスを付ければ統一感のある見た目にできます。