Sanskar Tiwari
IndianAppGuy Blog

IndianAppGuy Blog

Styled Code Embed in Html

Sanskar Tiwari's photo
Sanskar Tiwari
·Jul 12, 2022·

1 min read

Subscribe to my newsletter and never miss my upcoming articles

So i am working on landing page for click2contact.app and wanted to show the code styled so i used codemirror here is how i did it

Step1: Add these to the

  <!-- Code Mirror -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.41.0/codemirror.css" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.41.0/codemirror.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.41.0/mode/htmlmixed/htmlmixed.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.41.0/mode/xml/xml.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.41.0/mode/javascript/javascript.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.41.0/mode/css/css.js"></script>

Step2: Add the textarea element with your code.

Step3: then add this code after the text area in

// set the code editor
    var editor = CodeMirror.fromTextArea(document.getElementById("editor"), {
      lineNumbers: true,
      mode: "htmlmixed",
      htmlMode: true,
    });
 
Share this