【问题标题】:CSS: add background color to inline <pre> text [duplicate]CSS:为内联 <pre> 文本添加背景颜色 [重复]
【发布时间】:2019-05-18 11:09:02
【问题描述】:

如何获得内联 &lt;pre&gt;&lt;/pre&gt; 内的 this word 中的灰色背景颜色?

这就是我所做的。从代码 sn-p 来看,&lt;/pre&gt; 之后的内联似乎很好,但&lt;pre&gt; 之前就不行。

pre.inline {
    display: inline;
    background-color: #80b3FF;
    border-radius: 4px;
    padding: 3px
}
&lt;p&gt;We use &lt;pre class="inline"&gt;model&lt;/pre&gt; to specify the statistical model.&lt;/p&gt; 

【问题讨论】:

  • 你应该先检查this out。您可能应该使用code 作为内联而不是pre。

标签: html css


【解决方案1】:

这似乎是 pre 标记创建新行的结果,尽管是内联显示。如果您将其更改为跨度,它会按预期工作:

span{
    background-color: #80b3FF;
    border-radius: 4px;
    padding: 3px
}
&lt;p&gt;We use &lt;span&gt;model&lt;/span&gt; to specify the statistical model.&lt;/p&gt; 

如果你想要默认的等宽字体,或者代码:

code{
    background-color: #80b3FF;
    border-radius: 4px;
    padding: 3px
}
&lt;p&gt;We use &lt;code&gt;model&lt;/code&gt; to specify the statistical model.&lt;/p&gt; 

【讨论】:

    【解决方案2】:

    因为  pre 不是分句内容。请参阅here 改为使用 div。

    pre.inline {
        display: inline;
        background-color: #80b3FF;
        border-radius: 4px;
        padding: 3px
    }
    &lt;div&gt;We use &lt;pre class="inline"&gt;model&lt;/pre&gt; to specify the statistical model.&lt;/div&gt;

    【讨论】:

      【解决方案3】:

      下面是使用您的代码在浏览器中呈现的图像。这几乎解释了您面临的问题。

      为了解决这个问题,下面有几种方法。使用&lt;div&gt; 而不是&lt;p&gt;

      pre.inline {
          display: inline;
          background-color: #80b3FF;
          border-radius: 4px;
          padding: 3px
      }
      &lt;div&gt;We use &lt;pre class="inline"&gt;model&lt;/pre&gt; to specify the statistical model.&lt;/div&gt; 

      使用&lt;code&gt; 而不是&lt;pre&gt;

      code.inline {
          display: inline;
          background-color: #80b3FF;
          border-radius: 4px;
          padding: 3px;
      }
      &lt;p&gt;We use &lt;code class="inline"&gt;model&lt;/code&gt; to specify the statistical model.&lt;/p&gt;

      This link 将帮助您更多地了解&lt;code&gt; 和&lt;pre&gt;

      还有this link 更多关于&lt;p&gt; 元素

      【讨论】:

        【解决方案4】:

        使用“div”和“span”解决问题的其他方法

            .inline {
            background-color: #80b3FF;
            border-radius: 4px;    padding: 3px
        }
        

        &lt;div&gt;We use &lt;span class="inline"&gt;model&lt;/span&gt;指定统计模型。

        【讨论】:

          猜你喜欢
          • 2018-08-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-01
          • 2021-04-22
          • 1970-01-01
          • 2019-04-22
          • 2019-02-28
          相关资源
          最近更新 更多