如何 - 登录表单
了解如何使用 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 表单教程 了解更多关于如何设置表单元素样式的信息。