css 输入框动态特效

先上图

代码

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css 输入框动效</title>
    <style>
        .inputBox {
            position: relative;
            width: 250px;
        }

        .inputBox input {
            width: 100%;
            padding: 10px;
            border: 1px solid #cccccc;
            border-radius: 5px;
            font-size: 1em;
            outline: none;
        }

        .inputBox span {
            position: absolute;
            left: 0;
            top: 0;
            padding: 10px;
            font-size: 1em;
            pointer-events: none;
            color: #cccccc;
        }

        .inputBox input:valid ~ span,
        .inputBox input:focus ~ span {
            color: orangered;
            transform: translateX(10px) translateY(-7px);
            font-size: 0.65em;
            padding: 0 10px;
            background: #ffffff;
        }
    </style>
</head>
<body>
<div class="inputBox">
    <input type="text" required/>
    <span>用户名</span>
</div>
</body>
</html>
相关推荐
2分钟速写快排5 小时前
孔乙己的茴字有多少种写法?|AI 对话实时通信的三种实现
前端·javascript·github
Penge6667 小时前
JavaScript:BOM 详解
前端
Penge6667 小时前
Vite vs Webpack:前端构建工具原理与实战
前端
Penge6667 小时前
前端包管理工具:npm、Yarn、pnpm 快速入门
前端
Penge6668 小时前
Sass 了解
前端
Penge6668 小时前
Webpack 打包从 0 到 1 实战笔记
前端
Penge6668 小时前
Webpack 入门:是什么、为什么需要、怎么使用?
前端
Penge6669 小时前
Ant Design(Antd)了解
前端
wangruofeng9 小时前
同一行代码,百种渲染:Asuka、React Native 与 Flutter 跨平台渲染机制深度拆解
前端·github
Penge6669 小时前
JavaScript 核心语法速通
前端