CSS改变插入光标颜色caret-color简介及其它变色方法(转)

本文介绍如何使用CSS的caret-color属性改变输入框光标颜色,并提供了一种兼容多种浏览器的方法。此外,还探讨了如何在移动端使用rem布局而无需JavaScript。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、CSS改变输入框光标颜色的原生属性caret-color

CSS caret-color属性可以改变输入框插入光标的颜色,同时又不改变输入框里面的内容的颜色。

例如:

input {
    color: #333;
    caret-color: red;
}

结果光标颜色变成红色,文字还是深黑色:

光标颜色变红截图

眼见为实,您可以狠狠的点击这里:CSS caret-color改变光标颜色demo

//zxx: 单词caret表示“插入符号”,指处于内容可插入状态的光标。


caret-color属性不仅对于原生的输入表单控件有效,设置contenteditable的普通HTML标签也适用。

例如:

[contenteditable="true"] {
    width: 120px;
    border: 1px solid #ddd;
    padding: 3px;
    line-height: 20px;
    color: #333;
    caret-color: red;
}
<div contenteditable="true">文字</div>

效果如下图:
普通可编辑元素的光标颜色设置

兼容性

caret-color属性目前Chrome和Firefox基本上可以放心使用,但是Safari以及IE浏览器则还需要等待一些时日。

具体兼容性数据见下截图:

caret-color兼容性数据

下面问题来了,对于这些不兼容的浏览器,有没有什么其他办法可以让他们也能实现插入状态光标的变色效果呢?

二、其他方法改变输入框的闪烁的光标颜色

对于IE浏览器,其光标颜色看上去是永远固定的黑色,并不跟随输入框的颜色color变化,因此对于IE浏览器,是没有什么好方法的。

但是,对于Safari浏览器,由于输入框控件的闪烁光标颜色是和设置的color属性颜色一致,因此我们是有手段可以对光标进行控制的。

具体实现代码如下:

input {
  color: red;
}
input::first-line {
  color: #333;
}

于是效果即达成。

您可以狠狠地点击这里:借助::first-line改变插入光标颜色demo

Safari浏览器下截图效果如下:

Safari浏览器下光标颜色变化

借助::first-line伪元素的方法在Chrome,Safari浏览器下表现良好,但是Firefox浏览器并不支持,其表现为<input>输入框里面的内容不属于::first-line,因此,整个输入框文字都是红色。

对于不支持::first-line方法的浏览器,相关CSS会污染正常的样式表现,因此我们需要区分处理,例如可以这样:

input, input::first-line {
    color: #333;
}
@supports (-webkit-mask: none) {
    input { color: red; }
}

然而这种方法也有局限性,对于<textarea>这种多行输入控件就无能为力,因为::first-line只能控制首行元素颜色。

三、两种实现方法综合

综合上面两种方法,可以得到最佳实践如下:

如果浏览器支持caret-color属性,优先使用caret-color(Chrome/Firefox/Opera);其次使用::first-line方法(Safari);最后忽略(如IE)。

整合后CSS如下:

input {
    color: #333;
    caret-color: red;
}
@supports (-webkit-mask: none) and (not (cater-color: red)) {
    input { color: red; }
    input::first-line { color: #333; }
}

效果如下截图(Firefox截图):

Firefox浏览器下混合方法处理下的闪烁光标

您可以狠狠的点击这里:caret-color加first-line改变输入光标颜色demo

rem 布局不再使用 JavaScript 设置

这里不探讨 rem 的原理以及细节,还不熟悉的童鞋建议去恶补一下。

需求

有时候,移动端用 rem 布局时候,根据不同的屏幕宽度要设置不同的 font-size 来做到适配,要写一坨 JS 来设置,能不能不用JS呢?

例如:以 750px 设计稿作为基准,根节点设置 font-size 为 100px ,只考虑 DPR 为 2 的情况,只考虑最简单的情况

 document.querySelector('html').style.fontSize = `${window.innerWidth / 7.5 }px`;
代码

现在移动端 css3 单位 vw ,wh 兼容性已经很不错了,在不需要兼容太低版本的安卓机情况下可以这样来:

html{
    font-size: 100vw / 7.5
}

转载于:https://www.cnblogs.com/sunshq/p/9232834.html

### CSS `caret-color` Property Usage and Examples The `caret-color` property specifies the color of the caret (blinking cursor) in text fields or editable elements. This allows developers to customize the appearance of the blinking insertion marker, enhancing user experience by making it more visible against different backgrounds. For example, setting a specific color can be done as follows: ```css textarea { caret-color: red; } ``` When no value is specified for this attribute, browsers typically default to an automatic behavior where they choose a suitable contrasting color based on the element&#39;s background[^1]. In cases when working with transitions similar to those applied via JavaScript updates like changing styles dynamically after certain events occur within web pages, one might consider applying such changes carefully so that users are not confused about their interaction points during rapid UI transformations[^2]. However, direct application scenarios involving `caret-color` do not usually involve complex animations unless explicitly programmed into custom components. #### Example Demonstrating Caret Color Change Based On Conditions To illustrate how conditions could affect caret visibility through styling adjustments including but not limited to altering its hue upon focus state activation: ```html <style> input:focus { caret-color: blue; /* Changes caret color when input field gains focus */ } div.editable { -moz-user-modify: read-write; user-select: text; caret-color: orange; /* Sets caret color inside contenteditable divs */ } </style> <input type="text" placeholder="Click here..."> <div class="editable" contentEditable="true"> Type something... </div> ``` This code snippet shows two instances—one targeting form inputs specifically while another applies broadly across all inline-editable HTML sections—to demonstrate versatility regarding placement alongside other properties without interference.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值