大家好,我是 Just,这里是「设计师工作日常」,今天分享的是一款简约大方的动态输入框,适用于表单提交、账号登录入口。

《有趣的css》系列最新实例通过公众号「设计师工作日常」发布。


(目录)

整体效果

a3c8ec48959e4915aa662ef00af032ed.gif

使用 input 标签配合 transition 过渡属性实现输入框底部线条动态显示。

一款简约大方的动态输入框,适用于表单提交、账号登录入口。


核心代码部分,简要说明了写法思路;完整代码在最后,可直接复制到本地运行。

核心代码

html 代码

<label>
  <input type="text" placeholder="在此输入文字" required>
  <span class="line"></span>
</label>

input 标签设置 required 判断是否为空,span 标签画输入框底部线条。

css 部分代码

label{
  position: relative;
}
input{
  width: 140px;
  height: 36px;
  line-height: 36px;
  outline-style: none;
  font-size: 16px;
  color: #333;
  border: none;
  padding: 0 8px;
  box-sizing: border-box;
}
.line{
  width: 0;
  height: 2px;
  display: block;
  background-color: #4158D0;
  background-image: linear-gradient(90deg, #4158D0 0%, #C850C0 48%, #FFCC70 100%);
  transition: all 0.24s ease-in-out;
}
input:focus+.line,input:valid+.line{
  width: 100%;
}

input 自定义样式去掉边框,随着 :focus:valid 判断输入框状态,来实现 span 线条变化过渡动画。

完整代码如下

html 页面

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css">
    <title>简约大方的输入框</title>
  </head>
  <body>
    <div class="app">
      <label>
        <input type="text" placeholder="在此输入文字" required>
        <span class="line"></span>
      </label>
    </div>
  </body>
</html>

css 样式

/** style.css **/
.app{
  width: 100%;
  height: 100vh;
  background-color: #ffffff;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}
label{
  position: relative;
}
input{
  width: 140px;
  height: 36px;
  line-height: 36px;
  outline-style: none;
  font-size: 16px;
  color: #333;
  border: none;
  padding: 0 8px;
  box-sizing: border-box;
}
.line{
  width: 0;
  height: 2px;
  display: block;
  background-color: #4158D0;
  background-image: linear-gradient(90deg, #4158D0 0%, #C850C0 48%, #FFCC70 100%);
  transition: all 0.24s ease-in-out;
}
input:focus+.line,input:valid+.line{
  width: 100%;
}

页面渲染效果

a3c8ec48959e4915aa662ef00af032ed.gif

以上就是所有代码,以及简单的思路,希望对你有一些帮助或者启发。


CSS 是一种很酷很有趣的计算机语言,在这里跟大家分享一些 CSS 实例 Demo,为学习者获取灵感和思路提供一点帮助,希望你们喜欢。

我是 Just,这里是「设计师工作日常」,求点赞求关注!