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>
相关推荐
岱宗夫up4 小时前
FastAPI入门(上篇):快速构建高性能Python Web API
开发语言·前端·python·fastapi
紫陌涵光4 小时前
112. 路径总和
java·前端·算法
漠月瑾-西安4 小时前
CVE-2025-55182漏洞解析:你的React项目安全吗?
前端·安全·react.js
No丶slovenly5 小时前
flutter笔记-输入框
前端·笔记·flutter
国产化创客5 小时前
ESP32+Web实现智能气象站
前端·物联网·智能家居·智能硬件
coderYYY6 小时前
VSCode终端启动报错
前端·ide·vscode·npm·编辑器
tod1137 小时前
Redis 数据类型与 C++ 客户端实践指南(redis-plus-plus)
前端·c++·redis·bootstrap·html
Sylvia33.7 小时前
火星数据:棒球数据API
java·前端·人工智能
weixin199701080167 小时前
1688商品详情页前端性能优化实战
前端·性能优化
DEMO派7 小时前
前端常用XSS攻击演示与防御方案解析
前端·xss