WordPress Development: Properly load your Javascript and set dependency

Theme and Plugin developers should wp_enqueue_script() to load the Javascript sources used in their code. Why should you enqueue? In the first place you should prevent conflict between your scripts. Secondly you should only load the resources you need per page. Load the right scripts only will reduce your load time.

WordPress offers a list of bundled resources like jQuery. So you don’t have to load jQuery every time, but you will have to set the dependency of jQuery of the sources you add.

Enqueueing you scripts proper will also help other theme and plugin developers. When two plugin need the same Javascript library you will load it only once. You will have to realize this in not guaranteed and documented well. If two plugins for example need some plugins of Twitter’s Bootstrap there will be no rules to describe the source already loaded. A solution can be both plugins depends on a third plugin which loads the resources. This kind of dependency is outside the scope of this blog post.

Although the reasons for proper loading seems clear many plugins and themes haven’t enqueued all their code.

Why do developers skip wp_enqueue_script()?

The main reason i found was the use of dynamic variables. Enqueued scripts load before the theme or plugin functions have been parsed in many situations. For example you will set the width of an element by javascript where the width will be user defined. The easiest but wrong solution will be to write it direct to source. Write javascript to source and for example make use of jQuery’s document ready will create a dependency of jQuery.

Solutions to use dynamic variables with wp_enqueue_script:

The first solution will be to write your code in native javascript not dependent of any library.
The second, and preferred solution in my opinion, is to (mis)use the wp_localize_script() function. wp_localize_script() localizes a script, but only if script has already been added. Can also be used to include arbitrary Javascript data in a page. The latest can be used to set your dynamic variables. Also read: Use wp_localize_script, It Is Awesome
Some example:

/** set parameters for display **/

$options = array();
$options[‘color’] = get_option( ‘color’, ‘blue’ );

wp_register_script( ‘color_display’, plugin_dir_url( __FILE__).’display_color.js’, array( ‘jquery’ ), ‘20131108’ );
wp_enqueue_script( ‘color_display’ );
wp_localize_script(‘color_display’, ‘color_display_options’,$options)

With display_color.js:

jQuery(document).ready(function($) {
$('colored').css('background-color',color_display_options.color);
};

Defer loading plugin

Recent i wrote a plugin for Defer loading. This plugin requires scripts are enqueued proper. Testing this i found many issues with different

plugins:

Custom Navbars in Twitter’s Bootstrap 3

Update: Also read Adding a background gradient to Bootstrap’s navbar with Less

Everyone knows the navbars for navigation from Twitter’s Bootstrap 2. In version 3 the structure has been changed a little, also see Twitter’s Bootstrap 3 RC2 (important changes). The navbar intends to be the main navigation on a website. When using more navbars on the same page you will have to set the z-index manually. Twitter’s Bootstrap 3 add accessibility to the navbar by adding a direct link with a special sr-only class. This sr-only class is visible in screen readers only.

Twitter’s Bootstrap 3 provides four types of navbars. The default navbar, a static navbar on the top of the document and navbar fixed to top or bottom of the document. See Navbar Docs for some example code to add a nav-tag with navbar to your website. To style your navbar you need to add the navbar-default class always. Of cource you could use a custom class or the navbar-inverse class too.

The best way to create a custom navbar (class) will be changing the Less variables and (re)compile your own version of Bootstrap’s CSS. See also: Change navbar color in Twitter Bootstrap 3

The look of the (default) navbar has been changed with Twitter’s Bootstrap 3. Gradients of the background are dropped also the dividers between links seems to miss.
Default Navbar with dividers in Twitter’s Bootstrap 2.x:
Twitter's Bootstrap 2.x navbar with dividers

Default Navbar in Twitter’s Bootstrap 3:

Twitter's Bootstrap 3 default navbar

I also create a custom Navbar generator for those who don’t want to compile the CSS. This generator has the option to add a background gradient and borders to your navbar. The borders can be used instead of the former dividers.

Navbar generated with the navbar generator:
Navbar Twitter Boosstrap 3

An other example:
Purple navbar

Twitter’s Bootstrap 3 RC2 (important changes)

The release candidate 2 of Twitter’s Bootstrap 3 shows some important changes. I decide to write a separate blog for it. Before start reading read first to get an idea of the changes between Twitter’s Bootstrap 3 and Twitter’s Bootstrap 2.

Grid

RC2 shows new grid class prefixes: col-xs-* for mobile devices follow by col-sm-* (tablet), col-md-* (laptop) and col-lg-* (desktop). The grid is fluid by default still. Gutter between the columns are replaced with a padding on both sides of the row. See the table below for a total view:

Twitter's Bootstrap Grid
Twitter’s Bootstrap Grid

I will show you an example of the use of the grid. Consider this example html:


<div class="container">
<div class="row">
<div class="col-xs-6" style="background-color: pink;">Pink</div>
<div class="col-xs-6" style="background-color: purple;">Purple</div>
</div>
<div class="row">
<div class="col-sm-6" style="background-color: pink;">Pink</div>
<div class="col-sm-6" style="background-color: purple;">Purple</div>
</div>
<div class="row">
<div class="col-sm-6 col-md-4" style="background-color: pink;">Pink</div>
<div class="col-sm-6 col-md-8" style="background-color: purple;">Purple</div>
</div>
<div class="row">
<div class="col-sm-6 col-md-4 col-lg-2" style="background-color: pink;">Pink</div>
<div class="col-sm-6 col-md-8 col-lg-10" style="background-color: purple;">Purple</div>
</div>
</div>

On large desktops (screenwidth >=1200px) this will look like:

1200

Note the `col-lg-10` are applied on the fourth row.

Laptops >= 992px will look like (grid-md-* will be applied on the third and fourth row):

980

Tabets >= 768px:

768 On the first row col-xs-6 has been applied. The next three rows are split with col-sm-6.

<768px:

extra small grid for mobile phones

All rows stack except from the first. The first row got col-xs- for the extra small grid which never stack.

So the col-xs-* will never stack as you see. col-xs-* will always be applied (make the grid horizontal) while the other classes are applied above their breakpoint. Also see the difference between col-xs-* and col-sm-* in the mobile view. Beside the break-points all grids have a max-container size too. To build your templates you could use this default grid, but variations between breakpoints are possible also.

The Jamedo’s Bootstrap Start Theme use a max-container size of 1200 pixels in combination with the Small grid by default. To build a non responsive site TB advice too use the Extra Small Grid with a fixed contianer size, see http://getbootstrap.com/getting-started/#disable-responsive. Read more about non responsive sites with TB3: .

Also realize there is no breakpoint at 480px at the moment. This breakpoint can be used for different views for phones landscape and portrait. See also: Missing the col-ts-* (tiny to small) set of classes?

Althought the gutters are replaced with padding the Less files and the current version of the customizer still define a @grid-gutter-width. This value is used to calculate inner padding of the columns which is @grid-gutter-width/2 = 15 pixels on each side. The margins on the left and the right side of the grid are filled with a negative margin of @grid-gutter-width/2 px too.

Classes for offsets, pull and push are availiable for all grids except the Extra Small grid. The classes have names like .col-sm-offset-* and .col-lg-pull-*.

Other changes in RC2

Glyphicons are back. Twitter’s Bootstrap 3 includes 180 glyphs in font format from the Glyphicon Halflings set. Free of cost and so they ask for a backlink to Glyphicons.com.

Stucture of the navbar has been changed a little. TB3 provide four types of navbars: The default navbar, Static top navbar and fixed navbar top and bottom. See the examples on the Get started page.

Try to check the migration section in the docs to explorer the complete set of changes.

Twitter’s Bootstrap 3 also pays attention to accessibility. They intoduce a new class `sr-only` only visible for screen readers (See: http://a11yproject.com/posts/how-to-hide-content/). You can use it to build links to the main content on the top of your document of navbar like:

<body>
  <a href="#content" class="sr-only">Skip to content</a>
  <div class="container" id="content">
    The main page content.
  </div>
</body>

They also ask your attention for the logical structure of your header elements (<h1> ... <h6>). Default Bootstrap font sizes help you to make your visual (formatted) style consistent with the semantic structure. Screen readers use this semantic stucture to build a table of contents of your document. Consider to not change the font sizes by css but choose the subordinate heading element which fits the semantic structure too. See also: Headings and Subheadings.

New elements elements make it possible to construct b.e. a Listgroup with badges:

Listgroup with badges

Last but not least provides RC2 an example of a Bootstrap Theme constructed in Less: http://getbootstrap.com/examples/theme/

It’s a shame! The newest version of Github’s most popular project is not backward compatible

Yes, i’m talking about Twitter’s Bootstrap. Release Candidate 2 of version 3 of Bootstrap 3 has been released today. And we will find at least class name changes again, see: What is wrong with Bootstrap 3 RC2. Is there a reason to be not backward compatible? No. Or maybe…. you will need some extra CSS code which could slow down your site. Unless you didn’t use the responsive features of version 2.x. Twitter’s Bootstrap 3 can’t be used without the responsive code any more.
The real reason to be not backwards compatible will be the new mobile first approach, i think. To adopt this approach you will have to change the structure of your CSS loading. You will have to work from small to big. I wonder what the benefits of the mobile first approach are nowadays. I understand some older smart phones don’t handle all css3 features or not even Javascript. Of course it would be nice when all this old phone users will see a nice looking and well functioning website.
But who has such old phone and why? Most people buy the newest phone every week. Maybe not every week but certainly often.
I also bet most web designers never check their site on a ‘old’ phone. They will load the site on their newest i-Phone via broadband wifi and say: Wooh that looks cool.
Maybe this the problem with Twitter’s Bootstrap too. It’s cool.
Should we rename everything? Why not? Everybody will happy with this new stuff, they don’t care.

If the mobile first approach really reduce load time / data traffic mobile users will profit a little. But Mobile Internet will become faster and faster and this happens sooner and sooner.

Should you migrate your website from Twitter’s Bootstrap 2.x to 3?

I don’t think so. With Twitter’s Bootstrap you already have chosen a progressive technology. Your website will be responsive and fit mobile users needs too. New browsers and techniques won’t work with 2.x in future. But also consider the mean life time of a / your web design. Twitter’s Bootstrap drops support for IE7 and FF3 (check the facts here: IE10 blows past IE7 and IE6′s combined market share, Firefox gains too, but Chrome hits 21-month low ). Beside browser support the typehead plugin is also dropped.

Should you build your new website with Twitter’s Bootstrap 3?

You could consider it at least (after the final release). Read also: Writing Twitter’s Bootstrap with upgrading to v3 in mind. If you used 2.x before you should learn some new things.
Except from the backwards compatible their seems nothing wrong with the new 3 version. The new small grid will make happy the modern phone users. The grid is fluid by default now, so you will have to study the profits and disadvantages of this when you used to the fixed layout.

What are the alternatives? Personal i love the idea behind (css) frameworks with a grid. I should never consider to write all the media queries, browser compatible css. etc. myself again. Zurb’s Foundation can be an alternative. (Is Bootstrap 3 RC 1 ready for production?).

New techniques / insights decouple CSS from HTML. This will make future migrations less painful maybe. So also read this post: Migrating from Zurb Foundation / Twitter Bootstrap to Bourbon & Neat. It refers to Bourdon and Neat. As they say: “it relies entirely on Sass mixins and does not pollute your HTML”. This combination looks promising too.

Migrate your (former) Skematik theme framework to Twitter’s Bootstrap 3: Instructions for theme developers

At Jamedo Websites we work with the Skematik Theme(work) to build responsive websites. Unfortunately the website was down for a while and Skematik use Twitters Bootstrap 2 only. Cause we need a new version of Skematik i start to migrate it to TB3. You will find this new version on: https://github.com/bassjobsen/jamedo-bootstrap-start-theme.

To use the theme work to build your own child theme use the instruction below:

  1. download from: https://github.com/bassjobsen/jamedo-bootstrap-start-theme/archive/master.zip
  2. copy the content of this .zip to the folder skematik in the wp-content/themes folder (the zip file creates the folder “https://github.com/bassjobsen/jamedo-bootstrap-start-theme/archive/master.zip” you have to rename this to “skematik” )
  3. (search and) replace all “span*” classes in your theme files with “col-lg-*”, see also: http://stackoverflow.com/a/17890898/1596547
  4.  fix all other points (general issues should be send by email to bass@w3masters.nl or send as a pull request to https://github.com/bassjobsen/jamedo-bootstrap-start-theme )

Basic setup for your child themes:

Read: http://codex.wordpress.org/Child_Themes#How_to_Create_a_Child_Theme. So basically create style.css in your child theme folder. Start this file with: @import url("../skematik/style.css");

Learn more about Bootstrap 3

Bootstrap 3 don’t have a separate responsive CSS file any more.

From now Twitter’s Bootstrap defines three grids: Tiny grid for Phones (<480px), Small grid for Tablets (<768px) and the Medium-large grid for Desktops (>768px). The row class prefixes for these grid are “.col-”, “.col-sm-” and “.col-lg-”. The Medium-large grid will stack below 768 pixels screen width. So does the Small grid below 480 pixels and the tiny grid never stacks.

For this reason the “span*” classes in your theme files are replaced with “col-lg-*” in step 3 above. With “col-lg-*” the grid become horizontal above a screen width of 992px (desktop). Below this screen width elements will stack. To start the stacking at 767px use the “col-sm-*” prefixes instead of “.col-lg-”. Read Twitter Bootstrap 3 breakpoints and grid for more information.

Read Bootstrap your (designer’s) mind  also and ask your designer to deliver different designs for screen widths (from low to high). This will help you to choose the right grid settings.

For other changes read: Migrate your templates from Twitter Bootstrap 2.x to Twitter Bootstrap 3 and http://bootply.com/bootstrap-3-migration-guide.

You will find the official documentation of Twitter’s Bootstrap 3 now on: http://getbootstrap.com/.

Compile Twitter’s Bootstrap 3 without responsive features

Bootstrap 3 will be mobile first. No more separate responsive CSS file. Nice, but what if you don’t need / want the responsive features for some reason? So i try to compile Compile Twitter’s Bootstrap 3 without responsive features.

First i made a list of all less files writing media queries:

./print.less:@media print {
./modals.less:@media screen and (min-width: @screen-tablet) {
./carousel.less:@media screen and (min-width: @screen-tablet) {
./responsive-utilities.less:@media screen and (max-width: 400px) {
./responsive-utilities.less:@media (min-width: @screen-tablet) and (max-width: @screen-tablet-max) {
./responsive-utilities.less:@media (min-width: @screen-desktop) {
./responsive-utilities.less:@media print {
./jumbotron.less: @media screen and (min-width: @screen-tablet) {
./grid.less:@media (min-width: @screen-tablet) {
./grid.less:@media (min-width: @screen-desktop) {
./grid.less:@media (min-width: @screen-large-desktop) {
./navbar.less:@media screen and (min-width: @grid-float-breakpoint) {
./mixins.less: @media
./mixins.less: @media (min-width: @screen-small) {
./mixins.less: @media (min-width: @grid-float-breakpoint) {
./mixins.less: @media (min-width: @grid-float-breakpoint) {
./mixins.less: @media (min-width: @grid-float-breakpoint) {
./mixins.less: @media (min-width: @grid-float-breakpoint) {
./forms.less:@media (min-width: 768px) {

The basic idea will be drop the breakpoint and disable all grids except from the 960px (@screen-desktop) one.

I don’t need responsive-utilities.less so i remove it from bootstrap.less. Why does it file contains a “@media print” media query?

// Print utilities
.visible-print {
.responsive-invisibility();
}
.hidden-print { }

@media print {
.visible-print {
.responsive-visibility();
}
.hidden-print {
.responsive-invisibility();
}
}

See also: https://github.com/twitter/bootstrap/issues/8460

I should expect this in print.less, but for this reason i can’t remove responsive-visibility/responsive-invisibility. You will find this functions in mixins.less. (see: )
I also drop jumbotron.less. Why should i need this at all?

After this i look to: “./modals.less:@media screen and (min-width: @screen-tablet)”, “./carousel.less:@media screen and (min-width: @screen-tablet)”, “./navbar.less:@media screen and (min-width: @grid-float-breakpoint)”, “./forms.less:@media (min-width: 768px)”. The hardcoded 768px is a mistake i think, see also: https://github.com/twitter/bootstrap/pull/8454.

@grid-float-breakpoint will defined in variables.less as “@grid-float-breakpoint: @screen-tablet;”. On the first sight i should think this should be: “./modals.less: @media screen and (min-width: @grid-float-breakpoint)”, “./carousel.less:@media screen and (min-width: @grid-float-breakpoint)”, “./navbar.less:@media screen and (min-width: @grid-float-breakpoint)”, “./forms.less:@media (min-width: @grid-float-breakpoint)”. Cause this setting depend on a breakpoint which should be independent of the grid sizes (@screen-tablet). see: https://github.com/twitter/bootstrap/issues/8459

To compile my first non responsive version i have set them all to @grid-float-breakpoint with @grid-float-breakpoint : 0px. This works well for the forms where this breakpoint sets the right alignment for the form labels. Now the alignment is always right which could be okay for a non responsive site. For the dialog the effect will a fixed width of 560 pixels in stead of a 100% (auto) width. For a non responsive site we should prefer this too. Overall it doesn’t matter cause i will replace grid.less and don’t use @screen-tablet any more.

At least i changed grid.less to:


//
// Grid system
// --------------------------------------------------

// Set the container width, and override it for fixed navbars in media queries
.container {
.container-fixed();
}

// Mobile-first defaults
.row {
.make-row();
}

// Common styles for small and large grid columns
.col-1,
.col-2,
.col-3,
.col-4,
.col-5,
.col-6,
.col-7,
.col-8,
.col-9,
.col-10,
.col-11,
.col-12,
{
position: relative;
// Prevent columns from collapsing when empty
min-height: 1px;
// Inner gutter via padding
padding-left: (@grid-gutter-width / 2);
padding-right: (@grid-gutter-width / 2);
}

//
// Container and grid column sizing
//

// Tiny device columns (smartphones)
.col-1,
.col-2,
.col-3,
.col-4,
.col-5,
.col-6,
.col-7,
.col-8,
.col-9,
.col-10,
.col-11,
.col-12 {
float: left;
}
.col-1 { width: percentage((1 / @grid-columns)); }
.col-2 { width: percentage((2 / @grid-columns)); }
.col-3 { width: percentage((3 / @grid-columns)); }
.col-4 { width: percentage((4 / @grid-columns)); }
.col-5 { width: percentage((5 / @grid-columns)); }
.col-6 { width: percentage((6 / @grid-columns)); }
.col-7 { width: percentage((7 / @grid-columns)); }
.col-8 { width: percentage((8 / @grid-columns)); }
.col-9 { width: percentage((9 / @grid-columns)); }
.col-10 { width: percentage((10/ @grid-columns)); }
.col-11 { width: percentage((11/ @grid-columns)); }
.col-12 { width: 100%; }

// Push and pull columns for source order changes
.col-push-1 { left: percentage((1 / @grid-columns)); }
.col-push-2 { left: percentage((2 / @grid-columns)); }
.col-push-3 { left: percentage((3 / @grid-columns)); }
.col-push-4 { left: percentage((4 / @grid-columns)); }
.col-push-5 { left: percentage((5 / @grid-columns)); }
.col-push-6 { left: percentage((6 / @grid-columns)); }
.col-push-7 { left: percentage((7 / @grid-columns)); }
.col-push-8 { left: percentage((8 / @grid-columns)); }
.col-push-9 { left: percentage((9 / @grid-columns)); }
.col-push-10 { left: percentage((10/ @grid-columns)); }
.col-push-11 { left: percentage((11/ @grid-columns)); }

.col-pull-1 { right: percentage((1 / @grid-columns)); }
.col-pull-2 { right: percentage((2 / @grid-columns)); }
.col-pull-3 { right: percentage((3 / @grid-columns)); }
.col-pull-4 { right: percentage((4 / @grid-columns)); }
.col-pull-5 { right: percentage((5 / @grid-columns)); }
.col-pull-6 { right: percentage((6 / @grid-columns)); }
.col-pull-7 { right: percentage((7 / @grid-columns)); }
.col-pull-8 { right: percentage((8 / @grid-columns)); }
.col-pull-9 { right: percentage((9 / @grid-columns)); }
.col-pull-10 { right: percentage((10/ @grid-columns)); }
.col-pull-11 { right: percentage((11/ @grid-columns)); }

// Offsets
.col-offset-1 { margin-left: percentage((1 / @grid-columns)); }
.col-offset-2 { margin-left: percentage((2 / @grid-columns)); }
.col-offset-3 { margin-left: percentage((3 / @grid-columns)); }
.col-offset-4 { margin-left: percentage((4 / @grid-columns)); }
.col-offset-5 { margin-left: percentage((5 / @grid-columns)); }
.col-offset-6 { margin-left: percentage((6 / @grid-columns)); }
.col-offset-7 { margin-left: percentage((7 / @grid-columns)); }
.col-offset-8 { margin-left: percentage((8 / @grid-columns)); }
.col-offset-9 { margin-left: percentage((9 / @grid-columns)); }
.col-offset-10 { margin-left: percentage((10/ @grid-columns)); }
.col-offset-11 { margin-left: percentage((11/ @grid-columns)); }

.container {
max-width: @container-desktop;
}

 

Now you will have a non responsive grid. Remember use “.col-” as class prefix for your grid rows now. Try it yourself and download the css file from: https://github.com/bassjobsen/non-responsive-tb3

Custom button colors in Twitter’s Bootstrap 3

Twitter’s Bootstrap 3 also delivers 6 button colors. Define your own button colors will a lot simpler as in Twitter’s Bootstrap 2. Cause TB3 drops support for IE7 the button css becomes more straight forward. Every button color still got 3 variants (default, active and disable). For the active state an background and border color are calculated based on the base color. The disable state use the base color and only adds a css-opacity.

TB3 not only drops IE7 support. Twitter’s Bootstrap 3 buttons don’t have a gradient. Also the text-shadow is removed.

I create a new Twitter Bootstrap Button Generator. Use this generator for Twitter’s Boostrap 3.x. Input a html color gives you the css code for your buttons. Add the css in your html after the Bootstrap’s CSS inclusion.

You got 2 choses of buttons. The default simple css and the more complex ‘old style’ css with added gradient. This new generator use Lessphp to generate the css code. Lessphp is a compiler for LESS written in PHP. See also: Twitter bootstrap buttons

Migrate your templates from Twitter Bootstrap 2.x to Twitter Bootstrap 3

NB the text below has been written with Twitter’s Bootstrap 3 RC1 in mind. RC2 show important changes, so also read:

Twitter Bootstrap 3Major points for migration:

  1. reconsider your base templates
  2. rename your grid classes
  3. if you templates don’t use the fluid layout, check nesting rows
  4. check and reconsider the stacking of elements in the new mobile grid

Base templates

Twitter Bootstrap 3 drops support for IE7 and Firefox 3.x. Following this you don’t need to include html5shiv any more. Including respond.js (not included) will be enough for CSS media querie support in IE8. For Twitter Bootstrap 2.x i used initializr.com to create a base template. For Twitter Bootstrap 3 you can consider to use a minimalistic template as base:

<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 101 Template</title>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<!– Bootstrap –>
<link href=”css/bootstrap.min.css” rel=”stylesheet” media=”screen”>
</head>
<body>
<h1>Hello, world!</h1>

<!– JavaScript plugins (requires jQuery) –>
<script src=”http://code.jquery.com/jquery.js”></script>
<!– Include all compiled plugins (below), or include individual files as needed –>
<script src=”js/bootstrap.min.js”></script>

<!– Optionally enable responsive features in IE8 –>
<script src=”js/respond.js”></script>
</body>
</html>

This template will be responsive mobile first by default.

Rename your grid classes

The grid classes for span and offset are renamed form span* and offset* to col-span-* and col-offset-* in Twitter Bootstrap 3. Preform a global search and replace action in your templates files to rename this classes. You could use regular expressions to do this. I use Geany to edit my templates files. Regular expressions like replace “span([0-8]+)” with “col-span-\1” did the trick, see below:

regular expression
From now Twitter’s Bootstrap defines three grids: Tiny grid for Phones (<480px), Small grid for Tablets (<768px) and the Medium-large grid for Destkops (>768px). The row class prefixes for these grid are “.col-“, “.col-sm-” and “.col-lg-“. The Medium-large grid will stack below 768 pixels screen width. So does the Small grid below 480 pixels and the tiny grid never stacks.Twitter’s Bootstrap 3 defines three grids: Tiny grid for Phones (<768px), Small grid for Tablets (>768px) and the Medium-Large grid for Destkops (>992px). The row class prefixes for these grid are “.col-”, “.col-sm-” and “.col-lg-”. The Medium-large grid will stack below 992 pixels screen width. So does the Small grid below 768 pixels and the tiny grid never stacks. Except for old phones which always will stack the elements (mobile first design). (last update about the grid based on Twitter’s Bootstrap 3 RC1, see also: Twitter’s Bootstrap 3 grid, changing breakpoint and removing padding)

See also http://examples.getbootstrap.com/grid/ for more examples of the grids. Or consider the table below:

Twitter's Boostrap 3 Grid

Use Less mixins to migrate your grid

The ‘old’ span* will not be the same as col-* or col-lg-*. So replacing the class names in your templates will be give you better results. But when looking to an example like http://examples.getbootstrap.com/jumbotron/index.html you will find the the col-lg-* class in place of the former span* class. This will give you a possibility to create a mixin for span* with the same styles as col-lg-* in this case. Note: The col-lg-* will stack below 992px by default (see: New twitter bootstrap 3 grid, changing breakpoint and removing padding).

Switch to fluid layout

A fluid layout uses percents instead of pixels for column widths. Twitter Bootstrap 2.x let you choose between pixels and percents. In Twitter Bootstrap 3 the grid is fluid by default. So if you didn’t use the fluid grid it’s time to switch now. The most important difference will be the nesting of row. Using percents a nested row should get a total width of 100% too cause it gets its width from its parent. For this reason each nested level of columns should add up to 12 columns in the fluid layout. Who don’t use the fluid layout in Twitter Bootstrap 2.x will used to give a nested row a set of columns that add up to the number of columns of its parent. So find your nested rows and change their columns so their number add up to 12.

The small and tiny grid

In Twitter Bootstrap 3 anything under 768px use the so called small device grid. The small and tiny device grid also have 12 columns and are fluid too. Offsets are not available and you have to use special classes to set the columns. Use the .col-* and .col-sm-* classes for the mobilethese grids.
You can use all grid classes for the same column b.e.:

<div class=”col-span-3 col-small-span-6″>

<div class=”col-6 col-sm-6 col-lg-6″>

The above will give you a column of 50% width in the large grid, but a column of 50% in the smaller grids too. So .col-* and col-sm-* will help you to set the way of stacking the columns on small devices. The col-lg-* classes are never applied on the small grids. Without adding col-6 and col-sm-6 a div with col-lg-6 get 100% width on the small device grids. see: http://www.bootply.com/67209

Also read: Writing Twitter’s Bootstrap with upgrading to v3 in mind

Example

I migrate my free demo webshop template to TB3. After doing the steps above there still will be other thing to fix.

Responsive navbar

In these templates the menu is wrapped in a “Responsive navbar”. Also here class names are changed. The most important change here is the toggle button is now a real <button> with class .navbar-toggle. The .navbar class adds a background color now.
I change the <a> of the toggle button to button and add the .navbar-toggle class. I also add “.navbar {background:none}” to my custom stylesheets. Now the menu works and collapses as before, beside the collapsing point is at 728 pixels now in stead of 940 pixels.

Padding instead of margin

In the templates i wrapped the product boxes in columns of a row. After the migration this don’t fit any more.

In the in change log we could read: “New single grid system (still uses .row) utilizes percentages over pixels, padding instead of margin, and box-sizing: border-box for easy math.”. Each column now got b.e. padding-left: 15px and padding-right: 15px instead of margin-left: 30px;
In the template i add an extra div. This div got my custom classes and will be the parent of the product info code.

<div class=”span3 productitem allround”><div class=”productitemimage”> changed to <div class=”col-4″><div class=”productitem allround”><div class=”productitemimage”>

Input-append class

Before i create the search box with the input-append class. With Twitter Bootstrap 3 you should use the input-group class together with a input-group-btn class:

<div class=”input-group col-4″>
<input type=”text”>
<span class=”input-group-btn”>
<button class=”btn btn-search” type=”button”>Search</button>
</span>
</div>

Breadcrumbs

Twitter Bootstrap 3 adds a separator (/) automatic. You don’t need to code the separator yourself and you have to remove the <span class=”divider”>/</span> from your code. The separator is a slash with color gainsboro (#CCCCCC) by default. You could add “.breadcrumb > li:after” to your custom css to change the separator char or its color.
.breadcrumb > li:after {color:#FF0000; content: ” >”;}

Pagination

The pagination class needs to add to the ul-tag direct instead of to the containing div. But when you want to center the pagination you will have add the center class to the containing div. Note Pager links (previous / next) are centered by default.

Download

You can download this Free Twitter Bootstrap 3 template on Github. Test your own templates on http://bootply.com/ now.

Glyphicons

With the launch of Bootstrap 3, icons have been moved to a separate repository.

Responsive images

Twitter’s Bootstrap 2 makes images responsive by default (by adding max-weight:100%). Bootstrap 3 has a special class to make your images responsive. Use `class=”img-responsive”` to make your images responsive now. See also: Images not responsive by default in Twitter Bootstrap 3.0 RC1?

WordPress Bootstrap 3 Start Theme

I start to migrate the former Skematik theme to Bootstrap 3. This powerful theme framework that can be used as a standalone website builder or as a framework to create child themes for WordPress build on Twitter’s Bootstrap 3. Please give it a try: https://github.com/bassjobsen/jamedo-bootstrap-start-theme. Contributions are welcome 🙂

Migrators

Other useful links:

The migration section in the docs,
Migration Guide, PHP class for migration and Bootstrap migration tools in progress: https://github.com/iatek/bootstrap-migrate

Migrate your Javascript Plugins

Many people wrote Javascript Plugins for Twitter’s Boostrap 2.x. Some examples: Bootstrap-datepicker and Jasny Bootstrap. Most plugins can migrate to TB3 easily, see: How to migrate your Twitter’s Bootstrap Javascript plugins from version 2.x to version 3

Twitter Bootstrap 3 breakpoints and grid

Release Candidate 2 has important changes for the grid and the grid classes. Read Twitter’s Bootstrap 3 RC2 (important changes)

Twitter Bootstrap 3The most important changes in Twitter Bootstrap 3 will be the more mobile-first approaching and the grid. From now Twitter’s Bootstrap defines three grids: Tiny grid for Phones (<768px), Small grid for Tablets (<992px) and the Medium-large grid for Destkops (>992px). The row class prefixes for these grid are “.col-”, “.col-sm-” and “.col-lg-”. The Medium-large grid will stack below 992 pixels screen width. So does the Small grid below 768 pixels and the tiny grid never stacks. (updated for Twitter’s Bootstrap3 RC1)

Twitter’s Bootstrap 2 defines breakpoints at 480px, 768px, 980px and 1200px. The layout will always be fluid below 768px.

For this reason the ‘old’ span* will not be the same as col-* or col-lg-*. So replacing the class names in your templates will be give you better results. But when looking to an example like http://examples.getbootstrap.com/jumbotron/index.html you will find the the col-lg-* class in place of the former span* class.

Twitter's Bootstrap 3 Grid

Breakpoints

In variables.less you will find:

// Media queries breakpoints
// --------------------------------------------------

// Extra small screen / phone
@screen-xsmall:              480px;
@screen-phone:               @screen-xsmall;

// Small screen / tablet
@screen-small:               768px;
@screen-tablet:              @screen-small;

// Medium screen / desktop
@screen-medium:              992px;
@screen-desktop:             @screen-medium;

// Large screen / wide desktop
@screen-large:               1200px;
@screen-large-desktop:       @screen-large;

// So media queries don't overlap when required, provide a maximum
@screen-small-max:           (@screen-medium - 1);
@screen-tablet-max:          (@screen-desktop - 1);
@screen-desktop-max:         (@screen-large-desktop - 1);

// Grid system
// --------------------------------------------------

// Number of columns in the grid system
@grid-columns:              12;
// Padding, to be divided by two and applied to the left and right of all columns
@grid-gutter-width:         30px;
// Point at which the navbar stops collapsing
@grid-float-breakpoint:     @screen-tablet;

When i test this values if found something different:

  • “col-*” will be applied always (never stacks)
  • “col-sm-*” will be applied between 768 and higher (992px) (stacks at 767)
  • “col-lg-*” will be applied between 992 and higher (stacks at 991)

NOTE: @screen-small is not in use at the moment.

See also http://examples.getbootstrap.com/grid/ for more examples of the grids.

The grid of Twitter Bootstrap 3 has been change to fluid by default. Twitter Bootstrap 3 has only one breakpoint. We will find this breakpoint at 768 pixels. This breakpoint splits small screens and large screens.

Both sides of the breakpoint have a 12 column wide grid. Only modern phone will use the mobile grid, older phoned should only stack. Class names for span and offset are change to col-span-* and -*. Also Twitter Bootstrap 3 introduce new classes (col-small-span-*) for stacking in the mobile grid. You will use this new classes in combination with the standard span and offset classes for large devices:

Imagine you will add two image next to each other in a row.

Twitter Bootstrap 2: (old situation)

<div class=”row”>
<div class=”span6″><img src=”image1.png”></div>
<div class=”span6″><img src=”image1.png”></div>
</div>

On small screens the images will stack with a 100% width in a fluid layout.

Twitter bootstrap 3:

<div class=”row”>
<div class=”col-span-6″><img src=”image1.png”></div>
<div class=”col-span-6″><img src=”image1.png”></div>
</div>

Without adding the mobile classes the images will don’t use the mobile grid and stack with 100% width.
(Note this differs from my expectation. I expected the main classes also should influence the mobile grid.)

no grid

Now we could add the new classes and let the images show next to each other with 50% width:

<div class=”row”>
<div class=”col-span-6 col-small-span-6″><img src=”image1.png”></div>
<div class=”col-span-6 col-small-span-6″><img src=”image1.png”></div>
</div>

grid

You can also use this class to change the way of stacking:

<div class=”row”>
<div class=”col-span-4 col-small-span-12″><img src=”image1.png”></div>
<div class=”col-span-4 col-small-span-6″><img src=”image1.png”></div>
<div class=”col-span-4 col-small-span-6″><img src=”image1.png”></div>
</div>

So we lost some breakpoints but got a small device grid back. Remember offsets are not available with the small grid. Two other new classes are col-pull-* and col-push-*. This classes help you to change the order of your columns. Also these classes are not available with the small grid.

Large screens

The new fluid grid without a default width also lets me think about large screens again. Should i made a decision about the max width of my site when viewed on a large screen? Users who use a site on an large screen with a full screen browsers window should have a reason to do so. So it seems to be more user friendly to let the user choose the max width. I found an interesting article about this: Life Beyond 960px: Designing for Large Screens. At the moment the .container class provides max-widths (728px, 940px en 1170px) for the large grid.

Explore and install Twitter Bootstrap 3

Twitter Bootstrap 3Release Candidate 1 of Twitter’s Bootstrap 3 has been released now. With RC1 Bootstrap uses Grunt to compile the files (see at the end of this post) Twitter Bootstrap 3 is not ready for production yet. At this time you can download the latest version on GitHub.
To build and compile this version you need to install: node, npm, less, jshint recess, uglify-js and make. I build on Ubuntu 12.04 LTS.

        Download:

https://github.com/twitter/bootstrap/archive/3.0.0-wip.zip

        Install node (

https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager

        )
        Install npm (apt-get install npm)
        Install less (npm install -g less)

Install jshint (npm install -g jshint, use npm install jshint recess uglify-js -g)install jshint, recess and uglify-js (install npm)

        run

make build

      to compile Bootstrap

When i tried to build Bootstrap the first time, i got an error like “Error: No compatible version found: source-map@’>=0.1.7- <0.2.0-‘” I solved this by adding Chris Lea’s PPA (https://launchpad.net/~chris-lea/ archive/node.js/) to my system. After updating node.js i still got some errors (js/bootstrap-affix.js: line 23, col 17, Bad option: ‘;_;’. etc.) when building. Running ‘npm install‘ after updating solved this.
After reading npm 1.0: Global vs Local installation and take a better look at the Makefile i realize Running ‘npm install‘ installs local versions of jshint, recess, uglify-js in “./node_modules/.bin”. Where v2.3.1 requires a local install v3 also looks in the global path.

Documentation

To view the docs locally, you’ll need to install Jekyll to run a local server. Run Jekyll from the “docs” dir; jekyll –server. Docs will be available on http://localhost:{port}/docs.html where {port} is 9001 by default. See also: Compile bootstrap 3 docs (How to)?
Twitter Bootstrap 3 Docs

 

Grunt

With RC1 Bootstrap uses Grunt with convenient methods for working with the framework.

  1. Install `grunt-cli` globally with `npm install -g grunt-cli`.
  2. Install the [necessary local dependencies](package.json) via `npm install`
  3. use `grunt` to compile and run tests or `grunt dist` to only compile CSS and Javascript

 

Compiled CSS and JS

Try twitter Bootstrap 3 without compiling and building it? Download the compiled and minified versions of the CSS, JavaScript, and fonts here. No documentation or original source files are included. Updated to RC1

 

Bootply

Bootply is a playground for Twitter Bootstrap. Use Bootply to design, prototype, extend, or test the Bootstrap framework. Bootply also offers you the opportunity to test your code with Twitter Bootstrap 3.0.0 RC now.

 

CDN

The folks over at NetDNA have graciously provided CDN support for Bootstrap’s CSS and JavaScript. To use, swap your local instances for the Bootstrap CDN links listed below.


<!-- Latest compiled and minified CSS -->
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0-rc1/css/bootstrap.min.css" rel="stylesheet" />

<!-- Latest compiled and minified JavaScript -->
<script type="text/javascript" src="//netdna.bootstrapcdn.com/bootstrap/3.0.0-rc1/js/bootstrap.min.js"></script>