Why should you use Bootstrap?
What's new in Bootstrap 4?
The basic files and template required to start a project
Bootstrap is also a powerful prototyping tool in the start-up world. Often, you will want to vet an idea without investing tons of time into it. Bootstrap allows you to quickly build a prototype to prove out your idea without a large time commitment to build out a frontend that you might not end up using. Even better, if you're working in a team of developers, it is very likely everyone will be familiar with the framework. This will allow for code consistency from day one. No arguing over how to name the selectors or the best way to structure a CSS file. Most of the configuration is already set up for you and you can get on with creating your project faster.
With the release of Bootstrap 4, there are a number of key updates to the framework. One of the biggest changes is the move from Less, which is a CSS preprocessor, to Sass. When Bootstrap first started out, there was no clear favorite when it came to preprocessors. Over the last couple of years, Sass has gained a bit of an edge, so this switch should come as no surprise. If you haven't used Sass before, don't worry; it is similar to Less and really easy to learn. In later chapters, I will cover Sass in greater depth.
Another big new feature in version 4 is the improved grid system and the inclusion of flexbox. For the regular grid, another grid level has been added to better target mobile devices, and media queries have been reworked too. Flexbox is the grid of the future and it's really exciting that it's been included. By default, the regular grid will work out-of-the-box but you can switch to the flexbox grid by switching a simple Sass variable to take advantage of this new layout component.
Bootstrap 4 sees the deprecation of components such as wells, thumbnails, and panels, and the introduction of the new card component. This is a good thing for a couple of reasons. First of all, it removes a few components that were similar and replaces them with a single card component. This makes the framework a little lighter and easier to learn for the new user. The card component has also seen an increase in popularity lately, so it makes sense to include it here. All one has to do is to look at the popularity of Google's Material Design to see how cards are a great component to use in a web application.
One change that you might not notice immediately but is great nonetheless is the improvements to the built-in CSS reset. Bootstrap has taken
normalize.css and extended it with a new module called Reboot. Reboot improves on Normalize and tightens up the default browser styling that needs to be reset for all web-based projects.
I couldn't be happier to see that Bootstrap has dropped support for Internet Explorer 8 (IE8). The time has come to leave this browser in the past! If you need IE8 support, the recommendation is to continue using Bootstrap 3.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" integrity="sha384-y3tfxAZXuh4HwSYylfB+J125MxIs6mR5FOHamPBG064zB+AFeWH94NdvaCBm8qnd" crossorigin="anonymous">
If you would like to include the CSS file yourself, go to
and download the framework. Extract the resultant ZIP file and locate the
/css directory. Within this directory will be a number of CSS files. The only one you need to worry about is
bootstrap.min.css. Locate that file and copy it to the
/css directory of your own project. Once there, link it into the head of your document, which will look something like this:
<link rel="stylesheet" href="/path/to/your/file/bootstrap.min.css">
</body> tag. If you choose to use the CDN, insert the following lines of code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js" integrity="sha384-vZ2WRJMwsjRMW/8U7i6PWi6AlO1L79snBrmgiDpgIWJ82z8eA5lenwvxbMV1PAh7" crossorigin="anonymous"></script>
If you prefer to insert the files yourself, go back to the Bootstrap package you downloaded earlier and locate the
/js directory. There will be a few files here but the one you want is
bootstrap.min.js. You'll need to also head to http://jquery.com to download the jQuery framework file. Once you've done that, drop both files into the
/js directory for your own project. Next, enter the following lines of code at the bottom of your page template. Make sure jQuery is loaded before
bootstrap.min.js. This is critical; if you load them in the opposite order, the framework won't work properly:
<script src="/path/to/your/files/jquery.min.js"></script> <script src="/path/to/your/files/bootstrap.min.js"></script>
That concludes the explanation of the key Bootstrap framework files you need to include to get your project started. The next step will be to set up the basic starter template so you can begin coding your project.
The basic starter template is the bare bones of what you'll need to get a page going using Bootstrap. Let's start by reviewing the code for the entire template and then I'll break down each critical part:
<!DOCTYPE html> <html lang="en"> <head> <!-- Required meta tags always come first --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" integrity="sha384-y3tfxAZXuh4HwSYylfB+J125MxIs6mR5FOHamPBG064zB+AFeWH94NdvaCBm8qnd" crossorigin="anonymous"> </head> <body> <h1>Hello, world!</h1> <!-- jQuery first, then Bootstrap JS. --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js" integrity="sha384-vZ2WRJMwsjRMW/8U7i6PWi6AlO1L79snBrmgiDpgIWJ82z8eA5lenwvxbMV1PAh7" crossorigin="anonymous"></script> </body> </html>
Like most projects nowadays, Bootstrap uses the HTML5 DOCTYPEÂ for its template. That is represented by the following line of code:
Avoid using other DOCTYPES such as XHTML strict or transitional or unexpected issues will ariseÂ with your components and layouts.
Bootstrap is a mobile-first framework so the following meta tag needs to be included to allow for responsive web design. To make sure your project renders properly on all types of devices, you must include this meta tag in the
<head> of your project:
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
If you're interested in learning more about how responsive web design works in Bootstrap, you should check out the documentation at: http://v4-alpha.getbootstrap.com/layout/responsive-utilities/ .
That brings to a close the most important parts of the template that you need to be aware of. The remainder of the code in the starter template should be straightforward and easy to understand.
As I mentioned earlier, Bootstrap uses
normalize.css as the base CSS reset. With the addition of the Reboot reset, Bootstrap extends Normalize and allows for styling to only be done using CSS classes. This is a much safer pattern to follow, as it's much easier to deal with CSS specificity if you are NOT using CSS IDs for styling purposes. The CSS reset code is baked right into
bootstrap.min.css so there is no need to include any further CSS files for the reset.
Although we have our template set up, one of the main problems with static websites is when things change. If your project grew to 50, 100, or 500 pages and you wanted to possibly update to a new version of Bootstrap, you might be looking at having to update all of those files. This is extremely painful, to put it mildly. Now we enter static site generators.
One of the hottest trends right now in web development is the use of static site generators. What exactly does that mean? Instead of having several static files that require updating every time something changes globally, you can use a series of base templates then load your body content into them. This is sometimes called includes or partials. This way, you only have one or two layout files that include the header and footer code.
Why don't we integrate the basic template into a generator so I can show you what I'm talking about? My generator of choice is called Harp.js and you can install it over at http://harpjs.com/.
Before we get too far ahead of ourselves, we need to install Node.js. Harp runs off Node.js so it's a dependency you'll need to use. If this feels too advanced for you, feel free to skip ahead to Chapter 2 , Using Bootstrap Build Tools. This section is totally optional. Head to the following URL to install Node.js if you don't already have it on your computer: https://nodejs.org/download/ .
Follow the instructions on the Node.js website and, once you've finished installing it, run the following command in a command-line application such as Terminal or Cygwin:
$ node -v
This should spit out the version number of Node.js that you just installed and will also confirm that the installation worked. You should see something like this:
Perfect, now let's move on to installing Harp.
If you closed your command-line app, open it back up. If you are on a Mac, run the following command to install Harp:
$ sudo npm install -g harp
If you happen to be on a Windows machine, you'll need to enter a slightly different command, which is as follows:
$ npm install -g harp
After the installation completes, run the following command to get the Harp version number, which will also confirm that the installation was successful:
$ harp version
I should also mention that most static site generators will also have built-in CSS preprocessors. This avoids you having to compile your Sass code somewhere else when working on your project. Harp includes both Sass and Less, so this will save you some time in upcomingÂ chapters when we cover Sass in more detail.
Before we convert our template to a Harp template, we need to set up the project structure. Create a new folder on your computer for the project then create the following subdirectories:
img(if you plan on using images)
If you're storing the CSS files locally, copy
bootstrap.min.css from your original project and add that into the new
/css folder. In a future chapter, I'll show you how to code a custom Bootstrap theme. That file would also be included within this directory.
bootstrap.min.js locally, then copy them into the new
/img directory is optional and only applies if you plan to use images in your project. Ignore the
/partial directory for now and I'll cover that a bit later. In the
/fonts directory, you should drop in the Glyphicons icon set that comes with Bootstrap. If you downloaded Bootstrap, go back to the downloaded package and locate the font files. Copy them into this directory. Now that we have the project structure set up, we can start to break the basic page template down into a few different pieces.
In the root of your new Harp project, create a new file called
Locate the following line in the template and cut and paste it into a new file. Save the file and call it
Return to the layout file and insert the following line of code immediately after the
<%- yield %>
Save both files then let me explain what is happening. The yield tag is a variable. Any page template such as
index.ejsthat lives in the same directory as the layout will be loaded in wherever you place the yield in the layout. So the
Hello, world!line we inserted in the
index.ejsfile will load in here once you compile and launch your project.
Are you starting to see the advantage to this method? You could then go on and create other page templates so that all use this layout. In the future, if you need to make a change to the
<head> of the layout, you only have to edit the one template file and it will be compiled into all of your final HTML files.
Now that the template files are ready, we need to compile the project before we can preview it in the browser. Head back to your command-line app and make sure you are in the root of your project directory. Once there, run the following command to compile the project:
$ harp compile
Assuming you see no errors, your project was compiled successfully and can now be previewed in the browser. Before we move onto that step, though, take a look at your project directory and you'll see a
/www directory to your web server. More on deployment shortly.
Harp has a built-in node web server that you can use to preview your project locally before deploying it. Open up your command-line app and run the following command from the root of your Harp project:
$ harp server
After doing so, you should see a message in the Terminal telling you that the server is successfully running. Open a web browser and navigate to the following URL to preview your project:
Your project will load up in the browser and you should see the
Hello, world! message that was inserted on compile. This is only a fraction of what you can do with Harp. To learn more about how Harp works, visit their website at
If you're looking for a simple way to quickly deploy your project for testing, there is a tool called Surge from the same people that developed Harp. Surge is a free deployment solution; visit their website to learn more at http://surge.sh/.
To install Surge, you'll need to open up your Terminal again. Once you have done this, run the following command:
$ npm install --global surge
This will install Surge and make it available anywhere on your computer.
To deploy your new project, navigate back to the root directory in the Terminal then run the following command:
You'll now be prompted to log in or create a new account. Surge is free but you need to register an account to use it. You'll also notice in the Terminal that there is an autogenerated URL. This is the URL you can use to view your project live on the Internet. Once you've finished registering or logging in, visit the URL in your browser. If all went well, you should see the basic hello world page live.
Surge is a great tool if you're looking for a quick way to test your project on a live web server. If all goes well, you can then manually deploy your project to your own web server. Surge does offer a paid plan allowing for the use of a custom domain. So you could actually use it for your production deployment if that seems like a good idea.
That brings the first chapter to a close. I hope this chapter has proved to be a good introduction to Bootstrap 4 and provided you with a few advanced techniques for setting up your Bootstrap projects. In the next chapter, we'll take what we've learned here a step further by covering Bootstrap build tools. This will include a deeper explanation of how to use Harp, as well as other tools that are commonly used with Bootstrap.