登录表单

创建于 2024-12-03 / 24
字体: [默认] [大] [更大]

了解如何使用 CSS 创建响应式登录表单。


点击按钮打开登录表单:


× Avatar Forgot password?

亲自试一试 »


如何创建登录表单

步骤 1) 添加 HTML:

在容器内添加图像并为每个字段添加输入(带有匹配的标签)。 在它们周围包裹一个 <form> 元素来处理输入。 您可以在我们的 PHP 教程中了解有关如何处理输入的更多信息。

实例

<form action="action_page.php" method="post">
  <div class="imgcontainer">
    <img src="img_avatar2.png" alt="Avatar" class="avatar">
  </div>

  <div class="container">
    <label for="uname"><b>Username</b></label>
    <input type="text" placeholder="Enter Username" name="uname" required>

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

    <button type="submit">Login</button>
    <label>
      <input type="checkbox" checked="checked" name="remember"> Remember me
    </label>
  </div>

  <div class="container" style="background-color:#f1f1f1">
    <button type="button" class="cancelbtn">Cancel</button>
    <span class="psw">Forgot <a href="#">password?</a></span>
  </div>
</form>

步骤 2) 添加 CSS:

实例

/* 边框形式 */
form {
  border: 3px solid #f1f1f1;
}

/* 全角输入 */
input[type=text], input[type=password] {
  width: 100%;
  padding: 12px 20px;
  margin: 8px 0;
  display: inline-block;
  border: 1px solid #ccc;
  box-sizing: border-box;
}

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

/* 为按钮添加悬停效果 */
button:hover {
  opacity: 0.8;
}

/* 取消按钮的额外样式(红色)*/
.cancelbtn {
  width: auto;
  padding: 10px 18px;
  background-color: #f44336;
}

/* 在此容器内居中头像图像 */
.imgcontainer {
  text-align: center;
  margin: 24px 0 12px 0;
}

/* Avatar image */
img.avatar {
  width: 40%;
  border-radius: 50%;
}

/* 为容器添加填充 */
.container {
  padding: 16px;
}

/* The "Forgot password" text */
span.psw {
  float: right;
  padding-top: 16px;
}

/* 在超小屏幕上更改跨度和取消按钮的样式 */
@media screen and (max-width: 300px) {
  span.psw {
    display: block;
    float: none;
  }
  .cancelbtn {
    width: 100%;
  }
} 亲自试一试 »

如何创建模态登录表单

步骤 1) 添加 HTML:

实例

<!-- 打开模式登录表单的按钮 -->
<button onclick="document.getElementById('id01').style.display='block'">Login</button>

<!-- 模态 -->
<div id="id01" class="modal">
  <span onclick="document.getElementById('id01').style.display='none'"
class="close" title="Close Modal">&times;</span>

  <!-- 模态内容 -->
  <form class="modal-content animate" action="/action_page.php">
    <div class="imgcontainer">
      <img src="img_avatar2.png" alt="Avatar" class="avatar">
    </div>

    <div class="container">
      <label for="uname"><b>Username</b></label>
      <input type="text" placeholder="Enter Username" name="uname" required>

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

      <button type="submit">Login</button>
      <label>
        <input type="checkbox" checked="checked" name="remember"> Remember me
      </label>
    </div>

    <div class="container" style="background-color:#f1f1f1">
      <button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button>
      <span class="psw">Forgot <a href="#">password?</a></span>
    </div>
  </form>
</div>
步骤 2) 添加 CSS:

实例

/* 模态(背景) */
.modal {
  display: none; /* 默认隐藏 */
  position: fixed; /* 原地不动 */
  z-index: 1; /* 坐在上面 */
  left: 0;
  top: 0;
  width: 100%; /* 全宽 */
  height: 100%; /* 全高 */
  overflow: auto; /* 如果需要,启用滚动 */
  background-color: rgb(0,0,0); /* 后备颜色 */
  background-color: rgba(0,0,0,0.4); /* 黑色带不透明度 */
  padding-top: 60px;
}

/* 模态内容/框 */
.modal-content {
  background-color: #fefefe;
  margin: 5px auto; /* 15% 从顶部居中 */
  border: 1px solid #888;
  width: 80%; /* 可能或多或少,取决于屏幕大小 */
}

/* 关闭按钮 */
.close {
   /* 将其定位在模态框外的右上角 */
  position: absolute;
  right: 25px;
  top: 0;
  color: #000;
  font-size: 35px;
  font-weight: bold;
}

/* Close button on hover */
.close:hover,
.close:focus {
  color: red;
  cursor: pointer;
}

/* 添加缩放动画 */
.animate {
  -webkit-animation: animatezoom 0.6s;
  animation: animatezoom 0.6s
}

@-webkit-keyframes animatezoom {
  from {-webkit-transform: scale(0)}
  to {-webkit-transform: scale(1)}
}

@keyframes animatezoom {
  from {transform: scale(0)}
  to {transform: scale(1)}
}

提示:您还可以使用以下 javascript 通过单击模式内容外部来关闭模式(而不仅仅是使用"x"或"取消"按钮来关闭它):

实例

<script>
// Get the modal
var modal = document.getElementById('id01');

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
</script> 亲自试一试 »

提示:转到我们的HTML 表单教程,了解有关 HTML 表单的更多信息。

提示:请转到我们的CSS 表单教程,了解有关如何设置表单元素样式的更多信息。



0 人点赞过