CSS中的伪类

时间:2017-03-28 18:32:34

标签: css

我目前正在制作一个页脚。您可以在图片中看到3个输入元素。现在,如果单击输入,则border-top将变为白色。现在我想实现:如果输入字段填充了文本而我单击另一个元素,则必须出现绿色border-top。我必须使用哪个伪类?

enter image description here     HTML:

        <div id="footer">
            <ul>
                <li> <input type="text" name="name" placeholder="Name"> </li>
                <li> <input type="text" name="email" placeholder="Email"> </li>
                <li> <input class="last" type="text" name="message" placeholder="Message"> </li>
            </ul>       
        </div>


CSS:

    #footer {
      width: 100%;
      background-color: rgba(0, 0, 0, 0.8);
      text-align: center;
      padding: 20px 0px;
    }

    input {
      padding: 10px 5%;
      background: rgba(0, 0, 0, 0.4);
      outline: none;
      width: 60%;
      margin-bottom: 30px;
      font-family: Roboto-Thin;
      color: white;
      border: none;
      border-bottom: 2px solid rgba(255, 255, 255, 0.0);
      border-top: 2px solid rgba(255, 255, 255, 0.0);
      font-size: 18px;
      transition: 0.2s;
    }

    .last {
      margin-bottom: 0px;
    }

    input:focus {
      border-top: 2px solid white;
    }

  [1]: https://i.stack.imgur.com/4ZE0K.png

1 个答案:

答案 0 :(得分:1)

由于您使用占位符,因此可以使用:not(:placeholder-shown):not(:focus)

&#13;
&#13;
#footer {
      width: 100%;
      background-color: rgba(0, 0, 0, 0.8);
      text-align: center;
      padding: 20px 0px;
    }

    input {
      padding: 10px 5%;
      background: rgba(0, 0, 0, 0.4);
      outline: none;
      width: 60%;
      margin-bottom: 30px;
      font-family: Roboto-Thin;
      color: white;
      border: none;
      border-bottom: 2px solid rgba(255, 255, 255, 0.0);
      border-top: 2px solid rgba(255, 255, 255, 0.0);
      font-size: 18px;
      transition: 0.2s;
    }

    .last {
      margin-bottom: 0px;
    }

    input:focus {
      border-top: 2px solid white;
    }

input:not(:placeholder-shown):not(:focus) {
  border-top: 1px solid green;
}
&#13;
<div id="footer">
  <ul>
    <li> <input type="text" name="name" placeholder="Name"> </li>
    <li> <input type="text" name="email" placeholder="Email"> </li>
    <li> <input class="last" type="text" name="message" placeholder="Message"> </li>
  </ul>
</div>
&#13;
&#13;
&#13;

相关问题