I am creating a documentation page and in one section I attempt to explain the anatomy of the HTML document. In the final outcome, it does not show the correct indentation.
Code:
<p>
<code>
<!DOCTYPE html>
<html lang="en-US" dir="ltr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Title Name</title>
</head>
<body>
<h1>This is a headline.</h1>
<p>1st paragraph.</p>
<p>Usually there's quite a lot here.</p>
</body>
</html>
</code>
</p>
I even added physical indentations and it is still not evident in the final result.
<p>
<code>
<!DOCTYPE html>
<html lang="en-US" dir="ltr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Title Name</title>
</head>
<body>
<h1>This is a headline.</h1>
<p>1st paragraph.</p>
<p>Usually there's quite a lot here.</p>
</body>
</html>
</code>
</p>
CodePudding user response:
The semantics of the <code>
element don't imply that whitespace is significant.
Combine it with a <pre>
element (instead of using a paragraph).
CodePudding user response:
This property should be what you are looking for:
code {
white-space: pre-wrap;
}
CodePudding user response:
Use pre
instead of code
. The text in pre
is "typically rendered using a non-proportional, or monospaced, font. Whitespace inside this element is displayed as written."
CodePudding user response:
A code tag doesn't honor whitespace. The easiest fix is to wrap the code with pre.
<p>
<pre>
<code>
... your code
</code>
<pre>
</p>