Reader small image

You're reading from  Bootstrap 4 Cookbook

Product typeBook
Published inJun 2017
Reading LevelBeginner
PublisherPackt
ISBN-139781785889295
Edition1st Edition
Languages
Right arrow
Author (1)
Ajdin Imsirovic
Ajdin Imsirovic
author image
Ajdin Imsirovic

Ajdin Imsirovic is a full-stack web developer who has published several courses on the subject of web design and web development. He has also authored three books, Bootstrap 4 Cookbook, Elm Web Development, and Vue.js Quick Start Guide. In his fourth book, Vue CLI 3 Quick Start Guide, he introduces readers to the rich ecosystem of frontend tooling and best practices.
Read more about Ajdin Imsirovic

Right arrow

Chapter 8. Bootstrap 4 Flexbox and Layouts

In this chapter, we will cover the following topics:

  • Breakpoint-dependent switching of flex direction on card components
  • Letting cards take up space with the .flex-wrap and .col classes
  • Adding any number of columns with Flexbox
  • Combining numbered .col classes with plain .col classes
  • Working with the card component and the Flexbox grid
  • Center-aligning cards on wider viewports only
  • Positioning nav-tabs with Flexbox

Introduction


In this chapter, we will take a look at the brand new Flexbox--enabled grid in Bootstrap. We will also look at practical examples of using the Flexbox grid and the ways in which this CSS specification allows us to make layouts in an alternative way, by stepping away from float-based layouts. By including Flexbox in its new version, Bootstrap 4 really brings a whole new way to build websites. We will look at some of the new approaches in this chapter.

Breakpoint-dependent switching of flex direction on card components


In this recipe, we will ease into using the flexbox grid in Bootstrap 4 with a simple example of switching the flex-direction property. To achieve this effect, we will use a few helper classes to enable the use of Flexbox in our recipe. To get acquainted with the way Flexbox works in Bootstrap, check out the official documentation at https://v4-alpha.getbootstrap.com/utilities/flexbox/ .

Getting ready

To get started with the recipe, let's first get an idea of what we will make. Navigate to chapter8complete/app/ and run harp server. Then, preview the completed recipe at localhost:9000/recipe08-01. You should see a simple layout with four card components lined up horizontally.

Now, resize the browser, either by changing the browser's window width or by pressing F12 (which will open developer tools and allow you to narrow down the viewport by adjusting the size of developer tools). At a certain breakpoint (md, i.e. the medium...

Letting cards take up space with the .flex-wrap and .col classes


In this recipe, we will look at the power of applying helper classes to flex containers in combination with using the Flexbox grid on the flex items, in order to create a fully responsive Flexbox grid of Bootstrap components. This grid is very versatile and allows us to create wonderful responsive designs on the fly. Bootstrap now delivers even more on its promise of the fast layout of web pages with the simple use of predefined classes in its HTML markup. In this recipe, we will see how easy it is to set up a Flexbox-enabled layout.

Getting ready

To get started with the recipe, we will first inspect a completed recipe. Navigate to chapter8complete/app/ and run harp server. Then, preview the completed recipe at localhost:9000/recipe08-02. You should see a simple layout with eight card components, lined up in rows as per the available screen size.

If you resize the browser, you will be able to see that the cards always take up...

Adding any number of columns with Flexbox


In this recipe, we will see how to add an arbitrary number of columns with Flexbox. This means we no longer have to stick to the 12-column grid. This simple recipe will show the fundamental paradigm shift that the Flexbox model brings to front-end development.

Getting ready

Before you start with this recipe, preview the completed page by running the harp server command inside the chapter8/completed/app folder. To see the recipe itself, point your browser to localhost:9000/recipe08-03. Try resizing the browser to see the behavior of the completed Flexbox grid.

How to do it…

  1. Open the file titled recipe08-03.ejs add the following code in it:
<style>
 .col {
 flex-grow: 0;
 }
</style>
<div class="container">
 <h2 class="mt-5">Recipe 08-03: Adding Any Number of Columns with Flexbox</h2>
 <p class="mt-4">In this recipe we will look at using the flexbox grid to create layouts that were not possible in earlier versions of...

Combining numbered .col classes with plain .col classes


In this recipe, we will combine plain col classes with numbered col classes. This will allow us to create some wonderful combinations of columns that were previously not possible in Bootstrap. Even though this recipe is quite simple, having a firm understanding of these basic concepts will help us with more advanced recipes later in the chapter.

Getting ready

To begin the recipe, preview the completed one by navigating to chapter8/complete/app. Inside the folder, run the harp server commands from your console. Next, open the browser at localhost:9000/recipe08-04. You should see a row with three columns, a simple demonstration of important concepts we will discuss in this recipe.

How to do it…

  1. Open the folder with the starter code at chapter8/start/app.
  2. Open the file titled recipe08-04.ejs and add the code as follows:
<div class="container">
 <h2 class="mt-5">Recipe 08-04: Combining Numbered <code>col</code> Classes...

Working with card layouts and the Flexbox grid


In this recipe, we will make layouts using the cards component with the help of Bootstrap 4 Flexbox grid to align them and look at some new CSS properties that will make the use of images in our cards easier by retaining their proper aspect ratio and preventing image stretching. We will also look at using lorempixel.com to create more believable mock-ups of our layouts.

Getting ready

To begin, open the completed page in your browser by navigating to the chapter8/complete/app folder and running the harp server command through the console. Point your browser to the completed recipe by visiting localhost:9000/chapter08-05. Observe the changes in the layout of the cards, components on different viewport resolutions.

How to do it…

  1. Open the chapter8/start folder. Inside that folder, open the app folder, and then open the file titled recipe08-05.ejs and paste in the code that follows:
<div class="container">
 <h2 class="mt-5 pb-3">Recipe 08...

Center-aligning cards on wider viewports only


In this recipe, we will look at how we can center align cards' content. We will have varying amounts of content in cards inside the same row. We will see how easy it is to align this content with the use of Flexbox.

Getting ready

To get ready for this recipe, we will first preview the end result that we are trying to achieve. In your console, locate the chapter8/complete/app folder run the harp server command on it. Preview the completed recipe by visiting localhost:9000/recipe08-06 in your browser.

How to do it…

  1. Open the folder titled chapter8/start/app, search the file named recipe08-06.ejs and paste in the following code:
<div class="container">
 <h2 class="mt-5 pb-3">Recipe 08-06: Center-Aligning Cards on Wider Viewports Only</h2>
 <p class="mt-4">In this recipe we will look at centering the card content with flexbox.</p>
</div>
<!-- /.container -->
<style>
 .col {
 min-width: 280px;
 }
 .card img...

Positioning nav-tabs with Flexbox


In this recipe, we will look into using Flexbox utility classes to easily align nav-tabs. Doing this is a simple task. However, to extend the recipe and make it more useful, we have also included a lot of content in each of the nav-tabs. Thus, in this recipe, besides demonstrating how to align nav-tabs, we will look into splitting a complex layout into several partials, dealing with different heights on tabs, and using a few simple CSS declarations to modify the look of our nav-tab items.

Getting ready

To preview the completed recipe, navigate to chapter8/complete/app through your console. Run the harp server command. Open your browser and visit localhost:9000/recipe08-07. Click on different tabs and try resizing the viewport. Look at the way that tab content responds to changes in viewport size.

How to do it…

  1. In the chapter8/start/app folder, open the file titled recipe08-07.ejs and paste the following code in it:
<%- partial("partial/_0807-01-recipe-intro...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Bootstrap 4 Cookbook
Published in: Jun 2017Publisher: PacktISBN-13: 9781785889295
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Ajdin Imsirovic

Ajdin Imsirovic is a full-stack web developer who has published several courses on the subject of web design and web development. He has also authored three books, Bootstrap 4 Cookbook, Elm Web Development, and Vue.js Quick Start Guide. In his fourth book, Vue CLI 3 Quick Start Guide, he introduces readers to the rich ecosystem of frontend tooling and best practices.
Read more about Ajdin Imsirovic