css制作登录界面
基础HTML结构
创建登录界面的HTML结构,包含表单、输入框和按钮:
<!DOCTYPE html>
<html lang="zh-CN">
<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">
<h2>用户登录</h2>
<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>
<button type="submit">登录</button>
<div class="footer">
<a href="#">忘记密码?</a>
<a href="#">注册账号</a>
</div>
</form>
</div>
</body>
</html>
基本样式设计
添加CSS样式美化登录界面:
body {
font-family: 'Arial', sans-serif;
background-color: #f5f5f5;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.login-container {
background: white;
padding: 2rem;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 400px;
}
h2 {
color: #333;
text-align: center;
margin-bottom: 1.5rem;
}
.input-group {
margin-bottom: 1rem;
}
label {
display: block;
margin-bottom: 0.5rem;
color: #555;
}
input {
width: 100%;
padding: 0.75rem;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
button {
width: 100%;
padding: 0.75rem;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 1rem;
margin-top: 1rem;
}
button:hover {
background-color: #45a049;
}
.footer {
display: flex;
justify-content: space-between;
margin-top: 1rem;
}
a {
color: #4CAF50;
text-decoration: none;
font-size: 0.9rem;
}
a:hover {
text-decoration: underline;
}
响应式设计
添加媒体查询使登录界面适应不同屏幕尺寸:
@media (max-width: 480px) {
.login-container {
padding: 1.5rem;
margin: 0 1rem;
}
body {
padding: 1rem;
}
}
动画效果增强
为登录界面添加简单的动画效果:
.login-container {
animation: fadeIn 0.5s ease-out;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(-20px); }
to { opacity: 1; transform: translateY(0); }
}
button {
transition: background-color 0.3s ease;
}
input:focus {
outline: none;
border-color: #4CAF50;
box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.2);
transition: all 0.3s ease;
}
高级样式选项
使用CSS变量和现代样式增强视觉效果:

:root {
--primary-color: #4CAF50;
--primary-hover: #45a049;
--shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
--transition: all 0.3s ease;
}
.login-container {
box-shadow: var(--shadow);
}
button {
background-color: var(--primary-color);
transition: var(--transition);
}
button:hover {
background-color: var(--primary-hover);
}
.input-group {
position: relative;
}
.input-group:after {
content: '';
position: absolute;
left: 0;
bottom: 0;
width: 0;
height: 2px;
background-color: var(--primary-color);
transition: var(--transition);
}
.input-group:focus-within:after {
width: 100%;
}





