纯css制作登录
纯CSS制作登录表单
使用纯CSS可以创建简洁美观的登录表单,无需JavaScript。以下是实现方法:
HTML结构

<div class="login-container">
<form class="login-form">
<h2>Login</h2>
<div class="input-group">
<input type="text" id="username" required>
<label for="username">Username</label>
</div>
<div class="input-group">
<input type="password" id="password" required>
<label for="password">Password</label>
</div>
<button type="submit">Sign In</button>
</form>
</div>
CSS样式

.login-container {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.login-form {
width: 350px;
padding: 40px;
background: white;
border-radius: 10px;
box-shadow: 0 15px 35px rgba(0,0,0,0.2);
}
.login-form h2 {
margin-bottom: 30px;
text-align: center;
color: #333;
}
.input-group {
position: relative;
margin-bottom: 30px;
}
.input-group input {
width: 100%;
padding: 10px 0;
font-size: 16px;
border: none;
border-bottom: 1px solid #999;
outline: none;
background: transparent;
}
.input-group label {
position: absolute;
top: 10px;
left: 0;
font-size: 16px;
color: #999;
pointer-events: none;
transition: all 0.3s;
}
.input-group input:focus ~ label,
.input-group input:valid ~ label {
top: -15px;
font-size: 12px;
color: #667eea;
}
button {
width: 100%;
padding: 12px;
background: linear-gradient(to right, #667eea, #764ba2);
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
transition: all 0.3s;
}
button:hover {
opacity: 0.9;
transform: translateY(-2px);
}
动画效果增强
可以为输入框添加聚焦动画效果:
.input-group input:focus {
border-bottom: 2px solid #667eea;
}
.input-group::after {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 0;
height: 2px;
background: #667eea;
transition: width 0.3s;
}
.input-group input:focus ~ ::after {
width: 100%;
}
响应式设计
添加媒体查询确保在不同设备上显示良好:
@media (max-width: 480px) {
.login-form {
width: 90%;
padding: 30px;
}
}
注意事项
- 使用
required属性确保字段不为空 - 为表单元素添加适当的
tabindex属性 - 考虑添加密码显示/隐藏切换功能(需使用少量JavaScript)
- 确保颜色对比度符合无障碍标准
这种纯CSS实现的登录表单简洁美观,包含基本的交互效果,适合轻量级应用场景。






