Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeMirror : how to limit height in editor

Tags:

codemirror

I am using codemirror to show some code on a webpage. But when I initialize the codemirror editor, the height of the editor is way more than the number of lines in the code. Please see this fiddle or below image to understand what I am saying screen shot of extra height in code mirror Below is the code to create the codemirror editor.

var myCodeArea = document.getElementById("codeArea");
var myCodeMirror = CodeMirror(function(elt) {
  myCodeArea.parentNode.replaceChild(elt, myCodeArea);
}, {value: myCodeArea.innerHTML,
   lineNumbers:true, mode:"javascript"});

I read codemirror doc but not able to figure out which property controls the height.

Please help me with this

like image 837
Nitiraj Avatar asked Feb 07 '15 03:02

Nitiraj


People also ask

How to set height of codemirror?

Programmatically set the size of the editor (overriding the applicable CSS rules). width and height can be either numbers (interpreted as pixels) or CSS units ("100%", for example). You can pass null for either of them to indicate that that dimension should not be changed.


3 Answers

The height can be set through CSS (by giving the .CodeMirror class a height property), or by calling the editor's setSize method.

If you want the editor height to correspond to the height of it's contents, see this demo.

like image 185
Marijn Avatar answered Nov 04 '22 00:11

Marijn


If you see your fiddle, there is an entry in css which defines height.

.CodeMirror {


 /* Set height, width, borders, and global font properties here */
    font-family: monospace;
    height: 300px;
}

You can either override this css or use setSize(width,height) method of codemirror.

like image 34
Bala Avatar answered Nov 03 '22 22:11

Bala


Might help someone.

<textarea class="form-control" id="exampleTextarea"></textarea>

.

var config, editor;

            config = {                  
                lineNumbers: true,
                mode: "text/javascript",
                lineWrapping: true,
                htmlMode: true,
                matchClosing: true,       
                indentWithTabs: true,
                readOnly: true
            };


            editor = CodeMirror.fromTextArea(document.getElementById("exampleTextarea"), config);
            editor.setSize(900,"100%");
like image 27
Gero Avatar answered Nov 04 '22 00:11

Gero