更改文本框 HTML/CSS 中一个字符的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17341670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:32:19  来源:igfitidea点击:

Change color of one character in a text box HTML/CSS

htmlcsstextbox

提问by bluewonder

I'm designing a web site and i would like to ask you guys that, how can I change the color of just one character in a string in a text box of HTML by CSS?

我正在设计一个网站,我想问你们,如何通过 CSS 更改 HTML 文本框中字符串中一个字符的颜色?

example : STACK OVER FLOW just the 'A' letter is red!

例如:堆栈溢出,只是“A”字母是红色的!

Thanks guys

谢谢你们

回答by xec

You can't do this with a regular <input type="text">or <textarea>element, but with a normal element (like <div>or <p>) made contenteditable, you have all the freedoms of html/css formatting.

您不能使用常规元素<input type="text"><textarea>元素执行此操作,但是使用普通元素(如<div><p>) made contenteditable,您可以拥有 html/css 格式的所有自由。

<div contenteditable>
    ST<span style="color: red">A</span>CK OVERFLOW
</div>

http://jsfiddle.net/jVqDJ/

http://jsfiddle.net/jVqDJ/

The browser support is very good as well (IE5.5+). Read more at https://developer.mozilla.org/en-US/docs/Web/HTML/Content_Editable

浏览器支持也很好(IE5.5+)。在https://developer.mozilla.org/en-US/docs/Web/HTML/Content_Editable阅读更多

回答by Sumit

I just want to add to the solution if you want to change color of only first characterthen there is a CSS selector element::first-letter

如果您只想更改第一个字符的颜色,我只想添加到解决方案中,然后有一个 CSS 选择器element::first-letter

example:

例子:

div::first-letter{
   color: red;
}

回答by NeoEmberArts

You could try using

你可以尝试使用

<style> 
span.green{ 
     color:green; 
    } 
span.purple{ 
       color:purple; 
    } 
</style> 

<spanclass="purple">Var</span> x = "<span class="green">dude</span>"; 

回答by Endgamers

you could use bold tags A

你可以使用粗体标签 A

css:

css:

b {
font-color: red;
}

回答by eblue

From css you can only change an elements property so you need to insert the letter "A" in another element like this:

在 css 中,您只能更改元素属性,因此您需要在另一个元素中插入字母“A”,如下所示:

ST<span>A</span>CK OVER FLOW just the 'A' letter is red!

And the CSS part is

而 CSS 部分是

span{
   color:#FF0000;
}

Or attach a class to it like this

或者像这样附加一个类

ST<span class="myRedA">A</span>CK OVER FLOW just the '<A' letter is red!

CSS:

CSS:

span.myRedA{
       color:#FF0000;
    }

回答by Pixie Lumis Serigala

<font color="#colors">text text text</font>