Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<pre> tag and css font-family

Tags:

I have html with code below:

<pre>
                                      |       Date Offense |       Count |
   Title, Section & Nature of Offense |          Concluded |   Number(s) |
--------------------------------------------------------------------------
              18:2113(a)-BANK ROBBERY |   January 27, 2009 |           I |
--------------------------------------------------------------------------

</pre>

It works perfect in a simple page, but when I have added css with font-family defination, it performs ugly, the format is not tidy any more.

Any suggestions? Thanks!

like image 220
Dong Avatar asked Oct 22 '10 08:10

Dong


People also ask

How do I change the font style of a pre tag?

While the font-size cannot be changed for text directly inside pre tags, you can always wrap that text in another element (a span, for example) and change the font size of that element.

What is font-family tag in CSS?

The font-family property specifies the font for an element. The font-family property can hold several font names as a "fallback" system. If the browser does not support the first font, it tries the next font.

What is pre tag in CSS?

Definition and Usage. The <pre> tag defines preformatted text. Text in a <pre> element is displayed in a fixed-width font, and the text preserves both spaces and line breaks. The text will be displayed exactly as written in the HTML source code.

How do I use pre tag in CSS?

The “pre” of a <pre> tag literally means “preformatted text” – which doesn't say anything about what that text is. A <code> tag, semantically, says the text within is code. It makes sense to me! I always use it when placing blocks of code, which in my experience is the #1 use case.


2 Answers

You must use a monospace font (also known as typewriter font). That is a font in which all the characters take the same space.

In CSS the font name monospace is translated to the default monospace font on the user system. It's good practice to include monospace at the end of a font-familly list for the <pre> element so that it can be used as a fallback.

For instance:

pre {font-family: Consolas,monospace}

will use the Consolas monospace font if it's available or fallback to the default.

like image 150
Alex Jasmin Avatar answered Sep 17 '22 15:09

Alex Jasmin


Surprising! When I have posted this question, the display of Stackoverflow is just what I want.

So the solution is:

pre
{
  font-family: Consolas, Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace, serif;
  margin-bottom: 10px;
  overflow: auto;
  width: auto;
  padding: 5px;
  background-color: #eee;
  width: 650px!ie7;
  padding-bottom: 20px!ie7;
  max-height: 600px;
}
like image 38
Dong Avatar answered Sep 20 '22 15:09

Dong