2

How do I get the gray background color as in this word which is inside an inline <pre></pre>?

Here is what I have done. From the code snippet, inline seems to be fine after the </pre> but not before <pre>.

pre.inline {
    display: inline;
    background-color: #80b3FF;
    border-radius: 4px;
    padding: 3px
}
<p>We use <pre class="inline">model</pre> to specify the statistical model.</p> 
JACKY88
  • 3,391
  • 5
  • 32
  • 48
  • You should check [this out](https://stackoverflow.com/questions/4611591/code-vs-pre-vs-samp-for-inline-and-block-code-snippets) first. You should probably use `code` as inline instead of `pre`. – Mihail Minkov Dec 18 '18 at 03:59

4 Answers4

2

Other way to solve the problem using 'div' and 'span'

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

<div>We use <span class="inline">model</span> to specify the statistical model.

Sushil Sharma
  • 2,321
  • 3
  • 29
  • 49
1

This seems to be the result of the pre tag creating the new line despite being inline display. it works as expected if you change it to a span instead:

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

Or Code if you want the monospace font by default:

code{
    background-color: #80b3FF;
    border-radius: 4px;
    padding: 3px
}
<p>We use <code>model</code> to specify the statistical model.</p> 
M31
  • 1,348
  • 12
  • 16
1

It is because pre is not a phrasing content . See here Use div instead.

pre.inline {
    display: inline;
    background-color: #80b3FF;
    border-radius: 4px;
    padding: 3px
}
<div>We use <pre class="inline">model</pre> to specify the statistical model.</div>
David JP
  • 56
  • 1
1

Below is an image of what is rendered in browser with your code. And this pretty much explains the issue you are facing.

enter image description here

In order to solve this below are few ways. Use <div> instead of a <p>

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

Use <code> instead of <pre>

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

This link will help you understand a bit more about <code> and <pre>

And this link more about <p> element

Manish
  • 4,692
  • 3
  • 29
  • 41