Skip to content

表单美化(提升用户体验,常用实战场景)

24.1 表单元素美化

文本框和密码框

基本样式

css
input[type="text"],
input[type="password"],
input[type="email"] {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  transition: all 0.3s ease;
}

input[type="text"]:focus,
input[type="password"]:focus,
input[type="email"]:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
}

单选框和复选框

自定义样式

css
/* 隐藏默认单选框 */
input[type="radio"] {
  display: none;
}

/* 自定义单选框 */
.radio-label {
  position: relative;
  padding-left: 30px;
  cursor: pointer;
  display: inline-block;
  margin-right: 20px;
}

.radio-label::before {
  content: '';
  position: absolute;
  left: 0;
  top: 2px;
  width: 20px;
  height: 20px;
  border: 2px solid #ddd;
  border-radius: 50%;
  transition: all 0.3s ease;
}

input[type="radio"]:checked + .radio-label::before {
  border-color: #3498db;
  background-color: #3498db;
}

input[type="radio"]:checked + .radio-label::after {
  content: '';
  position: absolute;
  left: 7px;
  top: 9px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background-color: white;
}

下拉框

自定义样式

css
select {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  background-color: white;
  appearance: none;
  background-image: url('data:image/svg+xml;charset=US-ASCII,%3Csvg xmlns="http://www.w3.org/2000/svg" width="292.4" height="292.4"%3E%3Cpath fill="%23333" d="M287 69.4a17.6 17.6 0 0 0-13-5.4H18.4c-5 0-9.3 1.8-12.9 5.4A17.6 17.6 0 0 0 0 82.2c0 5 1.8 9.3 5.4 12.9l128 127.9c3.6 3.6 7.8 5.4 12.8 5.4s9.2-1.8 12.8-5.4L287 95c3.5-3.5 5.4-7.8 5.4-12.8 0-5-1.9-9.2-5.5-12.8z"/%3E%3C/svg%3E');
  background-repeat: no-repeat;
  background-position: right 15px center;
  background-size: 12px;
  transition: all 0.3s ease;
}

select:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
}

文本域

样式

css
textarea {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  resize: vertical;
  min-height: 120px;
  transition: all 0.3s ease;
}

textarea:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
}

24.2 表单状态美化

焦点状态(focus)

css
input:focus,
textarea:focus,
select:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
}

悬停状态(hover)

css
input:hover,
textarea:hover,
select:hover {
  border-color: #999;
}

禁用状态(disabled)

css
input:disabled,
textarea:disabled,
select:disabled {
  background-color: #f5f5f5;
  border-color: #ddd;
  cursor: not-allowed;
  color: #999;
}

错误状态

css
input.error,
textarea.error {
  border-color: #e74c3c;
}

input.error:focus,
textarea.error:focus {
  box-shadow: 0 0 0 3px rgba(231, 76, 60, 0.2);
}

.error-message {
  color: #e74c3c;
  font-size: 14px;
  margin-top: 5px;
}

24.3 表单按钮美化

基本按钮样式

css
button {
  padding: 12px 24px;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.btn-primary {
  background-color: #3498db;
  color: white;
}

.btn-primary:hover {
  background-color: #2980b9;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(52, 152, 219, 0.3);
}

.btn-secondary {
  background-color: #95a5a6;
  color: white;
}

.btn-secondary:hover {
  background-color: #7f8c8d;
}

按钮状态

css
button:disabled {
  background-color: #bdc3c7;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

button:active {
  transform: translateY(0);
}

24.4 实战:制作美观的注册表单、登录表单

实战目标

创建美观的注册表单和登录表单。

实战步骤

  1. 创建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>CSS表单美化实战</title>
  <style>
    /* 在这里添加CSS样式 */
  </style>
</head>
<body>
  <div class="container">
    <h1>CSS表单美化实战</h1>
    
    <!-- 登录表单 -->
    <section class="demo-section">
      <h2>登录表单</h2>
      <form class="login-form">
        <div class="form-group">
          <label for="email">邮箱</label>
          <input type="email" id="email" placeholder="请输入邮箱">
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input type="password" id="password" placeholder="请输入密码">
        </div>
        <div class="form-group checkbox-group">
          <input type="checkbox" id="remember">
          <label for="remember">记住我</label>
        </div>
        <button type="submit" class="btn btn-primary">登录</button>
      </form>
    </section>
    
    <!-- 注册表单 -->
    <section class="demo-section">
      <h2>注册表单</h2>
      <form class="register-form">
        <div class="form-row">
          <div class="form-group">
            <label for="firstName">姓名</label>
            <input type="text" id="firstName" placeholder="请输入姓名">
          </div>
          <div class="form-group">
            <label for="phone">手机号</label>
            <input type="text" id="phone" placeholder="请输入手机号">
          </div>
        </div>
        <div class="form-group">
          <label for="regEmail">邮箱</label>
          <input type="email" id="regEmail" placeholder="请输入邮箱">
        </div>
        <div class="form-group">
          <label for="regPassword">密码</label>
          <input type="password" id="regPassword" placeholder="请输入密码">
        </div>
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input type="password" id="confirmPassword" placeholder="请确认密码">
        </div>
        <div class="form-group radio-group">
          <label>性别</label>
          <div class="radio-options">
            <input type="radio" id="male" name="gender" checked>
            <label for="male" class="radio-label">男</label>
            <input type="radio" id="female" name="gender">
            <label for="female" class="radio-label">女</label>
          </div>
        </div>
        <div class="form-group">
          <label for="interest">兴趣爱好</label>
          <select id="interest">
            <option value="">请选择</option>
            <option value="sports">运动</option>
            <option value="music">音乐</option>
            <option value="reading">阅读</option>
          </select>
        </div>
        <div class="form-group">
          <label for="message">个人简介</label>
          <textarea id="message" placeholder="请输入个人简介"></textarea>
        </div>
        <div class="form-group checkbox-group">
          <input type="checkbox" id="agree">
          <label for="agree">我已阅读并同意<a href="#">用户协议</a></label>
        </div>
        <button type="submit" class="btn btn-primary">注册</button>
      </form>
    </section>
  </div>
</body>
</html>
  1. 添加CSS样式
css
/* 重置样式 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: "Microsoft YaHei", Arial, sans-serif;
  background-color: #f5f5f5;
  padding: 50px 20px;
}

.container {
  max-width: 800px;
  margin: 0 auto;
}

h1 {
  text-align: center;
  color: #333;
  margin-bottom: 40px;
}

.demo-section {
  margin-bottom: 40px;
  padding: 30px;
  background-color: white;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.demo-section h2 {
  color: #555;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 2px solid #ddd;
}

/* 表单样式 */
form {
  max-width: 600px;
  margin: 0 auto;
}

.form-group {
  margin-bottom: 20px;
}

.form-row {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
}

.form-row .form-group {
  flex: 1;
  margin-bottom: 0;
}

label {
  display: block;
  margin-bottom: 8px;
  color: #555;
  font-weight: 600;
}

input[type="text"],
input[type="email"],
input[type="password"],
select,
textarea {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  font-family: inherit;
  transition: all 0.3s ease;
}

input[type="text"]:focus,
input[type="email"]:focus,
input[type="password"]:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
}

/* 复选框样式 */
.checkbox-group {
  display: flex;
  align-items: center;
}

.checkbox-group input[type="checkbox"] {
  margin-right: 8px;
  width: auto;
}

.checkbox-group label {
  margin-bottom: 0;
  font-weight: normal;
}

.checkbox-group a {
  color: #3498db;
  text-decoration: none;
}

.checkbox-group a:hover {
  text-decoration: underline;
}

/* 单选框样式 */
.radio-group {
  margin-bottom: 20px;
}

.radio-group label {
  margin-bottom: 10px;
}

.radio-options {
  display: flex;
  gap: 30px;
}

.radio-label {
  position: relative;
  padding-left: 30px;
  cursor: pointer;
  display: inline-block;
  font-weight: normal;
}

.radio-label::before {
  content: '';
  position: absolute;
  left: 0;
  top: 2px;
  width: 20px;
  height: 20px;
  border: 2px solid #ddd;
  border-radius: 50%;
  transition: all 0.3s ease;
}

input[type="radio"] {
  display: none;
}

input[type="radio"]:checked + .radio-label::before {
  border-color: #3498db;
  background-color: #3498db;
}

input[type="radio"]:checked + .radio-label::after {
  content: '';
  position: absolute;
  left: 7px;
  top: 9px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background-color: white;
}

/* 按钮样式 */
.btn {
  display: inline-block;
  padding: 12px 30px;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  width: 100%;
}

.btn-primary {
  background-color: #3498db;
  color: white;
}

.btn-primary:hover {
  background-color: #2980b9;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(52, 152, 219, 0.3);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .form-row {
    flex-direction: column;
  }
  
  .radio-options {
    flex-direction: column;
    gap: 10px;
  }
}
  1. 运行页面:在浏览器中打开文件,观察效果

预期效果

  • 登录表单:包含邮箱、密码输入框和记住我复选框
  • 注册表单:包含姓名、手机号、邮箱、密码、确认密码、性别单选框、兴趣爱好下拉框、个人简介文本域和同意协议复选框
  • 表单元素:所有表单元素都有美观的样式,包括焦点状态、悬停状态
  • 响应式设计:在小屏幕设备上自动调整布局

© 2026 编程马·菜鸟教程 版权所有