CSS3搜索框

129 阅读1分钟

1.png

2.png

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    *,*:before,*:after {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }
    body{
      background: #333;
    }

    .wrap {
      width: 300px;
      height: 300px;
      margin: auto;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
    }
    
    .wrap .search{
      width: 80px;
      height: 80px;
      margin: auto;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      background: red;
      border-radius: 50%;
      box-shadow: 0 0 20px;
      transition: all 1.5s;
    }
    .wrap .search:before{
      content: '';
      width: 30px;
      height: 30px;
      margin: auto;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      border-radius: 50%;
      border: 2px solid white;
    }
    .wrap .search:after{
      content: '';
      width: 10px;
      height: 2px;
      margin: auto;
      position: absolute;
      top: 0;
      right: -30px;
      bottom: -30px;
      left: 0;
      transform: rotate(45deg);
      background: white;
    }

    .wrap input{
      width: 300px;
      height: 50px;
      margin: auto;
      position: absolute;
      z-index: 2;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      border: 2px solid orange;
      background: #333;
      color: white;
      transition: all 1.5s;
      padding: 0 10px;
      outline: none;
      opacity: 0;
    }
    .wrap input:focus{
      opacity: 1;
      width: 300px;
    }
    .wrap input:focus ~ .search{
      z-index: 3;
      right: -360px;
    }
    .wrap input:focus ~ .search:before{
      content: '';
      width: 30px;
      height: 2px;
      margin: auto;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      background: white;
      transform: rotate(45deg);
      border: none;
      border-radius: 0;
    }
    .wrap input:focus ~ .search:after{
      content: '';
      width: 30px;
      height: 2px;
      margin: auto;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      background: white;
      transform: rotate(-45deg);
    }
  </style>
</head>

<body>
  <div class="wrap">
    <input type="text" placeholder="Search...">
    <div class="search"></div>
  </div>
</body>

</html>