Posts Tagged ‘HTML’

Showing white space ‘as-is’ in HTML

March 14, 2008

A common gripe about HTML rendering is that white spaces are not preserved.

For e.g. giving

<table><tr><td>This is       a                       test</td></tr></table>

will end up being shown as ‘This is a test’.

To show the spaces, people generally convert spaces to & nbsp;

to preserve the spaces. Instead, a CSS property ‘white-space’ can be used.

For e.g. to preserve the spaces, do this:

<table><tr><td style = "white-space:PRE">This is       a                       test</td></tr></table>

This will display preserve the white spaces and show

'This is            a                        test'

on the browser. We need to include a !DOCTYPE to make use of ‘white-space:pre’ property on IE.