How to change a span to look like a pre with CSS?

Viewed 62014

Is it possible to to change a <span> tag (or <div>) to preformat its contents like a <pre> tag would using only CSS?

8 Answers

This makes a SPAN look like a PRE:

span {
  white-space: pre;
  font-family: monospace;
  display: block;
}

Remember to change the css selector as appropriate.

while the accepted answer is indeed correct, if you want the text to wrap you should use:

white-space: pre-wrap;

Why not just use the <pre> tag, instead of the span tag? Both are inline, so both should behave in the way you would like. If you have a problem overriding the entire definition of <pre>, just give it a class/id.

Related