CSS:内联一个<pre> tag

时间:2016-04-21 21:48:05

标签: html css

Here is my HTML:

<p>The following text <pre>is an inline preformat block</pre> and will not be parsed.</p>

I want it rendered as a single line, with a preformat block in the middle of the sentence. However, it is rendering as three separate lines:


The following text

is an inline preformat block
and will not be parsed.


And what I want is for it all to be on one single line. I have tried setting the style to use display:inline, but that only solves my problem halfway: no newline is introduced at the end of the pre block, but there is still one at the start.

As has been suggested elsewhere, I tried using white-space:nowrap, but it accomplishes absolutely nothing at all.

No solutions based on javascript or jquery, please. I want to make sure the solution works on browsers which have scripting disabled.

3 个答案:

答案 0 :(得分:5)

Solution #1 using <pre> (not recommended):
You can use the following code, but the <p> element is a little bit broken. If you want to avoid to affect all <p> elements, add class or id attribute to the <p> element.

pre, p {
  display:inline;
}
<p>The following text <pre>is an inline preformat block</pre> and will not be parsed.</p>

Solution #2 using <code>:
A better solution would be to replace the <pre> with <code>. The output looks the same like the solution using the <pre> element.

<p>The following text <code>is an inline preformat block</code> and will not be parsed.</p>

Solution #3 using <span>:
If you want to define the element yourself you can use the following:

p span {
  font-family:monospace;
}
<p>The following text <span>is an inline preformat block</span> and will not be parsed.</p>

答案 1 :(得分:1)

Can't you just use a span tag, and design that with css?

<p>The following text <span>is an inline preformat block</span> and will not be parsed.</p>

答案 2 :(得分:0)

您可以使用<code>。参见w3c