I bumped in to same issue and spent over an hour to find solution. I agree with @Fico's answer and wanted to support it by additional information.
Instead of doing this:
<pre><code>
My code snippet
Another line
</code></pre>
You want to do this:
<pre><code> My code snippet
Another line
</code></pre>
Note that you need to use same spaces for indentation on first line.
I looked at several other "standard webistes". For example, StackOverflow does this for code snippets inside <pre><code>
. The official demo examples of highlight.js library also uses the same convention. This feels bit ugly in HTML source, but The rule of thumb seems to be that your content inside <code>
should start at the same line as <code>
element.
Also there is a problem with solution that @Marc Audet proposed. If you use negative top margin, it will overwrite on the border if you have one (or if you put it in future).
There is probably workaround if you are willing to use little bit of JavaScript. You can basically trim all contents inside <pre><code>
simply by using this:
<script>
$( document ).ready(function() {
$(document.body).find("pre code").each(function() {
$(this).html($.trim($(this).html()));
});
});
</script>
You can see fiddle here: http://jsbin.com/bayawazi/2/edit
The advantage of JavaScript approach is that you have much more freedome to put <code>
element. Also most code snippets its probably good idea to remove extra lines.