需要HTML输入会弄乱我的输入

时间:2019-01-26 05:48:25

标签: html css html5

所以我不想使用“ Required”属性,但是如果我不把它放在那儿,它会完全弄乱我希望表单显示的方式。

The top "Email" input is the one with the required attribute and the bottom does not haveit.

那为什么要删除此更改呢?它的设置是这样,当您单击“电子邮件”时,它将移开,看起来像输入密码的那个。

这是我的html

    <form action="./includes/login.inc.php" method="post">
        <div class="group">
            <input type="text" name="emailuid" required=""/><span class="highlight"></span><span class="bar"></span>
            <label>Email</label>
        </div>
        <div class="group">
            <input type="password" name="pwduid"/><span class="highlight"></span><span class="bar"></span>
            <label>Password</label>
        </div>
        <div class="btn-box">
            <button class="btn btn-submit" type="submit" name="login-submit">Sign in</button>
        </div>
    </form>

这是我的CSS样式(仅用于表单!)

form {
  width: 320px;
  margin: 45px auto;
}
form h1 {
  font-size: 3em;
  font-weight: 300;
  text-align: center;
  color: #2196F3;
}
form h5 {
  text-align: center;
  text-transform: uppercase;
  color: #c6c6c6;
}
form hr.sep {
  background: #2196F3;
  box-shadow: none;
  border: none;
  height: 2px;
  width: 25%;
  margin: 0px auto 45px auto;
}
form .emoji {
  font-size: 1.2em;
}

.group {
  position: relative;
  margin: 45px 0;
}

textarea {
  resize: none;
}

input,
textarea {
  background: none;
  color: #4F4F4F;
  font-size: 18px;
  padding: 10px 10px 10px 5px;
  display: block;
  width: 320px;
  border: none;
  border-radius: 0;
  border-bottom: 1px solid #4F4F4F;
}
input:focus,
textarea:focus {
  outline: none;
}
input:focus ~ label, input:valid ~ label,
textarea:focus ~ label,
textarea:valid ~ label {
  top: -14px;
  font-size: 12px;
  color: #FF3B3F;
}
input:focus ~ .bar:before,
textarea:focus ~ .bar:before {
  width: 335px;
}

input[type="password"] {
  letter-spacing: 0.3em;
}

label {
  color: #4F4F4F;
  font-size: 16px;
  font-weight: normal;
  position: absolute;
  pointer-events: none;
  left: 5px;
  top: 10px;
  -webkit-transition: 300ms ease all;
  transition: 300ms ease all;
}

.bar {
  position: relative;
  display: block;
  width: 320px;
}
.bar:before {
  content: '';
  height: 2px;
  width: 0;
  bottom: 0px;
  position: absolute;
  background: #FF3B3F;
  -webkit-transition: 300ms ease all;
  transition: 300ms ease all;
  left: 0%;
}

.btn {
  background: #fff;
  color: #959595;
  border: none;
  padding: 10px 20px;
  border-radius: 3px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  text-decoration: none;
  outline: none;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
  -webkit-transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
}
.btn:hover {
  color: #752021;
  box-shadow: 0 7px 14px rgba(0, 0, 0, 0.18), 0 5px 5px rgba(0, 0, 0, 0.12);
}
.btn.btn-link {
  background: #FF3B3F;
  color: #d3eafd;
}
.btn.btn-link:hover {
  background: #0d8aee;
  color: #deeffd;
}
.btn.btn-submit {
  background: #FF3B3F;
  color: white;
}
.btn.btn-submit:hover {
  background: #752021;
  color: white;
}
.btn.btn-cancel {
  background: #eee;
}
.btn.btn-cancel:hover {
  background: #e1e1e1;
  color: #8b8b8b;
}

.btn-box {
  text-align: center;
  margin: 50px 0;
}

我只是很困惑为什么它强制我使用必填属性,我不想使用它,因为我做了一个php检查器来查看字段是否为空。这让我的外观看起来有些混乱。

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

TLDR; 更改此代码:

input:focus ~ label,
input:valid ~ label,
textarea:focus ~ label,
textarea:valid ~ label
{
  top: -14px;
  font-size: 12px;
  color: #FF3B3F;
}

对此:

input:focus ~ label,
textarea:focus ~ label
{
  top: -14px;
  font-size: 12px;
  color: #FF3B3F;
}

说明:

信不信由你,CSS样式可以应用于特别具有某些属性的元素,因此,应始终确保检查CSS中与这些属性相对应的伪类。示例:input:required {...}对应于required元素上的input HTML属性。

为了使事情变得更加复杂,required属性可以确定是否在该元素上激活了其他伪类,例如:valid。因此,如果将元素设置为required,但未正确填写该字段,则任何指定:invalid伪类的CSS都会被自动激活。如果正确填写,则:valid伪类CSS属性将生效。

答案 1 :(得分:1)

因此您的CSS中的input:valid ~ label引起了问题。 试试这个:

form {
  width: 320px;
  margin: 45px auto;
}
.group {
  position: relative;
  margin: 45px 0;
}
input {
  background: none;
  color: #4F4F4F;
  font-size: 18px;
  padding: 10px 10px 10px 5px;
  display: block;
  width: 320px;
  border: none;
  border-radius: 0;
  border-bottom: 1px solid #4F4F4F;
}
input:focus {
  outline: none;
}
input:focus ~ label {
  top: -14px;
  font-size: 12px;
  color: #FF3B3F;
}
input:focus ~ .bar:before {
  width: 335px;
}

input[type="password"] {
  letter-spacing: 0.3em;
}

label {
  color: #4F4F4F;
  font-size: 16px;
  font-weight: normal;
  position: absolute;
  pointer-events: none;
  left: 5px;
  top: 10px;
  -webkit-transition: 300ms ease all;
  transition: 300ms ease all;
}

.bar {
  position: relative;
  display: block;
  width: 320px;
}
.bar:before {
  content: '';
  height: 2px;
  width: 0;
  bottom: 0px;
  position: absolute;
  background: #FF3B3F;
  -webkit-transition: 300ms ease all;
  transition: 300ms ease all;
  left: 0%;
}

.btn {
  background: #fff;
  color: #959595;
  border: none;
  padding: 10px 20px;
  border-radius: 3px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  text-decoration: none;
  outline: none;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
  -webkit-transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
}
.btn:hover {
  color: #752021;
  box-shadow: 0 7px 14px rgba(0, 0, 0, 0.18), 0 5px 5px rgba(0, 0, 0, 0.12);
}
.btn.btn-link {
  background: #FF3B3F;
  color: #d3eafd;
}
.btn.btn-link:hover {
  background: #0d8aee;
  color: #deeffd;
}
.btn.btn-submit {
  background: #FF3B3F;
  color: white;
}
.btn.btn-submit:hover {
  background: #752021;
  color: white;
}
.btn.btn-cancel {
  background: #eee;
}
.btn.btn-cancel:hover {
  background: #e1e1e1;
  color: #8b8b8b;
}

.btn-box {
  text-align: center;
  margin: 50px 0;
}
<form action="#" method="post">
  <div class="group">
    <input type="text" name="emailuid"/><span class="highlight"></span><span class="bar"></span>
    <label>Email</label>
  </div>
  <div class="group">
    <input type="password" name="pwduid"/><span class="highlight"></span><span class="bar"></span>
    <label>Password</label>
  </div>
  <div class="btn-box">
    <button class="btn btn-submit" type="submit" name="login-submit">Sign in</button>
  </div>
</form>