简体   繁体   English

如何使用 ng2-bootstrap 覆盖 angular2 中的 twitter bootstrap 样式

[英]How to override twitter bootstrap styles in angular2 with ng2-bootstrap

I am using ng2-bootstrap from valor-software.我正在使用 valor-software 的 ng2-bootstrap。 When I start the website using ng serve, the page that is served up has the following style tags in the header:当我使用 ng serve 启动网站时,提供的页面在标题中具有以下样式标记:

<style type="text/css">/* You can add global styles to this file, and also import other style files */
</style>

<style type="text/css">/*!
 * Bootstrap v3.3.7 (http://getbootstrap.com)
 * Copyright 2011-2016 Twitter, Inc.
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 *//*! normalize.css v3.0.3 | MIT License | github.com/necolas/normalize.css */html{font-family:sans-serif;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%}body{margin:0}

It is clearly pulling the second inline style in from the node_modules/bootstrap/dist/css/bootstrap.css file.它显然是从 node_modules/bootstrap/dist/css/bootstrap.css 文件中提取第二个内联样式。 I want to override the background-color on the body tag.我想覆盖 body 标签上的背景颜色。 I can change the bootstrap.css file in the node-modules folder, but that doesn't feel like the right solution.我可以更改 node-modules 文件夹中的 bootstrap.css 文件,但这不是正确的解决方案。

Changing the style.css file doesn't do anything.更改 style.css 文件没有任何作用。 Even changing the index.html file doesn't seem to do anything.即使更改 index.html 文件似乎也没有任何作用。 The styles appear to be overridden.样式似乎已被覆盖。

Any help would be most appreciated.非常感激任何的帮助。

thanks谢谢

I found the solution after some digging.经过一番挖掘,我找到了解决方案。

In the angular-cli.json file was the following:angular-cli.json文件中是这样的:

"apps": [
{
  "root": "src",
  "outDir": "dist",
  "assets": [
    "assets",
    "favicon.ico"
  ],
  "index": "index.html",
  "main": "main.ts",
  "test": "test.ts",
  "tsconfig": "tsconfig.json",
  "prefix": "app",
  "mobile": false,
  "styles": [
    "styles.css",
    "../node_modules/bootstrap/dist/css/bootstrap.min.css"
  ],
  "scripts": [],
  "environments": {
    "source": "environments/environment.ts",
    "dev": "environments/environment.ts",
    "prod": "environments/environment.prod.ts"
  }
}

], ],

I just needed to switch the order of the styles我只需要切换样式的顺序

"styles": [
    "../node_modules/bootstrap/dist/css/bootstrap.min.css",
    "styles.css"
  ],

and add any overrides to the src/styles.css file并将任何覆盖添加到 src/styles.css 文件

If you are using sass, which I advise you should do, then you can place your overrides after you import bootstrap in your styles.scss file.如果您正在使用 sass,我建议您应该这样做,那么您可以在将 bootstrap 导入到style.scss文件后放置您的覆盖。 For example例如

// version 4
@import 'variables';
@import '~bootstrap/scss/bootstrap';

// START bootstrap override
.card {
  margin-bottom: 0.5rem;
}
.card-body {
  padding: 1rem;
}

Maybe they changed some things in the updates since this post in December.也许他们在 12 月发布这篇文章后的更新中更改了一些内容。 But I didn't have to make any changes to my angular-cli.json file, but to note the only style listed there was the default styles.css that's in the root src folder.但我没有做我的角cli.json文件进行任何更改,但唯一需要注意的风格上市有默认的styles.css这根src文件夹。 But what ended up working for me was just adding some @import statements for my custom css files to the bootstrap.css file and making sure that the stylesheet was referenced in my index.html.但最终对我有用的只是为我的自定义 css 文件添加了一些@import语句到 bootstrap.css 文件,并确保在我的 index.html 中引用了样式表。

My setup is @angular 2.4.7, node-sass 4.5, node 6.9.5, bootstrap 4, ngBootstrap 1.0.0-alpha我的设置是@angular 2.4.7、node-sass 4.5、node 6.9.5、bootstrap 4、ngBootstrap 1.0.0-alpha

The latest development release of ng2-bootstrap includes instructions I supplied on how to use bootstrap 4 with @angular/cli. ng2-bootstrap 的最新开发版本包括我提供的有关如何将 bootstrap 4 与 @angular/cli 一起使用的说明。
The main part of the instructions should still be applicable to bootstrap 3 without too much trouble.说明的主要部分应该仍然适用于引导程序 3,不会有太多麻烦。 The main point of interest for you is that if you use SASS and define a variable.scss file then any overrides you require can be placed in that file.您的主要兴趣点是,如果您使用 SASS 并定义一个 variable.scss 文件,那么您需要的任何覆盖都可以放置在该文件中。
If you do want to go this way you will have to use the SASS version of bootstrap (the github repository for the SASS version also shows details of how to override variables).如果您确实想采用这种方式,则必须使用引导程序的 SASS 版本(SASS 版本的 github 存储库还显示了如何覆盖变量的详细信息)。

Check bottom of src/styles.sass file for bootstrap import like this:检查src/styles.sass文件底部的引导程序导入,如下所示:

/* Importing Bootstrap SCSS file. */
@import '~bootstrap/scss/bootstrap'

Moving this line to the beginning of the file fixed my problem.将此行移动到文件的开头解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM