我的网站将有一些内联代码(“当使用 foo()
函数时...”)和一些块片段。这些往往是 XML,并且有很长的行,我更喜欢浏览器将其换行(即,我不想使用
)。我还想在块片段上设置 CSS 格式。
看来我不能同时使用
,因为如果我在上面放置 CSS 块属性(使用 display: block;
),它会破坏内联片段。
我很好奇人们在做什么。使用
表示块,使用
表示内联?使用
或类似的东西?
我想让实际的 HTML 尽可能简单,避免类,因为其他用户将维护它。
My site is going to have some inline code ("when using the foo()
function...") and some block snippets. These tend to be XML, and have very long lines which I prefer the browser to wrap (i.e., I don't want to use <pre>
). I'd also like to put CSS formatting on the block snippets.
It seems that I can't use <code>
for both, because if I put CSS block attributes on it (with display: block;
), it will break the inline snippets.
I'm curious what people do. Use <code>
for blocks, and <samp>
for inline? Use <code><blockquote>
or something similar?
I'd like to keep the actual HTML as simple as possible, avoiding classes, as other users will be maintaining it.
发布评论
评论(10)
使用
表示可以换行的内联代码,使用
(更好的是,如果你想易于维护,让用户以 Markdown 方式编辑文章,那么他们就不必记住使用
HTML5 同意这在 “
pre
元素中”:Use
<code>
for inline code that can wrap and<pre><code>
for block code that must not wrap.<samp>
is for sample output, so I would avoid using it to represent sample code (which the reader is to input). This is what Stack Overflow does.(Better yet, if you want easy to maintain, let the users edit the articles as Markdown, then they don’t have to remember to use
<pre><code>
.)HTML5 agrees with this in “the
pre
element”:我完全错过了一些事情:
http://jsfiddle.net/9mCN7/
Something I completely missed: the non-wrapping behaviour of
<pre>
can be controlled with CSS. So this gives the exact result I was looking for:http://jsfiddle.net/9mCN7/
就我个人而言,我会使用
因为这是语义上最正确的。然后,为了区分内联代码和块代码,我将添加一个类:
用于内联代码或:
用于代码块。取决于哪个不太常见。
Personally I'd use
<code>
because that's the most semantically correct. Then to differentiate between inline and block code I'd add a class either:for inline code or:
for code block. Depending on which is less common.
使用(过时的)
标签:非常遗憾这个标签已被弃用,但是它仍然可以在浏览器上使用,它是一个糟糕的标签。无需逃避其中的任何东西。多么高兴啊!
使用
Show HTML code, as-is, using the (obsolete)
<xmp>
tag:It is very sad this tag has been deprecated, but it does still works on browsers, it it is a bad-ass tag. no need to escape anything inside it. What a joy!
Show HTML code, as-is, using the
<textarea>
tag:对于正常内联
使用:
对于需要阻塞
的每个地方,使用
或者,定义一个
断裂衬里块标签(没有课程)
测试:
(注意:以下是使用
data:
URI 协议/方案的 scURIple,因此%0A
nl 格式代码对于保留此类数据至关重要当剪切并粘贴到 URL 栏中进行测试时 - 所以view-source:
(ctrl-U) 看起来不错 every< /em> 下面一行带有%0A
)For normal inlined
<code>
use:and for each and every place where blocked
<code>
is needed useAlternatively, define a
<codenza>
tag for break lining block<code>
(no classes)Testing:
(NB: the following is a scURIple utilizing a
data:
URI protocol/scheme, therefore the%0A
nl format codes are essential in preserving such when cut and pasted into the URL bar for testing - soview-source:
(ctrl-U) looks good preceed every line below with%0A
)考虑 TextArea
人们通过 Google 找到此内容并寻找更好的方法来管理其摘要的显示,还应该考虑
可能出现的地方)。
例如,要显示受控换行的单行,请考虑
。
为了比较所有...
Consider TextArea
People finding this via Google and looking for a better way to manage the display of their snippets should also consider
<textarea>
which gives a lot of control over width/height, scrolling etc. Noting that @vsync mentioned the deprecated tag<xmp>
, I find<textarea readonly>
is an excellent substitute for displaying HTML without the need to escape anything inside it (except where</textarea>
might appear within).For example, to display a single line with controlled line wrapping, consider
<textarea rows=1 cols=100 readonly>
your html or etc with any characters including tabs and CrLf's</textarea>
.To compare all...
这是一个普通、非 JavaScript 的 HTML 解决方案,使用起来非常简单,并且优于使用
它允许您的用户快速剪切和粘贴代码示例。它还允许您快速、轻松地将代码放入 HTML 元素中,而无需转义通常必须执行的所有
<
和>
字符使用。
使用
...使用一些简单的 CSS 样式...
请注意,它看起来像常规等宽
< ;code>
但是块级的,支持顺便说一句......您仍然可以使用
Here is a PLAIN, non-JavaScripted, HTML solution that is very simple to use, and superior to using
<pre>
and<code>
elements, or heavy-handed JavaScript solutions which are always overkill. I have been using this trick for 20 years! It works in all browsers, old and new. Kids today have just failed to learn the old ways.It allows your users to cut-and-paste code samples quickly. It also allows you to drop you code into an HTML element quickly, hassle-free, without having to escape all the
<
and>
characters you normally have to do when using<code>
.Use the
<textarea>
element to share code, like so:...with some simple CSS styling...
Notice that it looks like regular monospace
<code>
but is block-level, honors text formats like<pre>
, long text now wraps, the code box size is controllable, and allows more flexible displays of large HTML or script blocks users can more easily access.BTW....you can still use
<pre><code>
. I still do for smaller examples. And don't worry about semantic or accessibility issues using<textarea>
as it is a replaced element and has a more versatile use. If you are worried, then simply add an ARIA label to your<textarea>
, like I have done above.考虑 Prism.js: https://prismjs.com/#examples
它使得
Consider Prism.js: https://prismjs.com/#examples
It makes
<pre><code>
work and is attractive.这适用于我在前端显示代码:
查看实时演示: https://jsfiddle.net/bytxj50e/
This works for me to display code in frontend:
View Live Demo: https://jsfiddle.net/bytxj50e/
一个简单的解决方案是将
display: block;
声明放在div > 中。代码{}
规则。这样,block
显示样式将仅适用于作为的直接后代的
标记,以及任何出现在
、
或其他文本范围标记内的实例将保留默认的
inline
显示样式。如果网站使用更多语义标记(应该),则可以扩展此规则以包括、
和
说到语义,如果我们要把 W3C 的话当作福音,他们确实在 HTML5 规范
(我的重点)
按照你的意愿去做(也许他们的意思是“一个计算机程序名称”),但我不认为将
重新设计为块级元素可能被认为是一种可怕的罪恶。
One simple solution to that would be to put the
display: block;
declaration inside adiv > code {}
rule. That way theblock
display style would only apply to<code>
tags that are the immediate descendants of a<div>
, and any instances appearing inside a<p>
,<h2>
or other text span tag would retain the defaultinline
display style. If the site uses more semantic mark-up (it should) this rule could be extended to include<article>
,<section>
and<aside>
:And speaking of semantics, if we are to take the W3C's word as gospel, they do say in the HTML5 specification that
(my emphasis)
Make of that what you will (perhaps they meant to say "a computer program name"), but I don't think restyling
<code>
as a block-level element could be considered a terrible sin.