简体   繁体   English

如何在<code>tag?</code>使用缩进<code>tag?</code>

[英]How do I use indentation in a <code> tag?

I have a code tag with some computer code: 我有一些计算机代码的代码标签:

<code>
    int main(){  <br/ >
        printf("Hello World!");<br/ >
    }
</code>

But when I display it in the browser, there will be no indentation. 但是,当我在浏览器中显示它时,将没有缩进。 How can I do this, besides using &nbsp; 除了使用&nbsp;之外,我该如何做&nbsp; ? Writing them would be a very tedious job. 编写它们将是非常繁琐的工作。

<code>
    <pre>
        int main(){  
            printf("Hello World!");
        }
    </pre>
</code>

You can use the "pre" tag, which defines preformatted text. 您可以使用“ pre”标签,该标签定义预格式化的文本。 Text in a "pre" element is displayed in a fixed-width font (usually Courier), and it preserves both spaces and line breaks. “前置”元素中的文本以固定宽度的字体(通常为Courier)显示,并且保留空格和换行符。 It is used when displaying text with unusual formatting, or some sort of computer code. 在显示格式异常或某种计算机代码的文本时使用。 And you will not need additional "br" tags. 而且您将不需要其他的“ br”标签。

Your code should look like the above one. 您的代码应类似于上面的代码。

You can find more about the tag here . 您可以在此处找到有关该标签的更多信息。

The code element does not change normal HTML formatting rules, which collapse consecutive whitespace to a single space. code元素不会更改普通的HTML格式设置规则,该规则会将连续的空格折叠为一个空格。 It does not do much else either, beyond setting the font to monospace by default, though in special cases it may have some effect (eg, automatic translation software might treat code elements so that their content is left untranslated). 除了在默认情况下将字体设置为等宽字体之外,它也没有做其他任何事情,尽管在特殊情况下它可能会产生一些效果(例如,自动翻译软件可能会处理code元素,以使它们的内容保持未翻译状态)。

You could use CSS to request different formatting: 您可以使用CSS请求不同的格式:

<style>
code { white-space: pre }
</style>

But this is unnecessarily fragile (CSS rules might be ignored for various reasons), when you can simply use the pre element (and remove the br tags): 但这是不必要的脆弱(由于各种原因,CSS规则可能会被忽略),当您可以简单地使用pre元素 (并删除br标签)时:

<pre>
    int main(){  
        printf("Hello World!");
    }
</pre>

If you wish to additionally use the code element for some reason, then the valid way is to nest code inside pre , not vice versa. 如果您出于某种原因希望额外使用code元素,那么有效的方法是将code嵌套在pre ,反之亦然。 Here you then need to notice that special rules about ignoring a line break after a <pre> tag do not apply here (since there would be an intervening tag), so to avoid getting an extra line break at the beginning, you would need to write like this: 然后,您需要注意,关于忽略<pre>标记后的换行符的特殊规则不适用于此处(因为会有一个中间的标记),因此为了避免在开始时出现额外的换行符,您需要这样写:

<pre><code>   int main(){  
        printf("Hello World!");
    }</code></pre>

PS Neither code nor pre changes the rules about the markup-significant characters < and & , which should be encoded as &lt; PS codepre都不更改有关标记有效字符<&的规则,应将其编码为&lt; and &amp; &amp; , respectively. , 分别。 For example, text like a<b&c would need to be written as a&lt;b&amp;c . 例如,像a<b&c这样的文本将需要写为a&lt;b&amp;c

<code>
<pre>
    int main(){  <br/ >
        printf("Hello World!");<br/ >
    }
</pre>
</code>

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

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