本文转载自:https://www.zhangxinxu.com/wordpress/2018/01/css-caret-color-first-line/

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



代码为:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
input{
color: #333;
caret-color:red;
}
</style>
</head>
<body>
<input type="text">
</body>
</html>

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

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
[contenteditable="true"]{
width: 120px;
border: 1px solid #ddd;
padding: 3px;
line-height: 20px;
color: #333;
caret-color: red;
}
</style>
</head>
<body>
<div contenteditable="true">文字</div>
</body>
</html>



兼容性

caret-color属性目前Chrome和Firefox基本上可以放心使用,但是Safari以及IE浏览器还有些问题。

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

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

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

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
input {
color: red;
}
input::first-line {
color: #333;
}
</style>
</head>
<body>
<input type="text">
</body>
</html>

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

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

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

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

三、两种实现方法综合

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

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

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

后记:珍惜时间,好好加油

---恢复内容结束---

本文转载自:https://www.zhangxinxu.com/wordpress/2018/01/css-caret-color-first-line/

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



代码为:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
input{
color: #333;
caret-color:red;
}
</style>
</head>
<body>
<input type="text">
</body>
</html>

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

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
[contenteditable="true"]{
width: 120px;
border: 1px solid #ddd;
padding: 3px;
line-height: 20px;
color: #333;
caret-color: red;
}
</style>
</head>
<body>
<div contenteditable="true">文字</div>
</body>
</html>



兼容性

caret-color属性目前Chrome和Firefox基本上可以放心使用,但是Safari以及IE浏览器还有些问题。

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

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

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

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
input {
color: red;
}
input::first-line {
color: #333;
}
</style>
</head>
<body>
<input type="text">
</body>
</html>

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

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

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

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

三、两种实现方法综合

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

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

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

后记:珍惜时间,好好加油

input光标使用caret-color改变颜色的更多相关文章

  1. QTabWiget Change Color 改变颜色

    Qt中的QTabWiget 类提供了一个标签控件,但是这个控件默认初始化的颜色是白色,和原窗口的颜色不同,看起来非常的违和,所以我们希望将其的背景颜色设为当前窗口的背景颜色.我们所要做的就是先将应用程 ...

  2. QTabWiget Change Color 改变颜色(每个QWidget都有一个自己的调色板palette,设置它的颜色,然后setAutoFillBackground即可)

    Qt中的QTabWiget 类提供了一个便签控件,但是这个控件默认初始化的颜色是白色,和原窗口的颜色不同,看起来非常的违和,所以我们希望将其的背景颜色设为当前窗口的背景颜色.我们所要做的就是先将应用程 ...

  3. 改变input光标颜色与输入字体颜色不同

    设置input css: color #ffd600text-shadow 0px 0px 0px #bababa -webkit-text-fill-color initial input, tex ...

  4. HTML5的input color系统颜色选择器

    前两天,我写了一篇<推荐两款jQuery色盘选择器>介绍,那是使用JavaScript实现的色盘,今天我给大家介绍HTML5的色盘选择器.HTML5有一个input类型为color,即颜色 ...

  5. android中自定义view---实现竖直方向的文字功能,文字方向朝上,同时提供接口,判断当前touch的是哪个字符,并改变颜色

    android自定义view,实现竖直方向的文字功能,文字方向朝上,同时提供接口,判断当前touch的是哪个字符,并改变颜色. 由于时间比较仓促,因此没有对代码进行过多的优化,功能远远不如androi ...

  6. TextView设置动态改变颜色

    通过TextView的setTextColor方法进行文本颜色的设置, 这里可以有3种方式进行设置: 第1种:tv.setTextColor(android.graphics.Color.RED);/ ...

  7. input长度随输入内容动态变化 input光标定位在最右侧

    <input type="text" onkeydown="this.onkeyup();" onkeyup="this.size=(this. ...

  8. input 光标在 chrome下不兼容 解决方案

    input 光标在 chrome下不兼容 解决方案 height: 52px; line-height: normal; line-height:52px\9 .list li input[type= ...

  9. EditText搜索关键字,返回结果匹配关键字改变颜色

    自己项目 用到EditText搜索结果关键字改变颜色,就研究了一下,2种方法实现,发现一个好用的工具类,在代码中一行调用这个方法,直接实现需求. KeywordUtil.java工具类. packag ...

随机推荐

  1. jsonp的原理,应用场景,优缺点

    在开发测试中,难免会在不同域下进行跨域操作,出于安全性考虑,浏览器中的同源策略阻止从一个域上加载的脚本获取或者操作 另一个域下的文档属性,这时需要进行跨域的方式进行解决,如:使用jsonp ,ifra ...

  2. Codeforces 1166B - All the Vowels Please

    题目链接:http://codeforces.com/problemset/problem/1166/B 个元音. 思路:先判断能否弄出至少5*5的行列,然后按顺序填字符串就好了. AC代码: #in ...

  3. [kuangbin带你飞]专题一 简单搜索 - N - Find a way

    正确代码: #include<iostream> #include<queue> #define N 210 #define inf 0xffffff using namesp ...

  4. 22个Photoshop网页设计教程网站推荐

    这些网站都会经常更新一些优秀且高质量的Web界面设计教程.如果你热爱网页设计并且经常搜集各种界面设计教程,那么你一定要把下面这些网站收藏起来. 您还可以参考以下网页设计相关教程及资源:<Web ...

  5. CSS 命名规范将省下调试时间

    我听说很多开发者厌恶 CSS.而在我的经验中,这往往是由于他们并没有花时间来学习 CSS. CSS 算不上是最优美的『语言』,但迄今二十多年来,它都是美化 web 举足轻重的工具.从这点来说,也还算不 ...

  6. Balking Pattern不需要就不用做

    word自动保存功能,如果文档被修改了,后台线程每隔一段时间会自动执行保存功能,但是如果用户在自动保存之前用Ctrl+S手动保存呢?自动保存还会执行吗?答案是不会,因为这个操作时不需要重复做的. pu ...

  7. 利用msbuild白名单执行shellcode

    x64:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe x32:C:\Windows\Microsoft.NET\Framewo ...

  8. Error: Cannot find module '@babel/core'

    报错如下 产生原因 babel-loader和babel-core版本不对应所产生的, babel-loader 8.x对应babel-core 7.x babel-loader 7.x对应babel ...

  9. 微信公众号支付出现:“当前页面的URL未注册”

    微信公众号H5调起支付时,点击支付按钮出现“当前页面的URL未注册”的提示.解决办法:由于2017年8月1日微信官方把关于支付的信息转移到了商户平台:公众平台微信支付公众号支付授权目录.扫码支付回调U ...

  10. 十一. for of

    const fruits = ['Apple','Banana','Orange','Mango']; es5: 可读性差 for(let i=0; i < fruits.length; i + ...