如何命名Twitter Bootstrap使样式不会发生冲突

And*_*rew 102 less twitter-bootstrap

我想使用Twitter Bootstrap,但仅限于特定元素,因此我需要找出一种方法来为所有Twitter Bootstrap类添加前缀,或者使用less mixins.我对此没有经验,所以我不太明白该怎么做.这是我尝试设计样式的HTML示例:

<div class="normal-styles">
  <h1>dont style this with bootstrap</h1>
  <div class="bootstrap-styles">
    <h1>use bootstrap</h1>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

在这个例子中,Twitter Bootstrap将是正常的样式h1,但是我想更加选择性地应用Twitter Bootstrap样式的哪些区域.

And*_*rew 127

事实证明这比我想象的要容易.Less和Sass都支持命名空间(甚至使用相同的语法).当您包含引导程序时,您可以在选择器中对其进行命名空间:

.bootstrap-styles {
  @import 'bootstrap';
}
Run Code Online (Sandbox Code Playgroud)

更新:对于较新版本的LESS,以下是如何操作:

.bootstrap-styles {
  @import (less) url("bootstrap.css");
}
Run Code Online (Sandbox Code Playgroud)

这里回答了类似的问题.

  • 这在版本3之后不再起作用(http://hereswhatidid.com/2014/02/use-bootstrap-3-styles-within-the-wordpress-admin/) (3认同)
  • @Batman您不想使用CDN,因为您实际上是在重写所有Bootstrap以包含前缀.您需要使用像SASS这样的预处理器来"@ import"原始的Bootstrap.我也有模态问题,我认为因为Bootstrap将某些类应用于顶级body标签.我不记得我是否找到了解决方案.最后,我想我最终写了自己的模态替代品. (2认同)
  • 有人可以解释这个进口声明应该去哪里吗?看起来像一个单独的.less文件.我这样做并将其放在bootstrap less文件夹中,但它不能使用命名空间进行编译.我想我错过了一些东西 (2认同)

Kev*_*vin 34

@Andrew很棒的回答.您还需要注意,bootstrap修改了body {},主要是为了添加字体.因此,当您通过LESS/SASS命名它时,输出css文件如下所示:(在bootstrap 3中)

.bootstrap-styles body {
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-size: 14px;
    line-height: 1.428571429;
    color: #333333;
    background-color: white;
}
Run Code Online (Sandbox Code Playgroud)

但很显然你的div里面没有body标签,所以你的bootstrap内容不会有bootstrap字体(因为所有bootstrap都从parent继承了字体)

要修复,答案应该是:

.bootstrap-styles {
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-size: 14px;
    line-height: 1.428571429;
    color: #333333;
    background-color: white;

    @import 'bootstrap';
}
Run Code Online (Sandbox Code Playgroud)

  • 注意力!我不知道为什么,但在某些地方生成的 css 较少,例如 .my-prefix .my-prefix ...,因此您还需要搜索和替换它。 (2认同)

Sin*_*ity 9

将@Andrew,@ Kevin和@adamj的答案放在一起(加上其他一些样式),如果在名为"bootstrap-namespaced.less"的文件中包含以下内容,则只需将"bootstrap-namespaced.less"导入到任何更少的内容中文件:

// bootstrap-namespaced.less

// This less file is intended to be imported from other less files. 
// Example usage:
// my-custom-namespace {
//  import 'bootstrap-namespaced.less';
// }

// Import bootstrap.css (but treat it as a less file) to avoid problems 
// with the way Bootstrap is using the parent operator (&).
@import (less) 'bootstrap.css';

// Manually include styles using selectors that will not work when namespaced.

@import 'variables.less';

& {
    // From normalize.less

    // Previously inside "html {"
    // font-family: sans-serif; // Overridden below
    -ms-text-size-adjust: 100%;
    -webkit-text-size-adjust: 100%;

    // Previously inside "body {"
    margin: 0;

    // From scaffolding.less

    // Previously inside "html {"
    // font-size: 10px; // Overridden below
    -webkit-tap-highlight-color: rgba(0,0,0,0);

    // Previously inside "body {"
    font-family: @font-family-base;
    font-size: @font-size-base;
    line-height: @line-height-base;
    color: @text-color;
    background-color: @body-bg;
}
Run Code Online (Sandbox Code Playgroud)


Tob*_*nst 8

首先从 github 克隆引导程序:

git clone https://github.com/twbs/bootstrap.git
Run Code Online (Sandbox Code Playgroud)

安装要求:

npm install
Run Code Online (Sandbox Code Playgroud)

打开 scss/bootstrap.scss 并添加您的命名空间:

.your-namespace {
    @import "functions";
    ...
    @import "utilities/api";
}
Run Code Online (Sandbox Code Playgroud)

编译引导程序:

npm run dist
Run Code Online (Sandbox Code Playgroud)

打开并从和标记dist/css/bootstrap.css中删除命名空间。htmlbody

然后将 dist 文件夹的内容复制到您的项目中,一切就完成了。

玩得开心!

  • 现在可能值得一提的是,Bootstrap 5 正在开发中,并且现在是存储库的主要分支,在克隆存储库后,您可能需要签出您想要命名空间的版本的分支。例如,我检查了 v4-dev 分支,这样我就可以命名 Bootstrap v4 了。 (2认同)

小智 7

添加命名空间以引导CSS将破坏模态功能,因为引导程序将"模态背景"类直接添加到正文.解决方法是在打开模态后移动此元素,例如:

$('#myModal').modal();
var modalHolder = $('<div class="your-namespace"></div>');
$('body').append(modalHolder);
$('.modal-backdrop').first().appendTo(modalHolder); 
Run Code Online (Sandbox Code Playgroud)

您可以删除"hide.bs.modal"事件的处理程序中的元素.