rob1994
rob1994

Reputation: 41

Displaying a python file in HTML keeping the spaces etc

I've written some python code that I want to show on my website, but obviously just copying and pasting the text will render it almost unreadable due to the HTML not recognising the carriage return and removing the excess spacing.

Is there an easy way to show it in the HTML as if it were in a text editor or IDE short of CSSing the crap out of it?

Upvotes: 0

Views: 2647

Answers (4)

Alex W
Alex W

Reputation: 38253

You will probably want to use the CSS white-space property, e.g:

CSS:

div#python { white-space: pre; }

HTML:

<div id="python">
    Python code
</div>

There's also different options for how the white space is treated.

The advantage to this method is the abstraction of the style information into CSS which makes the page a lot more maintainable if it needs to be edited in the future.

Upvotes: 2

Adrift
Adrift

Reputation: 59819

Use the <pre> </pre> element - it does exactly what you want

Info: http://www.w3.org/wiki/HTML/Elements/pre

Upvotes: 1

Mauritz Hansen
Mauritz Hansen

Reputation: 4774

You can wrap it in a 'code' tag.

<code>this is my computer code</code>

Upvotes: 1

brbcoding
brbcoding

Reputation: 13596

<pre> or <code>...

Good discussion on <pre> vs <code> vs <samp>

Upvotes: 5

Related Questions