简体   繁体   English

<pre> tag和css font-family

[英]<pre> tag and css font-family

I have html with code below: 我有以下代码的HTML:

<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. 它在一个简单的页面中工作得很完美,但是当我添加了带有font-family defination的css时,它表现得很难看,格式不再整齐。

Any suggestions? 有什么建议? Thanks! 谢谢!

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. 在CSS中,字体名称monospace被转换为用户系统上的默认等宽字体。 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. 最好在<pre>元素的font-familly列表的末尾包含monospace ,以便它可以用作后备。

For instance: 例如:

pre {font-family: Consolas,monospace}

will use the Consolas monospace font if it's available or fallback to the default. 将使用Consolas monospace字体(如果可用)或回退到默认值。

Surprising! 奇怪! When I have posted this question, the display of Stackoverflow is just what I want. 当我发布这个问题时,Stackoverflow的显示就是我想要的。

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;
}

The default definition for the pre tag uses the monospace font (like in console). pre标签的默认定义使用等宽字体(如在控制台中)。

font-family: monospace;

It is possible your definition suggests a different type of a font which is why your nice design breaks. 你的定义可能会提出一种不同类型的字体,这就是为什么你的设计好了。 You must either specify a system-specific monospace font (eg 'Lucida Console') or the fall-back alias 'monospace'; 您必须指定系统特定的等宽字体(例如'Lucida Console')或后备别名'monospace';

The best answer seems to be How to use css to change <pre> font size : 最好的答案似乎是如何使用css来改变<pre>字体大小

From that answer: (referring to the text inside the pre ) 从这个问题的答案:(指的文本pre

...you can always wrap that text in another element (a span, for example) and change the font size of that element. ...您始终可以将该文本包装在另一个元素(例如span)中,并更改该元素的字体大小。

(Don't forget to upvote his answer there.) (不要忘记在那里提出他的答案。)

不要为pre标签添加字体系列定义删除它并且每件事都没有问题或者如果你必须使用定义那么就不要使用tab来插入空格只使用空格键来插入空格..很多工作但是那就是希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM