设置文本输入插入符号的样式
我想设置焦点 的插入符号的样式。具体来说,就是颜色和厚度。
I want to style the caret of a focused <input type='text'/>
. Specifically, the color and thickness.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
“插入符”是您要查找的词。但我确实相信,它是浏览器设计的一部分,而不是在 CSS 的掌握范围内。
然而,这里有一篇关于使用 Javascript 和 CSS 模拟插入符号更改的有趣文章 http://www.dynamicdrive.com/forums/showthread.php?t=17450 对我来说这似乎有点老套,但可能是完成任务的唯一方法。文章的要点是:
这里是一个正在运行的演示
2018年更新
有一个新的 css 属性
caret-color
其中适用于input
或contenteditable
区域的插入符号。支持正在增长,但不是 100%,这只会影响颜色,不会影响宽度或其他类型的外观。'Caret' is the word you are looking for. I do believe though, that it is part of the browsers design, and not within the grasp of css.
However, here is an interesting write up on simulating a caret change using Javascript and CSS http://www.dynamicdrive.com/forums/showthread.php?t=17450 It seems a bit hacky to me, but probably the only way to accomplish the task. The main point of the article is:
HERE is a demo in action
2018 update
There is a new css property
caret-color
which applies to the caret of aninput
orcontenteditable
area. The support is growing but not 100%, and this only affects color, not width or other types of appearance.如果您使用的是 webkit 浏览器,您可以按照下一个 CSS 片段更改插入符号的颜色。我不确定是否可以使用 CSS 更改格式。
这是一个示例: http://jsfiddle.net/8k1k0awb/
If you are using a webkit browser you can change the color of the caret by following the next CSS snippet. I'm not sure if It's possible to change the format with CSS.
Here is an example: http://jsfiddle.net/8k1k0awb/
在 CSS3 中,现在有一种原生方法可以做到这一点,无需现有答案中建议的任何技巧:
caret-color
属性。您可以使用插入符执行很多操作,如下所示。它甚至可以动画。
Firefox 55 和 Chrome 60 支持
caret-color
属性。Safari 技术预览版和 Opera 中也提供支持(但 Edge 中尚未提供)。您可以在此处查看当前支持表。In CSS3, there is now a native way to do this, without any of the hacks suggested in the existing answers: the
caret-color
property.There are a lot of things you can do to with the caret, as seen below. It can even be animated.
The
caret-color
property is supported from Firefox 55, and Chrome 60. Support is also available in the Safari Technical Preview and in Opera (but not yet in Edge). You can view the current support tables here.以下是您可能要寻找的一些供应商
您还可以设计不同的状态,例如焦点
您还可以对其进行某些转换,例如
Here are some vendors you might me looking for
You can also style different states, such as focus
You can also do certain transitions on it, like
以这种方式将 color 属性与 -webkit-text-fill-color 一起使用就足够了:
适用于 WebKit 浏览器(但不适用于 iOS Safari,其中仍使用系统颜色作为插入符)以及 Firefox。
所以这意味着我们使用文本填充颜色设置文本颜色,使用标准颜色属性设置插入符号颜色。在不受支持的浏览器中,插入符号和文本将具有相同的颜色 - 插入符号的颜色。
It is enough to use color property alongside with -webkit-text-fill-color this way:
Works in WebKit browsers (but not in iOS Safari, where is still used system color for caret) and also in Firefox.
So this means we set text color with text-fill-color and caret color with standard color property. In unsupported browser, caret and text will have same color – color of the caret.
您可以通过一些 hack 调整来“设计”插入符号输入...在此示例中,插入符号被“替换”为具有平滑移动动画的 div,就像在 MSWindows 的 Office 中一样
You can "style" the caret input with some hack tweaks... in this example the caret is "replaced" with a div animated with smooth movement, like in Office for MSWindows
当我尝试构建一个类似终端的网站时,我发现目前还没有办法使用 CSS 来更改光标的样式。
然后,经过近 3 天的研究,我通过检查网站得到了一个想法(抱歉,我无法提供链接),
它在
command string
中添加和删除 HTML span 元素并创建一个块光标,但如果您使用一点 CSS 知道插入符号的位置,则可以制作任何插入符号。这帮助我创建了一个插入符号,就像您在在我的网站中看到的那样,
您可以在以下位置查看源代码此仓库
摘要:
假设您的文本是
echo Hello
,在H
后带有插入符。那么代码可能类似于
希望它有帮助;-)
While I was trying to build a terminal like website i got to know that there is no way (yet) to change the style of the cursor using CSS.
Then, after almost 3 days of research I got an Idea from inspecting a website (sorry, I can't provide the link)
It adds and removes a HTML span element in the
command string
and makes a block cursor but you can make any caret if you know the caret's position with a little bit of CSS.This helped me create a caret like you see in my site
You can see the source in this repo
Summary:
suppose your text is
echo Hello
with caret afterH
.Then the code might be something like
Hope it helps ;-)
这很简单。
只需添加插入符号颜色属性,如下所示:
it is simple.
Just add the caret-color attribute like this: