当前位置: 首页 > news >正文

css 设置 input 插入光标样式

caret-color 可以设置光标颜色 

caret-color: red;

但只能更改颜色,粗细位置不能更改,可以自定义光标,由于 input不能使用 ::after、::before 伪元素,所以可以用 div 自定义

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Custom Cursor</title>
<style>.custom-cursor {position: relative;padding-left: 5px;caret-color: transparent;display: inline-block;width: 200px;}.custom-cursor:focus::after {content: '';position: absolute;top: 5px;width: 2px; /* 调整这个值来改变光标的“粗细” */height: 63%;background-color: red;animation: blink 0.8s step-end infinite;}.insert-input {display: inline-flex;position: relative;align-items: center;input {padding-left: 6px;caret-color: transparent;}}.insert-cursor {position: absolute;margin-left: 0px;margin-top: -1px;width: 2px; /* 调整这个值来改变光标的“粗细” */height: 68%;background-color: red;opacity: 0;}.insert-input:has(.input-text:focus) {.insert-cursor {margin-left: 5.5px;animation: blink 0.8s step-end infinite;opacity: 1;}}@keyframes blink {from, to { background-color: transparent; }50% { background-color: red;    }}
</style>
</head>
<body>
<div class="custom-cursor" contenteditable="true">在这里输入文本...</div><br>
<div class="insert-input"><span class="insert-cursor"></span><input class="input-text" type="text" autocomplete="off" placeholder="请输入内容">
</div>
</body>
</html>

 

但是需要注意的是,用 div 做的输入框不安全,有被劫持风险,光标暂不可以像正常光标那样插入到指定位置,input 光标位置不是放在输入文本后面,可能需要 js 动态设置位置,有待改善。

http://www.dtcms.com/a/270480.html

相关文章:

  • OpenCV图片操作100例:从入门到精通指南(2)
  • java17 gc笔记
  • 论文阅读|汽车虚拟环绕音响系统设计与实现策略的比较研究
  • 新加坡国立大学基于多维度EHR数据实现细粒度患者队列建模,住院时间预测准确率提升16.3%
  • Android网络层架构:统一错误处理的问题分析到解决方案与设计实现
  • java中list.remove(item); // 直接移除会导致ConcurrentModificationException
  • Android ViewModel机制与底层原理详解
  • N8N与Dify:自动化与AI的完美搭配
  • 零基础Qt 5 安装教程
  • 【深度学习新浪潮】什么是蛋白质反向折叠模型?
  • cad_recognition 笔记
  • 前端规范化设计详解
  • ORA-600 kokiasg1故障分析---惜分飞
  • [1-01-01].第50节:泛型 - 泛型的使用
  • Python标准库 bisect 模块
  • 云原生技术与应用-容器技术技术入门与Docker环境部署
  • 【洛谷题单】--顺序结构(一)
  • OSPFv3与OSPFv2不同点
  • eslint扁平化配置
  • Linux守护进程
  • 【ES实战】ES客户端线程量分析
  • java-网络编程
  • Java中数组与链表的性能对比:查询与增删效率分析
  • RabbitMQ第二章(RocketMQ的五大工作模式)
  • 【Linux服务器】-安装ftp与sftp服务
  • 数据结构:数组:合并数组(Merging Arrays)
  • 20 道 Node.js 高频面试题
  • Codeforces Round 868 (Div. 2) D. Unique Palindromes(1900,构造)
  • 深入企业内部的MCP知识(四):FastMCP装饰器与类方法:正确结合面向对象与MCP组件的实践指南
  • 4.权重衰减(weight decay)