1

I want to add a code (block) snippet in my HTML page using <code>:

<pre>
    <code class="codeblock">
        s3://bucket-name/partition_date=2018-05-22/views.parquet
        s3://bucket-name/partition_date=2018-05-22/clicks.parquet
      s3://bucket-name/partition_date=2018-05-21/views.parquet
        s3://bucket-name/partition_date=2018-05-21/clicks.parquet
        s3://bucket-name/partition_date=2018-05-20/views.parquet
        s3://bucket-name/partition_date=2018-05-20/clicks.parquet
        //...
    </code>
</pre>

But the code is shown as follows (right shifted):

enter image description here

How can I show the code correctly so that the approach is equally supported by Safari, Chrome, Firefox and IE?

3 Answers 3

1

You don't need <pre> and <code>.

Either just use <pre> like this:

<pre class="codeblock">
  s3://bucket-name/partition_date=2018-05-22/views.parquet
  s3://bucket-name/partition_date=2018-05-22/clicks.parquet
  s3://bucket-name/partition_date=2018-05-21/views.parquet
  s3://bucket-name/partition_date=2018-05-21/clicks.parquet
  s3://bucket-name/partition_date=2018-05-20/views.parquet
  s3://bucket-name/partition_date=2018-05-20/clicks.parquet
  //...
</pre>

Or use <code> and some CSS, like this:

.codeblock {
  white-space: pre
}
<code class="codeblock">
  s3://bucket-name/partition_date=2018-05-22/views.parquet
  s3://bucket-name/partition_date=2018-05-22/clicks.parquet
  s3://bucket-name/partition_date=2018-05-21/views.parquet
  s3://bucket-name/partition_date=2018-05-21/clicks.parquet
  s3://bucket-name/partition_date=2018-05-20/views.parquet
  s3://bucket-name/partition_date=2018-05-20/clicks.parquet
  //...
</code>

Most of all, reduce the amount of whitespace you have at the front of each line within the <pre> or <code> tags.

If your server controls what will be placed into the <pre> or <code> then you can reduce the amount of whitespace on those lines.

If not, then the client could read the textContent of the <pre> or <code> tag, split them into lines, trim them, rejoin them and set the value back into textContent.

Sign up to request clarification or add additional context in comments.

2 Comments

I wonder if it's possible to avoid shifting the code to the right? In the editor I have the code shifted to the right, because it looks clear visually. But I want to formatted it differently in the HTML page. It should have a background colour and should not be shifted.
You can always read the textContent of the <pre> or <code> tag, split them into lines, trim them, rejoin them and set the value back into textContent.
0

The pre element will render the code exactly as it's written, including the white space (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre).

So all those spaces you have at the beginning are going to push it to the right. Start by removing all the surrounding white space and then if it's still getting pushed to the right, take a look at your styles.

Comments

0

Your doing it right except you gotta remove all the tabs and spaces out, even on your development machine / IDE. Something like below. Notice, all that <code> is going to have any spaces/tabs preserved so you gotta take that into account in your source code.

<body>
    <main>
        <pre>
            <code class="codeblock">
s3://bucket-name/partition_date=2018-05-22/views.parquet
s3://bucket-name/partition_date=2018-05-22/clicks.parquet
s3://bucket-name/partition_date=2018-05-21/views.parquet
s3://bucket-name/partition_date=2018-05-21/clicks.parquet
s3://bucket-name/partition_date=2018-05-20/views.parquet
s3://bucket-name/partition_date=2018-05-20/clicks.parquet
            </code>
        </pre>
    </main>
</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.