css制作登录界面
使用CSS制作登录界面
制作一个美观且功能完善的登录界面需要结合HTML和CSS。以下是一个完整的实现方案:
HTML结构
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>登录界面</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="login-container">
<h1>用户登录</h1>
<form class="login-form">
<div class="input-group">
<label for="username">用户名</label>
<input type="text" id="username" placeholder="请输入用户名">
</div>
<div class="input-group">
<label for="password">密码</label>
<input type="password" id="password" placeholder="请输入密码">
</div>
<div class="remember-me">
<input type="checkbox" id="remember">
<label for="remember">记住我</label>
</div>
<button type="submit">登录</button>
<div class="links">
<a href="#">忘记密码?</a>
<a href="#">注册账号</a>
</div>
</form>
</div>
</body>
</html>
CSS样式
/* 基础样式重置 */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Arial', sans-serif;
}
body {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.login-container {
background-color: white;
padding: 2.5rem;
border-radius: 10px;
box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
width: 100%;
max-width: 400px;
}
h1 {
text-align: center;
margin-bottom: 1.5rem;
color: #333;
}
.input-group {
margin-bottom: 1.5rem;
}
label {
display: block;
margin-bottom: 0.5rem;
color: #555;
font-weight: 500;
}
input[type="text"],
input[type="password"] {
width: 100%;
padding: 12px;
border: 1px solid #ddd;
border-radius: 5px;
font-size: 16px;
transition: border-color 0.3s;
}
input[type="text"]:focus,
input[type="password"]:focus {
border-color: #667eea;
outline: none;
}
.remember-me {
display: flex;
align-items: center;
margin-bottom: 1.5rem;
}
.remember-me input {
margin-right: 0.5rem;
}
button {
width: 100%;
padding: 12px;
background: linear-gradient(to right, #667eea, #764ba2);
color: white;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
transition: opacity 0.3s;
}
button:hover {
opacity: 0.9;
}
.links {
display: flex;
justify-content: space-between;
margin-top: 1rem;
}
.links a {
color: #667eea;
text-decoration: none;
font-size: 14px;
}
.links a:hover {
text-decoration: underline;
}
响应式设计
@media (max-width: 480px) {
.login-container {
padding: 1.5rem;
margin: 0 1rem;
}
h1 {
font-size: 1.5rem;
}
}
动画效果增强
.login-container {
animation: fadeIn 0.5s ease-out;
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
button {
transition: transform 0.2s, opacity 0.3s;
}
button:active {
transform: scale(0.98);
}
验证样式
input:invalid {
border-color: #ff6b6b;
}
.error-message {
color: #ff6b6b;
font-size: 0.8rem;
margin-top: 0.3rem;
display: none;
}
input:invalid + .error-message {
display: block;
}
暗黑模式支持
@media (prefers-color-scheme: dark) {
.login-container {
background-color: #2d3748;
color: #f7fafc;
}
h1 {
color: #f7fafc;
}
label {
color: #e2e8f0;
}
input {
background-color: #4a5568;
color: #f7fafc;
border-color: #4a5568;
}
}
这个登录界面设计包含了现代UI的所有关键元素:渐变背景、卡片式布局、输入框焦点状态、按钮悬停效果、响应式设计以及可选的暗黑模式支持。可以根据实际需求调整颜色、间距和动画效果。







