Bootstrap code
Bootstrap allows you to display code in two ways:
The first is the <code> tag. If you want to display code inline, you should use the <code> tag.
The second type is the <pre> tag. If the code needs to be displayed as a separate block element or if the code has multiple lines, then you should use the <pre> tag.
Please make sure that when you use the <pre> and <code> tags, the opening and closing tags use the unicode variants: < and >.
Let's take a look at the following example:
Instance
<!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 代码</title> <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet"> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script> </head> <body> <p><code><header></code> 作为内联元素被包围。</p> <p>如果需要把代码显示为一个独立的块元素,请使用 <pre> 标签:</p> <pre> <article> <h1>Article Heading</h1> </article> </pre> </body> </html>
Run Example»
Click the "Run Example" button to view online examples
More examples
<var> Variable assignment: #Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css"> <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>代码</h2> <p>使用 var 元素表示变量:</p> <p><var>x</var> = <var>a</var><var>b</var> + <var>y</var></p> </div> </body> </html>
Click the "Run instance" button to view the online instance
<kbd> Tip: CTRL + P
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css"> <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>代码</h2> <p>使用 kbd 元素表示按键输入:</p> <p>使用 <kbd>ctrl + p</kbd> 来打开打印窗口。</p> </div> </body> </html>
Click the "Run Instance" button to view the online instance
<pre class="pre-scrollable"> Multiple lines of code with scroll bars
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css"> <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>代码</h2> <p>使用 pre 元素输出多行:</p> <pre>在 pre 元素中的文本 宽度的显示与文本的宽度一样, 保留了 空 格 和 换行。</pre> <p>如果你添加 .pre-scrollable 类, pre 元素最大的高度 max-height 为 350px ,并生成一个 Y 轴的滚动条:</p> <pre class="pre-scrollable">在 pre 元素中的文本 宽度的显示与文本的宽度一样, 保留了 空 格 和 换行。</pre> </div> </body> </html>
Click the "Run Example" button to view the online example
<samp> Computer program output: Sample output
Example
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css"> <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>代码</h2> <p>使用 samp 元素包含电脑输出的内容:</p> <p><samp>This text is output from a computer program....</samp></p> </div> </body> </html>
Click the "Run instance" button to view the online instance
<code> Same line of code Fragment: span, div
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css"> <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>代码</h2> <p>使用 code 元素来表示代码片段:</p> <p>以下是 HTML 元素: <code>span</code>, <code>section</code>, 和 <code>div</code> 。</p> </div> </body> </html>
Click the "Run instance" button to view the online instance