Skip to content

Q: How to use bootstrap-sass with @extend and mixins #1261

@bentolor

Description

@bentolor

I tried to ask this on Stackoverflow, but did not get any response yet. My question:

I'm trying to use Twitter Bootstrap with Angular 2, SCSS and the scaffold generated by angular-cli

I want to apply the best practice to not litter my markup with Bootstrap-specific CSS classes. Instead I want to apply/use them via @extend or as mixins:

.mybutton {
   @extend .btn;
}

My problem:

  • The SASS compiler throws an error, because it does not know about .btn
  • If I add @import "bootstrap"; to my .scss-File, it will render the full Bootstrap-CSS in every component css

Any clues for me how this can be matched with the Angular 2 / Angular-CLI approach of handling styles?

What I've done so far

  1. add @import "bootstrap"; to my app.component.scss

  2. Apply ViewEncapsulation.None to the AppComponent, so Angular 2 does not apply Shadow DOM emulation and the Bootstrap CSS applies to the whole application

     @Component({
        encapsulation: ViewEncapsulation.None
     })
    
  3. Include the following block into angular-cli-build.js, so relative @import will work

    sassCompiler: {
      cacheExclude: [/\/_[^\/]+$/],
      includePaths: [ 'node_modules/bootstrap-sass/assets/stylesheets' ]
    },
    vendorNpmFiles: [
        ...
        'bootstrap-sass/assets/**/*'           
    ]
    

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions