Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
I would like to highlight some parts of a single code block by changing either the color or the background color, preferably background color.

I've tried using span, for example
<span style="background-color:lightgreen">Some code</span>

in order to produce
Some code


However, it seems that this doesn't work inside code blocks. For example

try {
<span style="background-color:lightgreen">do something</span>
}


produces the following (still the tag is shown and no actual effect)
C#
try {
   <span style="background-color:lightgreen">do something</span>
}


Any ideas how I could use coloring?

I already tried using bold & italic but they don't actually draw any attraction inside a code block.
Posted

Hope yellow can fit.
See http://www.codeproject.com/Info/FAQ.aspx[^] > chapter Highlighting code.
C#
<pre lang="cs">// This is an important variable name
int <span class="highlight">myVar</span> = 0;
</pre>


Will look like
C#
// This is an important variable name
int <span class="highlight" >myVar</span> = 0;

But it may be bugged since the text after </span> do not appear or it is not supported in QA.
 
Share this answer
 
v3
Comments
Wendelius 4-Oct-15 4:34am    
Yellow is just fine. Thanks, this worked like a charm!
Mika, I think this is not supported. Have you seen this anywhere in CodeProject?

Also, I would suggest you to post this in Article Help Forum[^].
 
Share this answer
 
Comments
Wendelius 4-Oct-15 4:37am    
Thank you for the comments. It seems that the colour definition isn't supported as you said. However a predefined class "hightlight" exists so that helped me to emphasize the parts I wanted the reader to notice. Thanks for the effort.
Thanks for the info Mika. I will implement that in my upcoming articles. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900