Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display formatted code in webpage

Tags:

html

c#

css

I am trying to write a blog and I want to display c# code in a neat formatted way. Is there a way to it. I like to do it exactly the way stack overflow does including color. :)

like image 869
VolleyBall Player Avatar asked Mar 02 '11 15:03

VolleyBall Player


People also ask

How would you display code on a webpage?

To view only the source code, press Ctrl + U on your computer's keyboard. Right-click a blank part of the web page and select Page source from the pop-up menu that appears.

How do you display sample code in HTML?

You can include code examples in your HTML by using the <code> tag. This automatically uses a monospaced font and also semantically labels our code as what it is.

How do I display code snippets in HTML?

HTML provides many methods for text-formatting but <code> tag is displayed with fixed letter size, font, and spacing. Some points about <code> tag: It is mainly used to display the code snippet into the web browser.

How do I display HTML code as output?

HTML <output> Tag.


1 Answers

You have quite a few options here, but I would recommend against using a JavaScript formatter. If a user has scripts disabled, you still want the code to look good.

If you are using Visual Studio, the Productivity Power Tools add-in has a "copy HTML" option:
http://blogs.msdn.com/b/kirillosenkov/archive/2010/06/07/copy-code-in-html-format-with-visual-studio-2010.aspx

Jon Skeet provides a code formatter for public use:
http://csharpindepth.com/CodeFormatterTool.aspx

If you are using PHP, GeSHi is a good server-side option:
http://qbnz.com/highlighter/

I believe that stack-overflow uses google-code-prettify:
http://code.google.com/p/google-code-prettify/

SyntaxHighlighter is another good client-side solution:
http://alexgorbatchev.com/SyntaxHighlighter/

like image 97
John Gietzen Avatar answered Oct 17 '22 02:10

John Gietzen