如何 - 登入表單
瞭解如何使用 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">取消</button>
<span class="psw">忘記 </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'">登入</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">忘記 </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)}
}
提示:您也可以使用以下 JavaScript 在模態框外部單擊時關閉模態框(而不僅僅是使用“x”或“取消”按鈕關閉它)
示例
<script>
// 獲取模態框
var modal = document.getElementById('id01');
// 當用戶在模態框外部的任何位置單擊時,關閉它
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
自己動手試一試 »
提示:前往我們的 HTML 表單教程 瞭解更多關於 HTML 表單的資訊。
提示:前往我們的 CSS 表單教程 瞭解更多關於如何設定表單元素樣式的資訊。