line with backtick or code block
line with backtick or code block
Why is the first line automatically indented by one space when using backticks or <code>
tags to surround multiple lines?
Is this intentional or a design flaw?
The padding of the <code>
element is set to 1px 5px 1px 5px
, thus indenting the left and right by five pixels. This makes sense because inline code
needs to be properly spaced and it'd look weird without the padding.
Why does it look weird when using it for multiple lines? Because <code>
is not meant for that. <code>
is for inline formatting. You should use <pre></pre>
instead if you want a block element.
Compare the incorrect backtick / <code>
variant:
<code>multiline
foo</code>
Which renders as:
multiline
foo
And the correct:
<pre>
multiline
foo
</pre>
Which renders as:
multiline foo
Always use the latter for multiline code. You can of course indent code by four spaces as well to achieve the same thing. Or press CtrlK to indent selected text with our editor.
multiline
foo
<code>
with <pre>
it will add a line break. While this would suit in this case, there are probably cases where I want to avoid line breaks. What then?
<pre>
(currently) is styled using a fixed-width font, I'd say for code one should use <pre><code>
. (Which is exactly what using the four spaces Markdown gets one. Using the four spaces also gets one automatic encoding of HTML entities such as changing <strong>
into <strong>
.)
<pre><code>
you'll get an extra line break if you start the code on the next line.