【问题标题】:Apply Twitter Bootstrap stylings to CodeMirror?将 Twitter Bootstrap 样式应用于 CodeMirror?
【发布时间】:2012-10-05 08:03:11
【问题描述】:

我有一个带有文本区域的表单,用户可以在其中编写一些代码。我正在用 CodeMirror 代码编辑器框替换那个 textarea,所以我想对其应用 Bootstrap 样式。

这就是表单现在的样子,除了代码编辑器之外的所有表单元素都有 Bootstrap 样式:

所以特别是,我认为我需要在鼠标悬停时为代码编辑器提供圆角、边框、正确的宽度(input-xxlarge)和蓝色高亮。

我该怎么做?除了手动复制必要的 CSS 之外,还有其他方法吗?

更新

我尝试从 Bootstrap 复制 textarea CSS,当我在代码编辑器中单击时,除了焦点 CSS 之外,一切看起来都不错。这是我得到的:

亮点在内部,而不是外部。有什么想法可以解决这个问题吗?

这是我从 Bootstrap 复制添加的 CSS:

    .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);  
    }

【问题讨论】:

  • 复制了你的代码,它似乎对我有用,没有任何改变,或者你修复了它..

标签: css twitter-bootstrap code-editor


【解决方案1】:

代码镜像和引导程序(v3 和 v4):
样式尚不支持验证状态(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;
}

【讨论】:

  • 2019,这仍然像 Bootstrap 4 中的魅力!
  • 在 2020 年使用 Bootstrap 4!我只需要将padding: 6px 12px; 更改为padding: 6px 6px; 即可使其看起来正确。谢谢!
  • @ZackTarr - 感谢您的肯定,但我认为 Bootstrap 3 和 Bootstrap 4 的 box-shadow 是不同的。查看更新的代码。
  • @JosephVictorZammit - 感谢您的肯定,但我认为 Bootstrap 3 和 Bootstrap 4 的 box-shadow 是不同的。查看更新的代码。
【解决方案2】:

CodeMirror 隐藏了原始的textarea 并创建了一个由div 和pre 元素组成的(相当复杂的)结构。你可以设置最外层的div,它有一个.CodeMirror类来达到同样的效果。

这将需要自定义 CodeMirror 样式表或为类/元素添加您自己的样式。如果您使用 LESS 构建 Bootstrap,可能有一种方法可以应用 mixin 以避免重复 textarea 样式,尽管重复的数量可能很少。

【讨论】:

  • 太棒了,谢谢!我尝试从 Bootstrap 的 textarea 复制 CSS,它看起来基本正确,除非我专注于代码编辑器。 (我在上面更新了我的问题。)蓝色突出显示在代码编辑器内部,而不是围绕它。你对如何解决这个问题有什么建议吗?
  • 由于元素类型不同,轮廓的行为可能会有所不同。我会设置一个快速的 JS Fiddle 看看是不是这样。
  • jsfiddle.net/YhLjn - 看起来轮廓在div 和textarea 上的行为相同。也许focused 类被应用于内部元素?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-05
  • 2023-03-16
  • 2017-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-10
相关资源
最近更新 更多