XPages – Create a responsive layout with Twitter Bootstrap – #3

Today I want to dig a little deeper into Twitter Bootstrap and show what does that magic and makes the design responsive. If you missed the first 2 articles feel free to get an introduction in part 1  and a first responsive design in XPages in part 2 now.

OK, let’s go. So a responsive design adjust the layout of a webpage to different screen sizes. It does it on the fly while you are resizing your browser. The advantage of this approach is that you do not need to create different pages for different devices. Just create one page and let the responsive magic do the rest. This is an idealized and simplified statement since you have to think about what to show where in which screen size, what to remove, what to add and how to arrange things, but once done you only need to put stuff in the right places and let bootstrap do the rest.

The concept that responsive boilerplates and tools implement is based on media queries. Media queries allow web designers to write css for different media types like print and screen and for different screen sizes. You could let a print version of your webpage look different from the screen version by applying custom css into the following section of your style sheets:

@media print {
/* All your print styles go here *
#header, #footer, #nav { display: none !important; }

This does not make a webpage respond to different screen sizes, right? Here is a css frame that get’s you started and shows all the magic of Twitter Bootstrap and other frameworks like Bootstrap.

/* Large desktop */
@media (min-width: 1200px) { ... }

/* Portrait tablet to landscape and desktop */
@media (min-width: 768px) and (max-width: 979px) { ... }

/* Landscape phone to portrait tablet */
@media (max-width: 767px) { ... }

/* Landscape phones and down */
@media (max-width: 480px) { ... }

So you simply use the „min-width“ and „max-width“ attributes in media queries to define when certain css rules will show up in your application or vanish. Twitter Bootstrap uses these boundaries to adjust the design:

Label Layout width Column width Gutter width
Large display 1200px and up 70px 30px
Default 980px and up 60px 20px
Portrait tablets 768px and above 42px 20px
Phones to tablets 767px and below Fluid columns, no fixed widths
Phones 480px and below

So you can use any mix of these values in your style sheet as min and max values. If you want to apply a black body background only to landscape tablets you can use this in your style sheet:

@media (min-width: 480px) and (max-width: 767px) {
body {background:black;}

If you have already downloaded Bootstrap check out the file „bootstrap-responsive.css“. You will see that the entire file is based on media queries. It’s a good resource to get familiar with this technology.

To make it even easier for you Bootstrap provides a set of css classes that you can apply out of the box without having to code media queries. The following table shows css classes that speak for themselves.

Phones 767px and below Tablets 979px to 768px Desktops Default
.visible-phone Visible Hidden Hidden
.visible-tablet Hidden Visible Hidden
.visible-desktop Hidden Hidden Visible
.hidden-phone Hidden Visible Visible
.hidden-tablet Visible Hidden Visible
.hidden-desktop Visible Visible Hidden

Just use them in your Xpages either single or in combination to show or hide certain elements in different screen sizes. So I hope this helped a little to get a better understanding of the responsive magic. In my next article in this series I will show you how to set up a site navigator that works in all screen sizes with different looks and functionality.

Note: I copied some of the samples and the tables from the Twitter Bootstrap homepage.