如何创建:登录表单
学习如何使用 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 表单