Showing posts with label Bootstrap. Show all posts
Showing posts with label Bootstrap. Show all posts

April 21, 2017

Bootstrap Tricks And Tips

Bootstrap is full of incredible features but can seem too complex for many of us. You simply cannot remember all the functions or cannot take one week off to study its documentation. Many times even experienced users get surprised when they discover some of its hidden gems.

I have carefully chosen some of the best tips and tricks that I have used in my Bootstrap coding career so far and I would like to share them with you.

For a better reading experience, I divided them into 4 groups.

If you would have suggestions for some more tricks, share them with me in comments.




Bootstrap Components

navbar, footer

How to open a navbar dropdown on hover

A standard behaviour of Bootstrap dropdowns is that they open on click. It has its pros and cons and I usually keep it as default. If you would like to open dropdowns on hover, it is not a complicated process to achieve it.

We will need to change two things and we will apply our changes only if the viewport's wider than 768px (i.e. navbar is not collapsed).

First, add this CSS rule to your stylesheet after loading the Bootstrap's CSS. It is quite straightforward - if the viewport's wider than 768px and you hover above a .dropdown link, a .dropdown-menu opens.

Second, if the parent link should point to an URL too, we will need  to add a bit of JS code to change the .dropdown links' onclick behaviour. 

How to change navbar height

When you want to change the navbar height, you will need to adjust more things than simply adding a new height value for .navbar. 

In the following code, I outline an approach how to change your navbar's height to 80px. 

How to create sticky footer

Create a sticky footer for your website following these steps:

1. Create a footer element and set its position to absolute with bottom offset 0. 2. Set a fixed height for it. (In my example, it will be 80px).
3. Add bottom padding to your element, set it to the same value as the footer's height.

Bootstrap Grid

How to make columns same height

This is a classical problem. You have content boxes with different content but you want them to have the same height. A solution to this problem will be a smart usage of flexbox on the Bootstrap rows.

This approach consists of these steps:

1. Create a .row-flex and apply it to your content boxes' parent row.
2. Columns in the .row-flex row will have same height now.
3. Usually do not mix Bootstrap with my components and I have all the           backgrounds, padding, etc. of the content boxes declared in its child element -    .content. To make everything work, all you need to do is just to set a height: 100% to the .content boxes.

How to add vertical spacing to columns

To add some vertical spacing to your columns easily, use the following simple CSS rule that gives bottom margin of 30px to every Bootstrap column. 

How to use your own classes instead of columns and rows

Many people simply do not like the Bootstrap way of writing the code with columns and rows such as:

To achieve it, you will need to use Bootstrap {LESS} .make-row() and .make-*-column() mixins.

How to change ordering of columns on mobile

A quite useful feature of the Bootstrap grid is an ability to order columns differently on mobile devices and differently on desktops. 

All you need to do is to use .col-(breakpoint)-push-(number) and .col-(breakpoint)-pull-(number) classes to push or pull the columns on the specified breakpoint out of its original place.

I know it sounds a bit complicated and usually, it takes me some time to visualise the outcome but let's have a look at it in an example. In the following code, the first column will appear as a first item on mobiles but as a second item on tablets and desktops.

In the following code, the first column will appear as a first item on mobiles but as a second item on tablets and desktops.

How to show or hide elements on mobile

If you need to quickly and easily hide an element only on xs devices, there is a .hidden-xs class that you can use. 

Similarly, you can use a .hidden-(breakpoint) class for the rest of the breakpoints too and combine them together, i.e. use classes .hidden-lg, .hidden-md, .hidden-sm.

On the other hand, if you want to show an element only on certain devices, you can use .visible-(breakpoint)-(display) classes. Note a slight difference there - you have to use a display property there too. Possible values for the (display) part of the class name are block, inline-block and inline. So, if you need to display an element as a block on large devices, just add a .visible-lg-block class to it.

How to disable responsiveness

There can be situations when you would prefer your page to behave as a non-responsive. These could be when preparing your web page for print or generating output for PDF.

Basic steps to disable responsiveness:

Omit a
Set a fix width for your .container. E.g., .container {width: 1000px !important;}