Bootstrap tips and tricks
Reading time
10
Writer
David Johnson
Published date
Category
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 grid
How to make columns same height
How to add vertical spacing to columns
How to use your own classes instead of columns and rows
How to change ordering of columns on mobile
How to show or hide elements on mobile
How to disable responsiveness
Components - navbar, footer
How to open a navbar dropdown on hover
How to change navbar height
How to create sticky footer
Buttons and forms
How to create custom Bootstrap buttons
How to disable buttons and other form elements
Typography and layout tips
How to center a block element
How to center an inline content
How to use heading classes
How to responsively embed YouTube or Vimeo videos
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:
Create a
.row-flex
and apply it to your content boxes' parent row.Columns in the
.row-flex
row will have same height now.I 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 aheight: 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:
But you can easily write your code like in the next example.
To achieve it, you will need to use Bootstrap {LESS} .make-row()
and .make-*-column()
mixins.
More about the grid mixins in the Bootstrap official docs.
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.
To read more about this, consult the official documentation.
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
<meta name="viewport" ...>
Set a fix width for your .container. E.g.,
.container {width: 1000px !important;}
If more info needed on this, check out the official documentation.
Components - navbar, footer
How to open a navbar dropdown on hover
The 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.
See it live or experiment if you like on Codepen.
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 on how to change your navbar's height to 80px. You can find more info on this topic in my Bootstrap navbar tutorial.
How to create a sticky footer
Create a sticky footer for your website following these steps:
Create a footer element and set its position to absolute with bottom offset 0. Set a fixed height for it. (In my example, it will be 80px).
Add bottom padding to your <body> element, set it to the same value as the footer's height.
Have a look at a result or experiment with your own content in this Codepen.
Buttons and forms
How to create custom Bootstrap buttons
Bootstrap comes with some 5 default button styles - default, primary, success, danger, etc.
When you need to change a general button feeling, as to decrease its border-radius or padding, the best way is to overwrite the .btn
class.
Other than that, I would suggest to create a new button type and extend the common Bootstrap button style.
As an example, let's create an outline button and a dark blue button.
I will create 2 new classes:
.btn-outline
and.btn-colour-1
. These classes will extend the default.btn
styling.In CSS, I will style their default state and also
:active
,:focus
,:hover
and .active
(all these share the same style)When done, I will be ready to use a new button, e.g.
<a class="btn btn-outline">New button</a>
How to disable buttons and other form elements
You can easily disable buttons and inputs in your form. When disabled, the buttons will look lighter and will have a not-allowed cursor on hover.
For disabling radios and checkboxes:
add disabled class to a parent
.checkbox
or.radio element
add disabled attribute to the input
For disabling buttons
add disabled attribute to <button> buttons
or add .disabled class to <a> buttons
Typography and layout tips
How to center a block element
To easily horizontally center a block element, just add center-block class to it, as in <div class="center-block"></div>
.
This class adds the following CSS styles to your element:
How to center an inline content
If you need quickly center inline content or inline-block elements inside a div, add .text-center
class to the parent element.
Let's quickly horizontally centre text and button with the following code:
How to use heading classes
You often face a problem when you need to have a visually small heading but it should be h2. To mimic the appearance of h4, just add .h4
to your h2
. See more in the following example:
See more in the following example:
How to responsively embed YouTube or Vimeo videos
You can easily embed YouTube or Vimeo videos using Bootstrap's embed-responsive
helper classes.
Choose from embed-responsive-16by9
or embed-responsive-4by3
based on your video aspect ration.
Popular Templates
Meticulously crafted by pro designers and developers, so you get top-tier UI/UX and launch-ready functionality from the get-go.