How to do floating of labels in CSS

后端 未结 6 1062
死守一世寂寞
死守一世寂寞 2021-01-30 09:03

I want to display the label of an input inside its input, so that when I click the input, the label will animate and go above the input and change the styles of the input\'s bor

6条回答
  •  逝去的感伤
    2021-01-30 09:43

    .box {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 200px;
      background: #fff;
      padding: 40px;
      border: 1px solid red;
      box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
    }
    
    .box input {
      padding: 10px 0;
      margin-bottom: 30px;
    }
    
    .box input {
      width: 100%;
      box-sizing: border-box;
      box-shadow: none;
      outline: none;
      border: none;
      border-bottom: 2px solid #999;
    }
    
    .box form div {
      position: relative;
    }
    
    .box form div label {
      position: absolute;
      top: 10px;
      left: 0;
      color: #999;
      transition: 0.5s;
      pointer-events: none;
    }
    
    .box input:focus~label,
    .box input:valid~label {
      top: -12px;
      left: 0;
      color: red;
    }
    
    .box input:focus,
    .box input:valid {
      border-bottom: 2px solid red;
    }

提交回复
热议问题