Book Image

Web Development with Bootstrap 4 and Angular 2 - Second Edition

By : Sergey Akopkokhyants, Stephen Radford
Book Image

Web Development with Bootstrap 4 and Angular 2 - Second Edition

By: Sergey Akopkokhyants, Stephen Radford

Overview of this book

<p>Two of the most popular frontend frameworks, Angular and Bootstrap, have undergone a major overhaul to embrace emerging web technologies so that developers can build cutting-edge web applications.</p> <p>Inside this title you'll dive, fingers first, into the basics of both the tools, and once you're familiar with them, you'll move onto Bootstrap's new grid system and Angular's built-in directives. You'll then learn how to format output using Angular's pipes and how to make use of the built-in router to set up routes for all your components.</p> <p>Webpack will be your buddy to wrap up your project. Then, after throwing in some SASS to make things pretty, you'll learn how to validate the forms you've built and debug your application. Finally, you'll go on to learn how to obtain smooth transitioning from Bootstrap to Angular and then how to hook up with a server and use Firebase as the persistence layer.</p> <p>Once you're done with this book, you'll not only have a lovely little e-commerce application running, but you'll also take with you the confidence to innovate and build your own applications with ease.</p>
Table of Contents (16 chapters)
Web Development with Bootstrap 4 and Angular 2 - Second Edition
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface

Integrating Bootstrap 4


Now that we've created our Hello World application, and everything is working as expected, it's time to get involved with Bootstrap and add a bit of style and structure to our app. At the time of writing this book Bootstrap 4 was in alpha version, so bear in mind that the code and markup of your application might be slightly different. We need to add the Bootstrap 4 style sheet into the index.html file:

<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.css"> 
<link rel="stylesheet" href="styles.css"> 

The application is currently misaligned to the left, and everything is looking cramped, so let's sort that out first with a bit of scaffolding. Bootstrap comes with a great mobile first responsive grid system that we can utilize with the inclusion of a few div elements and classes. First, though, let's get a container around our content to clean it up immediately:

Note

Mobile first is a way of designing/developing for the smallest screens first and adding to the design rather than taking elements away.

<div class="container">  
  <h1>Hello, {{name || 'World'}}</h1>  
  <input type="text" [(ngModel)]="name">  
</div> 

If you resize your browser window, you should start to notice some of the responsiveness of the framework coming through and see it collapsing:

Now, I think it's a good idea to wrap this in what Bootstrap calls a Jumbotron (in previous versions of Bootstrap this was a hero unit). It'll make our headline stand out a lot more. We can do this by wrapping our H1 and input tags in a new div with the jumbotron class:

<div class="container"> 
  <div class="jumbotron"> 
    <h1>Hello, {{name || 'World'}}</h1> 
   <input type="text" ng-model="name"> 
  </div> 
</div> 

It's starting to look a lot better, but I'm not too happy about our content touching the top of the browser like that. We can make it look a lot nicer with a page header, but that input field still looks out of place to me.

First, let's sort out that page header:

<div class="container"> 
  <div class="page-header"> 
    <h2>Chapter 1 <small>Hello, World</small></h2> 
  </div> 
  <div class="jumbotron"> 
    <h1>Hello, {{name || 'World'}}</h1> 
    <input type="text" [(ng-model)]="name"> 
  </div> 
</div> 

I've included the chapter number and title here. The <small> tag within our <h2> tag gives us a nice differentiation between the chapter number and the title. The page-header class itself just gives us some additional margin and padding as well as a subtle border along the bottom.

The utmost thing I think we could improve upon is that input box. Bootstrap comes with some cool input styles so let's include those. First, we need to add the class of form-control to the text input. This will set the width to 100% and also bring out some beautiful styling such as rounded corners and glowing when we focus on the element:

<input type="text" [(ngModel)]="name" class="form-control"> 

Much better, but to me it looks a little small when you compare it with the heading. Bootstrap provides two additional classes we can include that will either make the element smaller or larger: form-control-lg and form-control-sm respectively. In our case, the form-control-lg class is the one we want, so go ahead and add that to the input.

<input type="text" [(ngModel)]="name"  
       class="form-control form-control-lg"> 

Tip

You can find the source code in the chapter_1/3.hello-bootstrap.