189

I'm only looking to format a specific string within a cell. I change that cell's format to "Markdown" but I'm not sure how to change text color of a single word.

I don't want to change the look of the whole notebook (via a CSS file).

10 Answers 10

259

You can simply use raw html tags like

foo <font color='red'>bar</font> foo

Be aware that this will not survive a conversion of the notebook to latex.

As there are some complaints about the deprecation of the proposed solution. They are totally valid and Scott has already answered the question with a more recent, i.e. CSS based approach. Nevertheless, this answer shows some general approach to use html tags within IPython to style markdown cell content beyond the available pure markdown capabilities.

4
  • 1
    @jayarjo In IPython 2.1 this still works flawlessly! Can you give more information on what you have tried?
    – Jakob
    Commented Jul 29, 2014 at 21:19
  • Works for me with Jupyter NB version 4.3.1
    – Austin
    Commented May 13, 2017 at 2:56
  • The "font" tag is now deprecated. You can use the "span" tag with the style attribute instead.
    – Paul
    Commented Sep 26, 2020 at 19:44
  • 1
    This answer doesn't works when doing File > Export Notebook as... > PDF, as the resulting PDF document shown the colored word in black. Is there any alternative?
    – Gian Arauz
    Commented Nov 11, 2021 at 10:16
86

Similarly to Jakob's answer, you can use HTML tags. Just a note that the color attribute of font (<font color=...>) is deprecated in HTML5. The following syntax would be HTML5-compliant:

This <span style="color:red">word</span> is not black.

Same caution that Jakob made probably still applies:

Be aware that this will not survive a conversion of the notebook to latex.

6
  • @jayarjo I just tried my solution in IPython 1.1.0, and it worked fine for me. Can you provide more information on what you tried and how your environment is set up? Remember to change the cell type to Markdown, which can be done through the Cell > Cell Type menu.
    – Scott H
    Commented Jul 31, 2014 at 17:39
  • I'm trying this on github.
    – jayarjo
    Commented Aug 9, 2014 at 10:32
  • @jayarjo, this question was specifically about coloring text in IPython notebooks, which allow markdown text. For the behavior of color in GitHub markdown files, see the following question specifically on that issue. The short answer is that there doesn't seem to currently be a way to specify color for text in GitHub markdown documents. stackoverflow.com/questions/11509830/…
    – Scott H
    Commented Aug 29, 2014 at 15:35
  • @ScottH: I do not understand what you mean by 'conversion of the notebook to latex'. I am able to convert part of the text in span to latex and jupyter renders the latex in red. Commented Aug 11, 2015 at 15:59
  • @ShishirPandey: What I was referring to with "conversion of the notebook to latex" is that you can normally convert a notebook to LaTex (a typesetting language) using ipython nbconvert --to latex mynotebook.ipynb. Alternatively, in an open notebook you can go to File > Download as... > PDF via LaTex. But I believe that color specification via html/css style may not be translated correctly into the LaTex. I don't have LaTex installed at the moment, so I can't re-verify, but I think that's the case.
    – Scott H
    Commented Aug 11, 2015 at 17:40
37

An alternative way to do that, is to enter a LaTeX environment within the notebook and change color from there (which is great if you are more fluent in LaTeX than in HTML). Example:

$\color{red}{\text{ciao}}$

would display ciao in red.

2
  • 1
    It's worth noting that if you plan to export a notebook to Latex or PDF or process it with pandoc, etc, that this solution will pass through the latex successfully where the raw HTML solutions are more likely to either be stripped by pandoc when converting to latex or result in latex errors. Commented Mar 19, 2021 at 18:25
  • 1
    Just to mention a potential downside, the LaTeX might be rendered in a different font than the rest of the markdown (this has been my experience in Jupyter notebooks at least using the above verbatim). Also, this solution does not work well for long sections of text because the text will not be automatically wrapped since this is within a text section within math mode.
    – j_v_wow_d
    Commented Jan 28, 2022 at 3:32
20

For example, if you want to make the color of "text" green, just type:

<font color='green'>text</font>
5

I have tested the ideas that I found in these answers in google colabs.

<span style='color:red'>green</span> 
$\color{green}{\text{blue}}$ 
<font color='red'>orange</font>

Of these the 2nd (LaTeX $…$, and html font) versions work. The style version does not work.

Note: the html font element is deprecated. Therefore I am using the LaTeX version.

1
  • the LaTeX version worked great! Thanks for adding it.
    – haltersweb
    Commented Aug 26, 2023 at 2:46
4

If none of the above suggestions works for you, try using the style attribute.

**Notes**
<p style="color:red;">ERROR: Setting focus didn't work for me when I tried from jupyter. However it worked well when I ran it from the terminal</p>

This gives me the following result

enter image description here

3

The text color can be changed using,

<span style='color:green'> message/text </span>
2
<p style="font-family: Arial; font-size:1.4em;color:gold;"> Golden </p>

or

Text <span style="font-family: Arial; font-size:1.4em;color:gold;"> Golden </p> Text
0
2
<span style='color:blue '> your message/text </span>

So here it is a perfect html css style entry inside a notebook ipynb file.

Of course you can choose your favourite color here and then your text.

2

This is a very simple and effective trick for google colab. Use the (empty) link syntax of the markdown.

[your_message]()

Then you'll get the blue text (underline).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.