Using Bootstrap 4 with Angular
Bootstrap 4 is out and ready to be added to your Angular project. In this blog post I will discuss what’s new with Bootstrap 4 and how it aligns well with Angular. Before I get started, I should mention, there is a Material library that is supported by the Angular team. It provides all sorts of built in components that play well with Angular, and if you are already familiar with material I would encourage you too explore the @angular/material library. With that said, it doesn’t hurt to explore your options. The following will provide a quick rundown of Bootstrap 4. I have also added some examples to my ng-example repo that you can find at https://github.com/rmroot/ng-example.
The Approach to Bootstrap 4
Per the Bootstrap documentation, found at https://getbootstrap.com/docs/4.0/extend/approach/, the team has a set of guides that steer their approach:
- Components should be responsive and mobile-first
- Components should be built with a base class and extend via modifier classes
- Component states should obey a common z-index scale
- Whenever possible, use utilities over custom styles
- Whenever possible, avoid enforcing strict HTML requirements (child selectors)
**It is possible to access Dom elements without JQuery or window calls with ElementRef but even Angular’s documentation discourages its use for security reasons.
Now I will admit that number five may seem to be a bit counter intuitive to an Angular component, since every component has it’s own custom style sheet. But if done properly, those style sheets can be used for modifier classes (number two), and your application styling can still maintain the Bootstrap approach. If you do find the need to create custom styles, I would encourage you to use a BEM (Block Element Modifier) methodology, because it closely aligns with Bootstrap’s approach.
For more information on BEM: http://getbem.com/introduction/
Finally, numbers 1, 3, and 6 are just going to make your life easier. I’m a firm believer in a class first approach when styling web applications, it provides much more flexibility in your HTML.
Adding Bootstrap 4
Adding Bootstrap to your Angular application is an easy two step process. Just install it and import it to the global style sheet provided by the Angular CLI.
npm install bootstrap@latest --save-dev
styles.cssand add the following
What Stands Out in Bootstrap 4
My two personal favorite pieces of Bootstrap 4 are the Card component and the addition of Flexbox utilities. Cards can be used as content containers with all sorts of variety and options that can be applied to them. Using cards is an easy way to group and cleanly layout a set of content in your application. They also work seamlessly with the grid system, so using multiple cards for repetitive content (with *ngFor) is a breeze. In addition to the typical bootstrap grid system of rows and columns, the added Flexbox utilities also are a huge help in getting your content exactly where you want it. It gives you the full power of Flexbox CSS in the form of classes.
I use Bootstrap 4 and Angular on a daily basis for my current project, and I am constantly finding new ways to leverage it to make my life easier. It gives you a large set of utility type classes that can be applied as-is or overwritten in a given component. Sometimes a developer just wants a way to center content on a web page without having to write their own CSS, and that’s the sort of thing Bootstrap 4 provides for you.