如何創建:登錄表單
學習如何使用 CSS 創建一個響應式登錄表單。
點擊按鈕可打開登錄表單:
如何創建登錄表單
第一步 - 添加 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>Username</b></label> <input type="text" placeholder="Enter Username" name="uname" required> <label for="psw"><b>Password</b></label> <input type="password" placeholder="Enter Password" name="psw" required> <button type="submit">Login</button> <label> <input type="checkbox" checked="checked" name="remember"> Remember me </label> </div> <div class="container" style="background-color:#f1f1f1"> <button type="button" class="cancelbtn">Cancel</button> <span class="psw">Forgot <a href="#">password?</a></span> </div> </form>
第二步 - 添加 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; } /* "Forgot password" 文本 */ span.psw { float: right; padding-top: 16px; } /* 更改超小屏幕上 span 和取消按鈕的樣式 */ @media screen and (max-width: 300px) { span.psw { display: block; float: none; } .cancelbtn { width: 100%; } }
如何創建模態登錄表單
第一步 - 添加 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="Close Modal">×</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>Username</b></label> <input type="text" placeholder="Enter Username" name="uname" required> <label for="psw"><b>Password</b></label> <input type="password" placeholder="Enter Password" name="psw" required> <button type="submit">Login</button> <label> <input type="checkbox" checked="checked" name="remember"> Remember me </label> </div> <div class="container" style="background-color:#f1f1f1"> <button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button> <span class="psw">Forgot <a href="#">password?</a></span> </div> </form> </div>
第二步 - 添加 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)} }
提示:您還可以使用 JavaScript 代碼,通過點擊模態框內容之外的區域來關閉模態框(而不僅僅是使用 “x” 或 "cancel" 按鈕):
<script> // 獲取模態 var modal = document.getElementById('id01'); // 當用戶單擊模態之外的任何位置時,將其關閉 window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } } </script>
相關頁面
教程:HTML 表單
教程:CSS 表單