Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply Twitter Bootstrap stylings to CodeMirror?

I have a form with a textarea where users can write some code. I'm replacing that textarea with a CodeMirror code editor box, so I want to apply Bootstrap stylings to it.

This is what the form looks like right now, where all the form elements except the code editor have Bootstrap stylings: enter image description here

So in particular, I think I need to give the code editor rounded corners, a border, the correct width (input-xxlarge), and blue highlights when mousing over.

How do I do this? Is there a way of doing this besides manually copying over the necessary CSS?

UPDATE

I tried copying over the textarea CSS from Bootstrap, and all looks good except the focus CSS when I click inside the code editor. This is what I get:

enter image description here

The highlight is on the inside, instead of the outside. Any ideas how I fix this?

This is the CSS I added by copying from Bootstrap:

    .CodeMirror {
      line-height: 1.3em;
      font-family: monospace;

      /* Necessary so the scrollbar can be absolutely positioned within the wrapper on Lion. */
      position: relative;
      /* This prevents unwanted scrollbars from showing up on the body and wrapper in IE. */
      overflow: hidden;
      background-color: white;
      width: 530px;

      /* Copied from Bootstrap's textarea */
      display: inline-block;
      padding: 4px 6px;
      margin-bottom: 9px;
      color: #555555;
      border: 1px solid #ccc;
      -webkit-border-radius: 3px;
      -moz-border-radius: 3px;
      border-radius: 3px;
      -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
      -moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
      box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
      -webkit-transition: border linear 0.2s, box-shadow linear 0.2s;
      -moz-transition: border linear 0.2s, box-shadow linear 0.2s;
      -ms-transition: border linear 0.2s, box-shadow linear 0.2s;
      -o-transition: border linear 0.2s, box-shadow linear 0.2s;
      transition: border linear 0.2s, box-shadow linear 0.2s;  
    }

    .CodeMirror-focused {
      /* Copied from Bootstrap's textarea */
      border-color: rgba(82, 168, 236, 0.8);
      outline: 0;
      outline: thin dotted \9;
      /* IE6-9 */

      -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
         -moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
              box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);  
    }
like image 860
grautur Avatar asked Oct 05 '12 08:10

grautur


2 Answers

CodeMirror hides the original textarea and creates a (fairly complex) structure of div and pre elements. You can style the outermost div which has a class of .CodeMirror to achieve the same effect.

This will require customizing the CodeMirror stylesheet or adding your own style for the class/element. If you are building Bootstrap using LESS, there may be a way to apply a mixin to avoid duplicating the textarea style, though the amount of duplication is probably minimal.

like image 133
Tim M. Avatar answered Nov 07 '22 10:11

Tim M.


Code Mirror and Bootstrap (v3 and v4):
Styling does not yet support validation states (has-error, has-warning, has-success)

.CodeMirror {
  /* Bootstrap Settings */
  box-sizing: border-box;
  margin: 0;
  font: inherit;
  overflow: auto;
  font-family: inherit;
  display: block;
  width: 100%;
  padding: 6px 12px;
  font-size: 14px;
  line-height: 1.42857143;
  color: #555;
  background-color: #fff;
  background-image: none;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075); /* Bootstrap 3 */
  box-shadow: none; /* Bootstrap 4 */
  transition: border-color .15s ease-in-out, box-shadow .15s ease-in-out;
  /* Code Mirror Settings */
  font-family: monospace;
  position: relative;
  overflow: hidden;
}
    
.CodeMirror-focused {
  /* Bootstrap Settings */
  border-color: #66afe9;
  outline: 0;
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 8px rgba(102, 175, 233, .6); /* Bootstrap 3 */
  box-shadow: 0 0 0 .2rem rgba(0, 123, 255, .25); /* Bootstrap 4 */
  transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
}
like image 43
tim-montague Avatar answered Nov 07 '22 10:11

tim-montague