如何创建:注册表单

学习如何使用 CSS 创建响应式注册表单。

单击按钮可打开注册表单:

亲自试一试

如何创建注册表单

第一步 - 添加 HTML:

使用 <form> 元素来处理输入。您可以在我们的 PHP 教程中了解更多相关信息。

然后为每个字段添加输入控件(并带有匹配的标签):

<form action="action_page.php" style="border:1px solid #ccc">
  <div class="container">
    <h1>Sign Up</h1>
    <p>Please fill in this form to create an account.</p>
    <hr>

    <label for="email"><b>Email</b></label>
    <input type="text" placeholder="Enter Email" name="email" required>

    <label for="psw"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="psw" required>

    <label for="psw-repeat"><b>Repeat Password</b></label>
    <input type="password" placeholder="Repeat Password" name="psw-repeat" required>

    <label>
    <input type="checkbox" checked="checked" name="remember" style="margin-bottom:15px">Remember me
    </label>

    <p>By creating an account you agree to our <a href="#" style="color:dodgerblue">Terms & Privacy</a>.</p>

    <div class="clearfix">
      <button type="button" class="cancelbtn">Cancel</button>
      <button type="submit" class="signupbtn">Sign Up</button>
    </div>
  </div>
</form>

第二步 - 添加 CSS:

* {box-sizing: border-box}

/* 全宽输入字段 */
  input[type=text], input[type=password] {
  width: 100%;
  padding: 15px;
  margin: 5px 0 22px 0;
  display: inline-block;
  border: none;
  background: #f1f1f1;
}

input[type=text]:focus, input[type=password]:focus {
  background-color: #ddd;
  outline: none;
}

hr {
  border: 1px solid #f1f1f1;
  margin-bottom: 25px;
}

/* 为所有按钮设置样式 */
button {
  background-color: #04AA6D;
  color: white;
  padding: 14px 20px;
  margin: 8px 0;
  border: none;
  cursor: pointer;
  width: 100%;
  opacity: 0.9;
}

button:hover {
  opacity:1;
}

/* 设置取消按钮的额外样式 */
.cancelbtn {
  padding: 14px 20px;
  background-color: #f44336;
}

/* 使取消和注册按钮浮动并添加等宽 */
.cancelbtn, .signupbtn {
  float: left;
  width: 50%;
}

/* 向容器元素添加内边框 */
.container {
  padding: 16px;
}

/* 清除浮标 */
.clearfix::after {
  content: "";
  clear: both;
  display: table;
}

/* 更改超小屏幕上取消按钮和注册按钮的样式 */
@media screen and (max-width: 300px) {
  .cancelbtn, .signupbtn {
    width: 100%;
  }
}

亲自试一试

如何创建模态注册表单

第一步 - 添加 HTML:

使用 <form> 元素来处理输入。您可以在我们的 PHP 教程中了解更多相关信息。

然后为每个字段添加输入控件(并带有匹配的标签):

<!-- Button to open the modal -->
<button onclick="document.getElementById('id01').style.display='block'">Sign Up</button>

<!-- The Modal (contains the Sign Up form) -->
<div id="id01" class="modal">
  <span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">times;</span>
  <form class="modal-content" action="/action_page.php">
    <div class="container">
      <h1>Sign Up</h1>
      <p>Please fill in this form to create an account.</p>
      <hr>
      <label for="email"><b>Email</b></label>
      <input type="text" placeholder="Enter Email" name="email" required>

      <label for="psw"><b>Password</b></label>
      <input type="password" placeholder="Enter Password" name="psw" required>

      <label for="psw-repeat"><b>Repeat Password</b></label>
      <input type="password" placeholder="Repeat Password" name="psw-repeat" required>

      <label>
        <input type="checkbox" checked="checked" name="remember" style="margin-bottom:15px"> Remember me
      </label>

      <p>By creating an account you agree to our <a href="#" style="color:dodgerblue">Terms & Privacy</a>.</p>

      <div class="clearfix">
        <button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button>
        <button type="submit" class="signup">Sign Up</button>
      </div>
    </div>
  </form>
</div>

第二步 - 添加 CSS:

* {box-sizing: border-box}
/* Full-width input fields */
  input[type=text], input[type=password] {
  width: 100%;
  padding: 15px;
  margin: 5px 0 22px 0;
  display: inline-block;
  border: none;
  background: #f1f1f1;
}

/* 当输入框获得焦点时添加背景颜色 */
input[type=text]:focus, input[type=password]:focus {
  background-color: #ddd;
  outline: none;
}

/* 为所有按钮设置样式 */
button {
  background-color: #04AA6D;
  color: white;
  padding: 14px 20px;
  margin: 8px 0;
  border: none;
  cursor: pointer;
  width: 100%;
  opacity: 0.9;
}

button:hover {
  opacity:1;
}

/* 设置取消按钮的额外样式 */
.cancelbtn {
  padding: 14px 20px;
  background-color: #f44336;
}

/* 使取消和注册按钮浮动并设置相等的宽度 */
.cancelbtn, .signupbtn {
  float: left;
  width: 50%;
}

/* 向容器元素添加内边距 */
.container {
  padding: 16px;
}

/* 模态(背景) */
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: #474e5d;
  padding-top: 50px;
}

/* 模态内容/框 */
.modal-content {
  background-color: #fefefe;
  margin: 5% auto 15% auto; /* 距顶部 5%,距底部 15%,并且居中 */
  border: 1px solid #888;
  width: 80%; /* 可能更多或更少,具体取决于屏幕尺寸 */
}

/* 设置水平线的样式 */
hr {
  border: 1px solid #f1f1f1;
  margin-bottom: 25px;
}

/* 关闭按钮 (x) */
.close {
  position: absolute;
  right: 35px;
  top: 15px;
  font-size: 40px;
  font-weight: bold;
  color: #f1f1f1;
}

.close:hover,
.close:focus {
  color: #f44336;
  cursor: pointer;
}

/* Clear floats */
.clearfix::after {
  content: "";
  clear: both;
  display: table;
}

/* 更改超小屏幕上取消按钮和注册按钮的样式 */
@media screen and (max-width: 300px) {
  .cancelbtn, .signupbtn {
    width: 100%;
  }
}

提示:您还可以使用以下 JavaScript 代码,通过点击模态框内容之外的区域来关闭模态框(而不仅仅是通过使用 "×" 或 "cancel" 按钮来关闭它):

<script>
// 获取模态
var modal = document.getElementById('id01');

// 当用户单击模式之外的任何位置时,将其关闭
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
</script>

亲自试一试

相关页面

教程:HTML 表单

教程:CSS 表单