使用 CSS 强制 HTML textarea 使用等宽字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2032652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Forcing HTML textarea to use a monospace font using CSS
提问by Lawrence Dol
How can I make my text area use a monospaced font?
如何让我的文本区域使用等宽字体?
回答by meder omuraliev
If I'm understanding properly, it should already inherit default user-agent styles, but if you want to explicitly, just specify a font-family ( styles Hymaned from Stackoverflow stylesheet )
如果我理解正确,它应该已经继承了默认的用户代理样式,但是如果您想明确指定,只需指定一个字体系列(从 Stackoverflow 样式表中提取的样式)
textarea {
font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;
}
Specify the more specific (monospace-specific) font families first, and if the user agent doesn't have that available it will keep trying them until the end, in which case it would default to the default user agent font family for that element, if any was specified.
首先指定更具体的(特定于等宽的)字体系列,如果用户代理没有可用的字体系列,它将继续尝试直到最后,在这种情况下,它将默认为该元素的默认用户代理字体系列,如果有指定。
回答by Jeff Atwood
You can also use the standard generic font-family: monospace
, but be careful -- there are some severeside effects (sadly) in Chrome, Safari and anything WebKit based.
您也可以使用标准的 generic font-family: monospace
,但要小心——Chrome、Safari 和任何基于 WebKit 的东西都有一些严重的副作用(遗憾的是)。
see:
看:
回答by slebetman
Just use a monospace font. For example for something like:
只需使用等宽字体。例如对于这样的事情:
<textarea id="mytextarea"></textarea>
the css would be:
css 将是:
#mytextarea {
font-family: monospace;
}
If you want to use a specific monospace font, you can but don't forget adding the generic 'monospace' at the end in case the user does not have your preferred font installed:
如果您想使用特定的等宽字体,您可以但不要忘记在末尾添加通用的“等宽”,以防用户没有安装您喜欢的字体:
#mytextarea {
font-family: 'DejaVu Sans Mono', monospace;
}
回答by Gazzer
textarea
{
font-family: monospace;
}
You may need to add an important if not working
如果不工作,您可能需要添加一个重要的
textarea
{
font-family: monospace !important;
}