0

I in my application I need to show code snippets of user entered code

How can I create code snippets styled with syntax highlights using HTML, CSS, JS or may Jquery like these examples -

Example 1 of Carbon Website

enter image description here

Example 2 of W3 School

enter image description here

Example 3 of Stack Overflow

// As you can see this code snippet image or customized code box
//this is the example of stackoverflow website

const simpleConst = "How can I create this type of code snippets by using Html CSS Js";

console.log(simpleConst);

any help will be always appreciated :)

2
  • 1
    Search for js syntax highlighter, there are a lot of them. Commented May 20, 2022 at 2:53
  • @vee thank you for this information. I think it will help Commented May 20, 2022 at 3:01

1 Answer 1

2

I understand your questions like You wanna make code blocks in HTML Screen.

maybe You should see this article. https://css-tricks.com/web-component-for-a-code-block/

and make user input or textarea for typing code ...

    <div>
      <input id="textElm" />
      <br />
      <code id="codeElm">hi</code>
    </div>
const input = document.getElementById("textElm");
const code = document.getElementById("codeElm");

input.addEventListener("change", (e) => {
  console.log(e.target.value);
  console.log(code)
  code.innerHTML = e.target.value;
});

or use https://highlightjs.org/

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

2 Comments

I thinks so too. If you want to highlighting code for share blog post. just wrap the code in <code></code>
or Use github gist

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.