作成方法:登録フォーム
CSSを使用してリスポンシブな登録フォームを作成する方法を学びます。
ボタンをクリックすると、登録フォームが開きます:
×
登録フォームの作成方法
第1歩 - HTMLを追加:
<form>要素を使用して入力を処理します。詳細については、PHPトレーニングでさらに情報を得ることができます。
各フィールドに入力コントロール(対応するラベル付き)を追加します:
<form action="action_page.php" style="border:1px solid #ccc"> <div class="container"> <h1>サインアップ</h1> <p>アカウントを作成するためにこのフォームに情報を入力してください。</p> <hr> <label for="email"><b>メール</b></label> <input type="text" placeholder="メールを入力" name="email" required> <label for="psw"><b>パスワード</b></label> <input type="password" placeholder="パスワードを入力" name="psw" required> <label for="psw-repeat"><b>パスワードの再入力</b></label> <input type="password" placeholder="パスワードの再入力" name="psw-repeat" required> <label> <input type="checkbox" checked="checked" name="remember" style="margin-bottom:15px">覚えておく </label> <p>アカウントを作成することで、当社の<a href="#" style="color:dodgerblue">利用規約とプライバシーポリシー</a>に同意したことになります。</p> <div class="clearfix"> <button type="button" class="cancelbtn">キャンセル</button> <button type="submit" class="signupbtn">サインアップ</button> </div> </div> </form>
第2段 - CSSを追加:
* {box-sizing: border-box} /* 全幅の入力フィールド */ input[type=text], input[type=password] { width: 100%; padding: 15px; margin: 5px 0 22px 0; display: inline-block; border: none; background: #f1f1f1; } input[type=text]:focus, input[type=password]:focus { background-color: #ddd; outline: none; } hr { border: 1px solid #f1f1f1; margin-bottom: 25px; } /* 全てのボタンのスタイルを設定 */ button { background-color: #04AA6D; color: white; padding: 14px 20px; margin: 8px 0; border: none; cursor: pointer; width: 100%; opacity: 0.9; } button:hover { opacity:1; } /* キャンセルボタンの追加スタイルを設定 */ .cancelbtn { padding: 14px 20px; background-color: #f44336; } /* キャンセルと登録ボタンをフロートにし、等幅を追加 */ .cancelbtn, .signupbtn { float: left; width: 50%; } /* コンテナ要素に内枠を追加 */ .container { padding: 16px; } /* フロートをクリア */ .clearfix::after { content: ""; clear: both; display: table; } /* 超小画面上のキャンセルボタンと登録ボタンのスタイルを変更 */ @media screen and (max-width: 300px) { .cancelbtn, .signupbtn { width: 100%; } }
モーダル登録フォームの作成方法
第1歩 - HTMLを追加:
<form>要素を使用して入力を処理します。詳細については、PHPトレーニングでさらに情報を得ることができます。
各フィールドに入力コントロール(対応するラベル付き)を追加します:
<!-- モーダルを開くボタン --> <button onclick="document.getElementById('id01').style.display='block'">サインアップ</button> <!-- モーダル(サインアップフォームを含む) --> <div id="id01" class="modal"> <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">タイムズ;</span> <form class="modal-content" action="/action_page.php"> <div class="container"> <h1>サインアップ</h1> <p>アカウントを作成するためにこのフォームに情報を入力してください。</p> <hr> <label for="email"><b>メール</b></label> <input type="text" placeholder="メールを入力" name="email" required> <label for="psw"><b>パスワード</b></label> <input type="password" placeholder="パスワードを入力" name="psw" required> <label for="psw-repeat"><b>パスワードの再入力</b></label> <input type="password" placeholder="パスワードの再入力" name="psw-repeat" required> <label> <input type="checkbox" checked="checked" name="remember" style="margin-bottom:15px"> メモリメイク </label> <p>アカウントを作成することで、当社の<a href="#" style="color:dodgerblue">利用規約とプライバシーポリシー</a>に同意したことになります。</p> <div class="clearfix"> <button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">キャンセル</button> <button type="submit" class="signup">Sign Up</button> </div> </div> </form> </div>
第2段 - CSSを追加:
* {box-sizing: border-box} /* 全幅の入力フィールド */ input[type=text], input[type=password] { width: 100%; padding: 15px; margin: 5px 0 22px 0; display: inline-block; border: none; background: #f1f1f1; } /* 入力フィールドがフォーカスを取得したときに背景色を追加 */ input[type=text]:focus, input[type=password]:focus { background-color: #ddd; outline: none; } /* 全てのボタンのスタイルを設定 */ button { background-color: #04AA6D; color: white; padding: 14px 20px; margin: 8px 0; border: none; cursor: pointer; width: 100%; opacity: 0.9; } button:hover { opacity:1; } /* キャンセルボタンの追加スタイルを設定 */ .cancelbtn { padding: 14px 20px; background-color: #f44336; } /* キャンセルと登録ボタンのフロートを有効にし、幅を等しく設定 */ .cancelbtn, .signupbtn { float: left; width: 50%; } /* コンテナ要素に内余白を追加 */ .container { padding: 16px; } /* モーダル(背景) */ .modal { display: none; /* デフォルトで隠す */ position: fixed; /* 位置固定 */ z-index: 1; /* 上面に配置 */ left: 0; top: 0; width: 100%; /* 全ての幅 */ height: 100%; /* 全ての高さ */ overflow: auto; /* 適切にスクロールを有効に */ background-color: #474e5d; padding-top: 50px; } /* モーダルの内容/ボックス */ .modal-content { background-color: #fefefe; margin: 5% auto 15% auto; /* 顶部5%、底部15%、中央に配置 */ border: 1px solid #888; width: 80%; /* 画面サイズによってはもっと多くまたは少なくなる可能性があります */ } /* 水平線のスタイルを設定 */ hr { border: 1px solid #f1f1f1; margin-bottom: 25px; } /* 閉じるボタン(×) */ .close { position: absolute; right: 35px; top: 15px; font-size: 40px; font-weight: bold; color: #f1f1f1; } .close:hover { .close:focus { color: #f44336; cursor: pointer; } /* フロートをクリア */ .clearfix::after { content: ""; clear: both; display: table; } /* 超小画面上のキャンセルボタンと登録ボタンのスタイルを変更 */ @media screen and (max-width: 300px) { .cancelbtn, .signupbtn { width: 100%; } }
ヒント:以下のJavaScriptコードを使用して、モーダルの内容の外側をクリックしてモーダルを閉じることができます(「×」や「キャンセル」ボタンを使用するだけでなく):
<script> // 获取模态 var modal = document.getElementById('id01'); // 当用户单击模式之外的任何位置时,将其关闭 window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } } </script>
実際に試してみる
関連ページHTMLフォーム
関連ページCSSフォーム