How do I have different font colors in a textarea?

10,956

Solution 1

Textarea is a standard HTML element and so was invented just after the dawn of time. Unfortunately this means it is limited in it's appearance and functionality.

Changing the colours of specific words is not possible as far as I know. However a way to get around this would be to have an iFrame embedded in the page. That way, you can treat the iFrame content as another web page and style it using CSS.

The Yahoo RTE, the FCKEditor and the Lightweight RTE works in this way.

Another option, which does not use an iFrame is the editor used here on Stack Overflow, known as the WMD. The files are here.

Solution 2

It's not possible.

Way to go is to make textarea's font, but not cursor, transparent using color:#000;-webkit-text-fill-color:transparent, then create underlying, 100% overlapping div to which content of textarea will be copied + formatted on textarea's oninput event.

You'll need to adress (or avoid) some issues coming out of syncing these two elements, like scrolling for example, but it can be done. I made my own HTML editor this way.

AFAIK, css property -webkit-text-fil-color is supported in Opera, Chrome and should be in soon-to-be-released Firefox v.48.

Share:
10,956
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    I want the font color to change in a textarea as I type in specific keywords, like in Visual Studio.

    I have not seen this anywhere, so I don't know if this is possible with HTML and JavaScript.

    Has anyone seen anything like this? Or know how to write it?