如何创建:登录表单

学习如何使用 CSS 创建一个响应式登录表单。

点击按钮可打开登录表单:

自分で試してみてください

如何创建登录表单

第1歩 - HTMLを追加:

在容器中添加一张图片,并为每个字段添加输入控件(并带有匹配的ラベル)。それらを <form> 要素で囲んで、入力を処理します。

您可以在我们的 PHPトレーニング 中了解更多关于如何处理输入的信息。

<form action="action_page.php" method="post">
  <div class="imgcontainer">
    <img src="img_avatar2.png" alt="Avatar" class="avatar">
  </div>
  <div class="container">
    <label for="uname"><b>ユーザー名</b></label>
    <input type="text" placeholder="ユーザー名を入力" name="uname" required>
    <label for="psw"><b>パスワード</b></label>
    <input type="password" placeholder="パスワードを入力" name="psw" required>
    <button type="submit">ログイン</button>
    <label>
      <input type="checkbox" checked="checked" name="remember"> メモリーメイク
    </label>
  </div>
  <div class="container" style="background-color:#f1f1f1">
    <button type="button" class="cancelbtn">Cancel</button>
    <span class="psw">パスワードを忘れた?<a href="#">パスワードをリセット</a></span>
  </div>
</form>

第2ステップ - CSSを追加します:

/* 有边框的表单 */
form {
  border: 3px solid #f1f1f1;
}
/* 全宽输入框 */
input[type=text], input[type=password] {
  width: 100%;
  padding: 12px 20px;
  margin: 8px 0;
  display: inline-block;
  border: 1px solid #ccc;
  box-sizing: border-box;
}
/* 为所有按钮设置样式 */
button {
  background-color: #04AA6D;
  color: white;
  padding: 14px 20px;
  margin: 8px 0;
  border: none;
  cursor: pointer;
  width: 100%;
}
/* ボタンにホバー効果を追加 */
button:hover {
  opacity: 0.8;
}
/* キャンセルボタンの追加スタイル(赤色) */
.cancelbtn {
  width: auto;
  padding: 10px 18px;
  background-color: #f44336;
}
/* このコンテナ内のアバターアイコン画像を中央に配置 */
.imgcontainer {
  text-align: center;
  margin: 24px 0 12px 0;
}
/* アバターアイコン画像 */
img.avatar {
  width: 40%;
  border-radius: 50%;
}
/* コンテナに内余白を追加 */
.container {
  padding: 16px;
}
/* "パスワードを忘れたか"テキスト */
span.psw {
  float: right;
  padding-top: 16px;
}
/* 超小画面上のspanとキャンセルボタンのスタイルを変更 */
@media screen and (max-width: 300px) {
  span.psw {
    display: block;
    float: none;
  }
  .cancelbtn {
    width: 100%;
  }
}

自分で試してみてください

モーダルログインフォームを作成する方法

第1歩 - HTMLを追加:

<!-- モーダルログインフォームを開くボタンのコメント -->
<button onclick="document.getElementById('id01').style.display='block'">Login</button>
<!-- モーダル -->
<div id="id01" class="modal">
  <span onclick="document.getElementById('id01').style.display='none'"
class="close" title="モーダルを閉じる">×</span>
  <!-- モーダル内容 -->
  <form class="modal-content animate" action="/action_page.php">
    <div class="imgcontainer">
      <img src="img_avatar2.png" alt="Avatar" class="avatar">
    </div>
    <div class="container">
      <label for="uname"><b>ユーザー名</b></label>
      <input type="text" placeholder="ユーザー名を入力" name="uname" required>
      <label for="psw"><b>パスワード</b></label>
      <input type="password" placeholder="パスワードを入力" name="psw" required>
      <button type="submit">ログイン</button>
      <label>
        <input type="checkbox" checked="checked" name="remember"> メモリーメイク
      </label>
    </div>
    <div class="container" style="background-color:#f1f1f1">
      <button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">キャンセル</button>
      <span class="psw">パスワードを忘れた?<a href="#">パスワードをリセット</a></span>
    </div>
  </form>
</div>

第2ステップ - CSSを追加します:

/* モーダル(背景) */
.modal {
  display: none; /* デフォルトで非表示 */
  position: fixed; /* 原地に留まる */
  z-index: 1; /* 顶部に配置 */
  left: 0;
  top: 0;
  width: 100%; /* 全幅 */
  height: 100%; /* 全高 */
  overflow: auto; /* 必要に応じてスクロールを有効にします */
  background-color: rgb(0,0,0); /* 背景色のリトライ */
  background-color: rgba(0,0,0,0.4); /* 透明度のある黒 */
  padding-top: 60px;
}
/* モダルの内容/ボックス */
.modal-content {
  background-color: #fefefe;
  margin: 5px auto; /* 顶部 15% に配置し中央に */
  border: 1px solid #888;
  width: 80%; /* 画面サイズによって多少異なる可能性があります */
}
/* 閉じるボタン */
.close {
  /* モダルボックスの外部の右上隅に配置 */
  position: absolute;
  right: 25px;
  top: 0;
  color: #000;
  font-size: 35px;
  font-weight: bold;
}
/* ホバー時の閉じるボタン */
.close:hover,
.close:focus {
  color: red;
  cursor: pointer;
}
/* 拡大縮小アニメーションを追加 */
.animate {
  -webkit-animation: animatezoom 0.6s;
  animation: animatezoom 0.6s
}
@-webkit-keyframes animatezoom {
  from {-webkit-transform: scale(0)}
  to {-webkit-transform: scale(1)}
}
@keyframes animatezoom {
  from {transform: scale(0)}
  to {transform: scale(1)}
}

ヒント:モダルボックスの内容の外側をクリックしてモダルボックスを閉じる(「x」や「cancel」ボタンを使用するだけでなく)JavaScriptコードを使用して閉じることができます:

<script>
// モダルを取得
var modal = document.getElementById('id01');
// ユーザーがモダルの外のどこかをクリックした場合、それを閉じます 
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
</script>

自分で試してみてください

関連ページ

チュートリアル:HTMLフォーム

チュートリアル:CSSフォーム