css制作登录网页
创建HTML结构
使用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>
<button type="submit">登录</button>
</form>
</div>
</body>
</html>
设计CSS样式
通过CSS美化登录页面,设置布局、颜色和交互效果。
body {
font-family: 'Arial', sans-serif;
background-color: #f4f4f9;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.login-container {
background: #ffffff;
padding: 2rem;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 400px;
}
.login-container h1 {
margin-bottom: 1.5rem;
color: #333;
text-align: center;
}
.login-form {
display: flex;
flex-direction: column;
}
.input-group {
margin-bottom: 1rem;
}
.input-group label {
display: block;
margin-bottom: 0.5rem;
color: #555;
}
.input-group input {
width: 100%;
padding: 0.75rem;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 1rem;
}
.input-group input:focus {
border-color: #007bff;
outline: none;
}
button {
background-color: #007bff;
color: white;
padding: 0.75rem;
border: none;
border-radius: 4px;
font-size: 1rem;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #0056b3;
}
添加响应式设计
确保登录页面在不同设备上都能良好显示。
@media (max-width: 600px) {
.login-container {
padding: 1.5rem;
margin: 0 1rem;
}
}
增强交互效果
通过CSS增加悬停和焦点状态,提升用户体验。
button:active {
transform: scale(0.98);
}
.input-group input:focus {
box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
}
可选:添加额外功能
根据需求可以添加记住密码、忘记密码链接或社交媒体登录选项。
<div class="login-options">
<div class="remember-me">
<input type="checkbox" id="remember">
<label for="remember">记住我</label>
</div>
<a href="#" class="forgot-password">忘记密码?</a>
</div>
.login-options {
display: flex;
justify-content: space-between;
margin-bottom: 1rem;
}
.remember-me {
display: flex;
align-items: center;
}
.remember-me input {
margin-right: 0.5rem;
}
.forgot-password {
color: #007bff;
text-decoration: none;
}
.forgot-password:hover {
text-decoration: underline;
}





