Adding a Syntax Highlighter to your Blogger blog

If you have problems with Syntax Highlighter, post them here.



Syntax HighlighterSyntax or code highlighting is important particularly when you need to show code examples on your blog.

It is always a big task to read codes on a blog or a website without syntax highlighting, though they are enclosed with code tags. By integrating the syntax highlighter on a blog or a website, readers can read the code examples easier.

There are so many free syntax highlighting scripts available on many websites. Most of the scripts are written in JavaScript, though some of them are powered by other programming languages such as Phyton or Ruby.

I believe "SyntaxHighlighter by Alex Gorbatchev" is the most common syntax highlighting script used by most of us. It supports a lot of different languages and you can easily create new "brush" for your language if it is not supported by default.

Adding Syntax Highlighter to your blog


First you need to backup your blogger template. In your blogger dashboard, click on 'Design' > 'Edit HTML' and then click on 'download full template' and save your template.

1. Go to Blogger Dashboard > Design > Edit HTML.
2. Press CTRL+F to find the code </head>
3. Copy the below code

<!--SYNTAX HIGHLIGHTER BEGINS-->
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPerl.js' type='text/javascript'></script>
<script language='javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf';
SyntaxHighlighter.all();
</script>
<!--SYNTAX HIGHLIGHTER ENDS-->


4. Paste it on top of </head> this code.
5. Preview your template and if everything works fine, then save it.

Make the script work


You have just added the script to your blog. If you want to highlight code in your post, then you need to use a tag in order to make your Syntax Highlighter work properly. There are two tags and you can use any one on your blog.

1. <pre> tag
2. <script> tag

<pre> tag


When writing a new post, you need to use <pre> tag in the post. The code of your post needs to go in between <pre class="brush:html"> and </pre> tags, in order to highlight your code properly. See the screenshot below.

Syntax Highlighter pre tag

If you are posting HTML code, then you have to use 'html' brush in the <pre> tag.

<pre class="brush:html">
Your 'HTML' code goes here
</pre>


Similarly, for CSS code use 'css' brush in the <pre> tag. Here is a list of brushes you can use for your blog post.

<pre class="brush:css">
Your 'CSS' code goes here
</pre>


Note for Bloggers


There is a small issue with <pre> tag. When you post HTML code in blogger post between
<pre class="brush:html"> and </pre> tags directly, you might get an error like this.

Your HTML cannot be accepted
Stop showing HTML errors for the body of this post

To get rid of this error, You have to convert raw HTML to escape characters.
  • Go to Quick Escape.
  • Paste your HTML code into the text box and click on "Convert to escaped characters →" button.
  • After conversion, you can use the converted HTML code in between
    <pre class="brush: html"> and </pre> tags.
  • Now, HTML code will render correctly on your blog.


<script> tag


This is almost same as <pre> tag, but the code of your post needs to go in between <script type="syntaxhighlighter" class="brush:html"><![CDATA[ and ]]></script> tags.

<script type="syntaxhighlighter" class="brush:html"><![CDATA[
Your 'HTML' code goes here
]]></script>


If you are posting PHP code, then you have to use 'php' brush in the <script> tag.

<script type="syntaxhighlighter" class="brush:php"><![CDATA[
Your 'PHP' code goes here
]]></script>


If you use <script> tag, then you don't have to convert HTML to escape characters. But I suggest you to use <pre> tag.

Themes for your Syntax Highlighter


Syntax Highlighter 2.0 introduced custom CSS themes. This means that by switching out just one CSS file you can completely change the look and feel of the highlighted syntax. A small number of popular color themes are included with Syntax Highlighter and you can easily make your own. Just click the css theme below to see how it looks...



<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>




<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDjango.css' rel='stylesheet' type='text/css'/>




<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeEclipse.css' rel='stylesheet' type='text/css'/>




<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeEmacs.css' rel='stylesheet' type='text/css'/>




<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeFadeToGrey.css' rel='stylesheet' type='text/css'/>




<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeMidnight.css' rel='stylesheet' type='text/css'/>




<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeRDark.css' rel='stylesheet' type='text/css'/>


After selecting the theme of your choice, just copy the code below the theme and replace the default theme link in the 'Syntax Highlighter' code.

<!--SYNTAX HIGHLIGHTER BEGINS-->
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPerl.js' type='text/javascript'></script>
<script language='javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf';
SyntaxHighlighter.all();
</script>
<!--SYNTAX HIGHLIGHTER ENDS-->


Customizing your code


You don't need to paste the exact code on your blog. You can choose brushes which you use on your blog. If you are posting HTML & CSS code on your blog and want to highlight HTML or CSS syntax, then use HTML & CSS brushes as shown below and remove the rest of the code.

<!--SYNTAX HIGHLIGHTER BEGINS-->
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'></script>

<script language='javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf';
SyntaxHighlighter.all();
</script>
<!--SYNTAX HIGHLIGHTER ENDS-->


You can add or remove brushes according to your needs.

Brushes


Here is a list of brushes you can use on your blog. If you don't find a brush you are looking for, here is a long list of brushes you can download.

Now you have got beautiful Syntax Highlighter on your blog.

{ 62 comments }

Seba, Tuesday, July 06, 2010

Thanks so much!!! You're the first one who explain how to add this library to blogger correctly! =)

kaizer1v, Wednesday, July 07, 2010

Amazing! Crystal Clear Tutorial. Great!

Damir Vadas, Saturday, July 10, 2010

Where is zebra ???
THX

rasiqzone, Saturday, July 17, 2010

nice job..

BSDSoftware, Tuesday, August 10, 2010

Grazie mille!!

sarwasunda, Saturday, August 14, 2010

Thanks, great info!

Anonymous, Thursday, August 19, 2010

Thanks, very easy information!!

Anonymous, Wednesday, August 25, 2010

In my case I have an ugly scroll-bar to the right ... how do i get rid of it?

And the second Q: how do I get those nice "hovering actions" edit, copy2clipboard etc to the < pre > code?

Thanks!

Cyber Criminal, Wednesday, August 25, 2010

@SCC

Can you post your blog url, so I can figure out the problem =]

Computer Projects, Monday, September 13, 2010

Nice trick,

Thanks .

Robert, Sunday, October 31, 2010

ok Good job!!!

Warlock, Wednesday, December 22, 2010

how to add 'copy to clipboard' link, 'print' etc?

Anonymous, Friday, December 24, 2010

@Avink: It was available in previous versions. If you want to use it. Just replace the word 'current' in the links to any previous version of the highlighter that is given in the link below.

Source: http://alexgorbatchev.com/SyntaxHighlighter/hosting.html

Eg. http://alexgorbatchev.com/pub/sh/current/styles/shCore.css to
http://alexgorbatchev.com/pub/sh/2.1.382/styles/shCore.css

akira, Saturday, December 25, 2010

--------------------------------------------------------------
how to enable scroll bar in long line code?
Mine just went trough the border and my page look terrible.

______________
example:
I have 20 character wide blog,
then I input 24 character code,
then I just see my page end with 20th character of my code,

and if I blend it with normal text, then my normal text just do the same as the code.

width:20 char
code:24 char
____________
max width :
12345678901234567890
====================

the code :
12345678901234567890
abcdefghijklmnopqrstuvwx

and the normal text is:
12345678901234567890
This is the normal text for blog.

then it should be look like:
____________________
12345678901234567890
====================
abcdefghijklmnopqrstuvwx <-- like this with a scroll bar.
====================
This is the normal
text for blog
____________________

but actually my page was look like:
____________________
12345678901234567890
====================
abcdefghijklmnopqrst <-- the code missing and not end well.
====================
This is the normal t <-- also the normal text be the same.
____________________

I also has a screen shoot on this error
screenshoot


can u help me with that?
thanks for the response
--------------------------------------------------------------

akira, Saturday, December 25, 2010

I'm sorry for the sized of the screenshoot before, I just realized that the picture was too small, here the original size ,
would you help me?

Anonymous, Saturday, December 25, 2010

@akira: Can you post your blog link, so it would be easy for me to figure out the problem.

akira, Saturday, December 25, 2010

well, I just made post to show you me problem,
this is my blog link

need your help.

thanks before.

Anonymous, Saturday, December 25, 2010

@akira

Find this line in your template.

#outer-wrapper{width:960px;margin:0px auto 0px auto;padding:0;text-align:$startSide;font:$bodyfont}

and replace with this

#outer-wrapper{width:960px;margin:0px auto;padding:0;text-align:$startSide;font:$bodyfont}

Let me know if it is working or not.

akira, Saturday, December 25, 2010

It solved :)

Thanks a lot, you are awesome..

:)

Shailendrasinh, Wednesday, January 05, 2011

how can I add zebra lines like the format in following code

http://forums.asp.net/t/1599156.aspx

please help.

Cyber Criminal, Wednesday, January 05, 2011

Sorry, what exactly do you mean by zebra lines? Can you attach a screenshot?

Shailendrasinh, Thursday, January 06, 2011

Hi, You can see the syntaxhighligher code in following url

http://forums.asp.net/t/1599156.aspx

And also see my url

http://bnb-development.blogspot.com

You will find the difference in look. Can you please sort it out?

Cyber Criminal, Thursday, January 06, 2011

Shailendrasinh: You're using current version of Syntax Highlighter. Change the version of your Syntax Highlighter to get the look you need.

Just replace the word 'current' in the links to any previous version of the highlighter that is given in the link below.

Source: http://alexgorbatchev.com/SyntaxHighlighter/hosting.html

Eg. http://alexgorbatchev.com/pub/sh/current/styles/shCore.css to
http://alexgorbatchev.com/pub/sh/2.1.382/styles/shCore.css

replace all the links containing the word 'current' to '2.1.382'. Let me know if it works or not.

Shailendrasinh, Friday, January 07, 2011

Yes, it worked.. Many thanks.!! But the fonts are little larger than expected. How can I change them to make them little smaller?

- Shailendra

Cyber Criminal, Saturday, January 08, 2011

Shailendra: Navigate to Blogger Dashboard > Design > Edit HTML and find this code .post-body in the CSS section.

It will look like this

.post-body {
line-height: 1.4;
font-size: 110%;
position: relative;
}


Set the font-size to 100% or even lesser.

Cyber Criminal, Saturday, January 08, 2011

It looks good at 95% =]

Shailendrasinh, Saturday, January 08, 2011

Thank you so much...!!

Sohail Viruss Khan, Wednesday, February 16, 2011

Awesome explanation :-)

Alejandro Villamarin, Wednesday, March 16, 2011

Hey Ciber,

I followed your instructions to the letter and I don't get any results, this is what I added to my template html (I had to escape it, otherwise I could not publish it here):


<link href='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shCore.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shBrushCpp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shBrushCss.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shBrushJava.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shBrushJScript.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shBrushPhp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shBrushPython.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/shBrushXml.js' type='text/javascript'/>
<script language='javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = &#39;http://alexgorbatchev.com.s3.amazonaws.com/pub/sh/3.0.83/scripts/clipboard.swf&#39;;
SyntaxHighlighter.all();
</script>

I changed uri so they point to latest versions, but still no luck. In one of my posts I include this:

<pre class="brush:html">
<html><body><p>why the hell this is not working!!! grrrrr</p></body></html>
</pre>

And only get a sad gray font with the html tags and pre tags rendered...

Why I'm doing wrong? I checked both in Firefox, Opera and Chrome and get the same results...

Best regards,
Alex

Anonymous, Saturday, April 16, 2011

どうもありがとう。

Thanks.

Rafael Pimenta, Saturday, April 23, 2011

Not working for Delphi!

RELOAD.ID, Monday, June 20, 2011

thanks alot...
i'm just post html, css, n php
work for my blogger.

Sangram Nandkhile, Tuesday, June 28, 2011

Its not working for me at all..its prompts message saying
"Can't find brush for: CSharp "
"Can't find brush for: XML "

i used the following fuction in my blog post.
---------------------------------------------

pre class="brush:CSharp">
using (StreamReader sr = new StreamReader(path, true))
{
XDocument xdoc = XDocument.Load(sr);
}
/pre>
----------------------------------------------

check it out at

Code Hippo

Anonymous, Sunday, July 03, 2011

@The Internet Mosquito: I had a look at your blog post and it seems to be working fine.

http://internetmosquito.blogspot.com/2011/03/memory-allocation-and-argument-passing.html

Let me know if you're still having problems, I could probably help you.

Anonymous, Sunday, July 03, 2011

@Rafael Pimenta: To make it work, you'll need to add Delphi brush to your blog.

<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushDelphi.js' type='text/javascript'></script>

Anonymous, Sunday, July 03, 2011

@Sangram Nandkhile: I just checked your blog source and didn't find syntax highlighter added on your blog.

Add the below code before </head> tag and let me know if it works.

<!--SYNTAX HIGHLIGHTER BEGINS-->
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'></script>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'></script>
<script language='javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/current/scripts/clipboard.swf';
SyntaxHighlighter.all();
</script>
<!--SYNTAX HIGHLIGHTER ENDS-->

Anonymous, Sunday, July 03, 2011

Helpful!

Giorgio Vespucci, Wednesday, July 06, 2011

Many many thanks! It work on my Blogge blog :)
I did not even have the need to escape the HTML characters :)

Unknown, Saturday, August 06, 2011

Halloo...
How to use "SyntaxHighlighter html brush"

I don't found "shBrushHtml.js"

Thank you

Anonymous, Saturday, August 06, 2011

@wiyono

You can use shBrushXml.js to highlight xml, xhtml, html codes.
http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js

Refer to this link...
SyntaxHighlighter - Bundled Brushes

Pemo, Wednesday, August 10, 2011

I always get a "cant find brush for .php" error!

And how can I get rid of the horizontal scroll bar without having to break the lines?

Thank you,
Pemo

Anonymous, Thursday, August 11, 2011

@Pemo

Make sure you've included PHP brush in your SyntaxHighlighter code.
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js' type='text/javascript'></script>

To get rid of the scroll-bar, you've to use older version of SyntaxHighlighter.

Wittaya, Thursday, August 18, 2011

Thank you very much.

Anonymous, Thursday, September 01, 2011

How can I align where says View source so my visitors can see the code?

http://gastonacuriogordo.blogspot.com/2011/08/function-replacedated-var-da-d.html

Thanks

Anonymous, Thursday, September 15, 2011

Thanks for your help.
Explain is very clear.

a_one_chollet, Sunday, October 16, 2011

thanks,,
its work on my blog..

Marius Bughiu, Sunday, October 16, 2011

Thank you for this post.
It helped a lot.

Madhav Madhu, Friday, October 21, 2011

It took me more than one hour to install the plugin for syntax highlighting.:-(
Thanks at least i found one where it's explained pretty well.
But when i am putting my 'c' code in between pre tags i am finding some unwanted texts being displayed in my post, like question mark in the beginning and at the bottom. Help!!
http://howtosolveitbycomputer.blogspot.com/2011/10/supplementary-problems.html

Unknown, Monday, December 05, 2011

Thank you for a lot!

grobanderson, Wednesday, January 04, 2012

Thanks for the post.

khan-87, Sunday, March 04, 2012

very good work thanks a lot

Major Projects

Unknown, Friday, March 16, 2012

Hi.
Thanks for great library!
I have small problem. Can't remove scrolls bars:
http://programowalnasiec.blogspot.com/2012/02/szukamy-ciekawe-zespoy-muzyczne-za.html
Any tips?

Unknown, Sunday, March 18, 2012

It's Work Fine =) thx bro.

Unknown, Monday, March 26, 2012

Cyberrack, can you help me?
all the code is fine but appear a problem
When I add "<" or "<" and the same kind. The code which I write is not in one line. Please take a look in one of my post.

http://lpuarmy.blogspot.com/2011/04/cara-redirect-page-dengan-html-php-dan.html

Sorry for my bad english, thank you :)

Unknown, Sunday, April 01, 2012

Hi, I´m begin to use highlighter code in blogger and when I insert it on top of my /head, the template of my movile view dissapear and I don´t found the way to fix it, somebody can help me?

Loefa-Cebook, Sunday, April 01, 2012

wowwwwwwwwwwwwwwwwwwwwwwwwww coollllll :D i love this post.. thanks for this share,, I looking for this from last month.

Unknown, Friday, April 27, 2012

it not working for my blog .please help me, thanks

http://pydean.blogspot.com/2012/04/test.html

Messaink, Tuesday, May 15, 2012

It work on my blog!!
thanks for great tutorial...

Neville, Thursday, May 17, 2012

i have a problem, when i type
#include

it includes an additional tag like this
#include


why is it considering as an html tag... could you please help me out with it.

Unknown, Sunday, June 03, 2012

very useful, it is what I wanted.

Anonymous, Tuesday, June 12, 2012

When I do a preview of the template, it says "More than one widget was found with id: Navbar1. Widget IDs should be unique.
Error 500"

Anonymous, Monday, July 02, 2012

Excellent Artical

We'd love to hear from you — Leave us a comment!