如何創建:注冊表單
學習如何使用 CSS 創建響應式注冊表單。
單擊按鈕可打開注冊表單:
×
如何創建注冊表單
第一步 - 添加 HTML:
使用 <form> 元素來處理輸入。您可以在我們的 PHP 教程中了解更多相關信息。
然后為每個字段添加輸入控件(并帶有匹配的標簽):
<form action="action_page.php" style="border:1px solid #ccc"> <div class="container"> <h1>Sign Up</h1> <p>Please fill in this form to create an account.</p> <hr> <label for="email"><b>Email</b></label> <input type="text" placeholder="Enter Email" name="email" required> <label for="psw"><b>Password</b></label> <input type="password" placeholder="Enter Password" name="psw" required> <label for="psw-repeat"><b>Repeat Password</b></label> <input type="password" placeholder="Repeat Password" name="psw-repeat" required> <label> <input type="checkbox" checked="checked" name="remember" style="margin-bottom:15px">Remember me </label> <p>By creating an account you agree to our <a href="#" style="color:dodgerblue">Terms & Privacy</a>.</p> <div class="clearfix"> <button type="button" class="cancelbtn">Cancel</button> <button type="submit" class="signupbtn">Sign Up</button> </div> </div> </form>
第二步 - 添加 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%; } }
如何創建模態注冊表單
第一步 - 添加 HTML:
使用 <form> 元素來處理輸入。您可以在我們的 PHP 教程中了解更多相關信息。
然后為每個字段添加輸入控件(并帶有匹配的標簽):
<!-- Button to open the modal --> <button onclick="document.getElementById('id01').style.display='block'">Sign Up</button> <!-- The Modal (contains the Sign Up form) --> <div id="id01" class="modal"> <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">times;</span> <form class="modal-content" action="/action_page.php"> <div class="container"> <h1>Sign Up</h1> <p>Please fill in this form to create an account.</p> <hr> <label for="email"><b>Email</b></label> <input type="text" placeholder="Enter Email" name="email" required> <label for="psw"><b>Password</b></label> <input type="password" placeholder="Enter Password" name="psw" required> <label for="psw-repeat"><b>Repeat Password</b></label> <input type="password" placeholder="Repeat Password" name="psw-repeat" required> <label> <input type="checkbox" checked="checked" name="remember" style="margin-bottom:15px"> Remember me </label> <p>By creating an account you agree to our <a href="#" style="color:dodgerblue">Terms & Privacy</a>.</p> <div class="clearfix"> <button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button> <button type="submit" class="signup">Sign Up</button> </div> </div> </form> </div>
第二步 - 添加 CSS:
* {box-sizing: border-box} /* Full-width input fields */ 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; /* Hidden by default */ position: fixed; /* Stay in place */ z-index: 1; /* Sit on top */ left: 0; top: 0; width: 100%; /* Full width */ height: 100%; /* Full height */ overflow: auto; /* Enable scroll if needed */ 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; } /* 關閉按鈕 (x) */ .close { position: absolute; right: 35px; top: 15px; font-size: 40px; font-weight: bold; color: #f1f1f1; } .close:hover, .close:focus { color: #f44336; cursor: pointer; } /* Clear floats */ .clearfix::after { content: ""; clear: both; display: table; } /* 更改超小屏幕上取消按鈕和注冊按鈕的樣式 */ @media screen and (max-width: 300px) { .cancelbtn, .signupbtn { width: 100%; } }
提示:您還可以使用以下 JavaScript 代碼,通過點擊模態框內容之外的區域來關閉模態框(而不僅僅是通過使用 "×" 或 "cancel" 按鈕來關閉它):
<script> // 獲取模態 var modal = document.getElementById('id01'); // 當用戶單擊模式之外的任何位置時,將其關閉 window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } } </script>
相關頁面
教程:HTML 表單
教程:CSS 表單