HTML-pre标签和code标签

<pre>:预格式化标签

<code>:将字符设置为等宽字符的标签

在html文件中需要显示一些比如< >这种符号时需要使用到实体化字符

> &#60

< &#62

<!  DOCTYPE html>
<html>
<head>
    <title>预格式化</title>
    <meta charset = “utf-8”>
    <meta name = “viewport” content = “width=device-width, initial-scale=1.0”>
    <meta name = “keyword” content = “预格式化文本”>
    <meta name = “description” content = “预格式化纯文本”>
    <meta author = “王思怡”>
    <style>
    body{
    background:pink;
    }
    </style>    
</head>
<body>
    <!—- 以下代码的括号需要使用实体化字符来写 —->
    <pre>
    &#60;DOCTYPE !html&#62;
    &#60;html&#62;
    &#60;head&#62;
        &#60;title&#62;预格式化&#60;/title&#62;
        &#60;meta charset = “utf-8”&#62;
        &#60;meta name = “viewport” content = “width=device-width” initial = 1.0&#62;
        &#60;meta name = “keyword” content = “预格式化文本”&#62;
        &#60;meta name = “description” content = “预格式化纯文本”&#62;
        &#60;meta author = “王思怡”&#62;
        &#60;style&#62;
        body{
        background:pink;
        }
        &#60;/style&#62;    
    &#60;/head&#62;
    &#60;/html&#62;
    </pre>
<body>
</html>
<!DOCTYPE html>
<html>
<head>
    <title>code标签的使用</title>
    <meta charset = “utf-8”>
    <meta name = “viewport” content = “width=device-width, initial-scale=1.0”>
    <meta name = “keyword” content = “code标签的使用”>
    <meta name = “description” content = “code标签的使用”>
    <meta author = “王思怡”>
    <style>
    body{
    background:pink;
    }
    </style>
</head>
<body>
    <pre>
    <code>
        &#60;DOCTYPE !html&#62;
        &#60;html&#62;
        &#60;head&#62;
            &#60;title&#62;预格式化&#60;/title&#62;
            &#60;meta charset = “utf-8”&#62;
            &#60;meta name = “viewport” content = “width=device-width” initial = 1.0&#62;
            &#60;meta name = “keyword” content = “预格式化文本”&#62;
            &#60;meta name = “description” content = “预格式化纯文本”&#62;
            &#60;meta author = “王思怡”&#62;
            &#60;style&#62;
            body{
            background:pink;
            }
            &#60;/style&#62;    
        &#60;/head&#62;
        &#60;/html&#62;
    </code>
    </pre>
</body>
</html>

 

转载于:https://www.cnblogs.com/sucker/p/10933353.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: <code>pre</code>标签默认会保留空格和换行符,而<code>code</code>标签会被浏览器解析为等宽字体,所以将<code>code</code>标签放在<code>pre</code>标签里面后,会导致文本右对齐。解决方法可以在<code>code</code>标签里添加<code>style="display:block"</code>或者将<code>code</code>标签换成 <code>div</code> 标签。 ### 回答2: 当将`<code>`标签放在`<pre>`标签内时,浏览器默认会为`<pre>`标签内的文本设置等宽字体(monospace),以保持代码的格式和缩进。因此,`<code>`标签的字体会跟随`<pre>`标签的字体样式,导致字体显示偏右。 要解决这个问题,可以在`<pre>`标签内部再使用一个包装元素(如`<span>`),并将需要应用等宽字体的代码放入包装元素中,然后使用CSS样式将包装元素的字体样式修改为等宽字体即可。具体步骤如下: 1. 在`<pre>`标签内添加一个包装元素,如`<span>`: ```html <pre><span><code>console.log();</code></span></pre> ``` 2. 使用CSS将包装元素的字体样式修改为等宽字体,比如使用`font-family`属性来设置: ```html <style> pre span { font-family: monospace; } </style> ``` 这样就能保持`<pre>`标签内的文本仍然是等宽字体,而`<code>`标签内的字体不会跑到右边了。 希望这个解决方案对你有帮助! ### 回答3: 在HTML中,`pre`标签定义了预格式化的文本,其中的文本会保留空格和换行符的原始格式。而`code`标签用于表示计算机代码,通常会应用一些默认的样式。 当将`code`标签放在`pre`标签内时,`code`的样式可能会受到`pre`的样式影响,导致字体跑到右边。这是因为`pre`标签默认具有一个固定的宽度,而`code`标签默认是一个行内元素,会根据父元素的宽度自动调整位置。 您可以通过自定义样式来解决这个问题。可以使用CSS中的`display: block`样式来将`code`标签转换为块级元素,然后再根据需要设置宽度、边距和对齐等样式。 ```html <pre> <code style="display: block;">console.log();</code> </pre> ``` 通过将`code`标签转换为块级元素,它将独占一行,不再受到`pre`标签的影响,从而正常显示在左侧。您还可以通过为`pre`标签或`code`标签添加额外的CSS样式来实现更复杂的布局和样式效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值