Sponsored Link

Monday, September 2, 2013

Javascript :: Ace Editor Positioning with Bootstrap3

Ace editor is a fantastic option for live source code editor on the web. One problem you come across when using Ace Editor is with the default absolute positioning. Also it doesn't fit inside a container div by default. Here is how I overcame these issues.

The example from ace website uses absolute positioning by default, as show below. But luckily, ace can work perfectly with relative positioning

<!DOCTYPE html>
<html lang="en">
<head>
<title>ACE in Action
<style type="text/css" media="screen">
    #editor { 
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
    }
</style>
</head>
<body>

function foo(items) { var x = "All this is syntax highlighted"; return x; }
<script src="/ace-builds/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script> <script> var editor = ace.edit("editor"); editor.setTheme("ace/theme/monokai"); editor.getSession().setMode("ace/mode/javascript"); </script> </body> </html>

In order to put ace-editor inside a container div, we can give relative positioning to the editor and remove the positioning attribs, like top, right, bottom and left.

#editor { 
    position: relative;
}

We also need to give height, otherwise editor won't show up.

#editor { 
    position: relative;
    height: 300px;
}

That said, when using Twitter Bootstrap, you can absolutely forget about relative positioning of ace. But we still need to set height, as ace does not fill. In the following jsfiddle, I have used Bootstrap's panel component for showing the editor.

Full Screen Result is available here. http://jsfiddle.net/deepumohanp/U5JtP/embedded/result/

Fiddle here: http://jsfiddle.net/deepumohanp/U5JtP/

References

  1. How do I make a textarea an ace editor?
  2. Ace doesn't fit inside a container div
Related Posts Plugin for WordPress, Blogger...