skip to Main Content

I recently started to learn HTML and CSS.

When I use <code></code>, it just shows up as plain, black and white text, although I would like it to automatically highlight the syntax appropriately.

It currently looks like this:

text = "Hello World!"
print(text)

I would like it to look like this:

text = "Hello World!"
print(text)

Is it possible to do that with just HTML and CSS?

2

Answers


  1. automatically highlight

    Is it possible to do that with just HTML and CSS?

    No.


    It requires more detailed markup and CSS.

    e.g.

    var {
      color: blue;
    }
    
    code.operator {
      color: red;
    }
    
    code.literal {
      color: purple;
    }
    
    code.function-call {
      color: green;
    }
    <pre><code class="python"><var>text</var> <code class="operator">=</code> <code class="literal">"Hello World!"</code>
    <code class="function-call">print(<var>text</var>)</code>

    To do it automatically you need to involve a programming language. You could use it to preprocess your code before it is delivered to the browser, or use client-side JavaScript to do it after the page has loaded.

    There are various libraries available to to this.

    Login or Signup to reply.
  2. For syntax highlighting, you typically need to use additional tools or libraries. One common approach is to use JavaScript-based syntax highlighting libraries like "highlight.js" or "Prism.js." These libraries can dynamically apply appropriate styles to different parts of the code to make it visually appealing and easy to read.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search