Table of Contents
Quick start
Here is the minimal markup you need to put in your pages:
<!doctype html> <html> <head> <!-- Don't forget the viewport meta tag for responsive sites! --> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Include PocketGrid --> <link rel="stylesheet" href="pocketgrid.css" /> </head> <body> <!-- YOUR CONTENT HERE --> </body> </html>
As you can see, you just have to include the pocketgrid.css (or pocketgrid.min.css) file in the <head>
tag of your page
and don't forget to set the viewport meta tag to manage media queries correctly on mobile.
That's all you need to work with PocketGrid!
Basic rules
Instead of creating grids with rows and columns, PocketGrid introduces a more flexible concept with blocks and block groups.
Block groups are similar to rows, and blocks are similar to columns.
The main difference is that a block group can represent several rows (thanks to the "automatic rows" feature that we will see later), allowing much more flexibility than traditional grids.
The basic rules with PocketGrid are:
- Blocks have a default width of 100%
- Blocks must be contained in a block-group
- To display blocks side by side (like columns), just give them a width in percentage (for example 30% and 70% to make 2 columns)
- Automatic rows: blocks automatically go to a new row if the current row is full, like words in a text!
So you don't need to put a block-group container for each row!
Creating rows and columns: the classical way...
<div class="block-group">
) and columns (using <div class="block">
).Here you can see a demo with a grid containing 3 rows with various column sizes (the source code is just after the demo):
You can open each example alone in a new tab clicking on the "Open in a new tab" orange button at the top right side of the frame below.
- create your block-groups (rows) and blocks (columns) in your HTML
- define the block widths in your CSS
<div class="block-group"> <!-- First row --> <div class="b1 block">b1</div> <div class="b2 block">b2</div> </div> <div class="block-group"> <!-- Second row --> <div class="b3 block">b3</div> <div class="b4 block">b4</div> <div class="b5 block">b5</div> </div> <div class="block-group"> <!-- Third row --> <div class="b6 block">b6</div> <div class="b7 block">b7</div> <div class="b8 block">b8</div> <div class="b9 block">b9</div> <div class="b10 block">b10</div> </div>
/* First row */ .b1, .b2 { width: 50%; } /* Second row */ .b3 { width: 50%; } .b4, .b5 { width: 25%; } /* Third row */ .b6, .b7, .b8, .b9, .b10 { width: 20%; }
Creating rows and columns: the COOL way!
Thus, instead of creating a block-group for each row, we can create just 1 block-group containing all our blocks!
Then, according to the width of blocks, blocks will automatically go to a new line when a row is full:
- b1 + b2 = 50% + 50% = 100%, so b3 goes to a new line.
- b3 + b4 + b5 = 50% + 25% + 25% = 100%, so b6 goes to a new line.
- b6 + b7 + b8 + b9 = 25% + 25% + 20% + 20% = 90%, but b10 has a width of 20%, so the total would be greater than 100%. Thus b10 goes to a new line!
<div class="block-group"> <div class="b1 block">b1</div> <div class="b2 block">b2</div> <div class="b3 block">b3</div> <div class="b4 block">b4</div> <div class="b5 block">b5</div> <div class="b6 block">b6</div> <div class="b7 block">b7</div> <div class="b8 block">b8</div> <div class="b9 block">b9</div> <div class="b10 block">b10</div> </div>
.b1, .b2, .b3 { width: 50%; } .b4, .b5, .b6, .b7 { width: 25%; } .b8, .b9, .b10 { width: 20%; }
- In the HTML, you just define blocks in a block-group, no matter if they are on the same row or not.
- In the CSS, you set the block widths, and according to the block widths, the blocks will be displayed on the same row (or not).
- It's compatible with Media Queries: you can set different widths for each block according to the device (smartphone, tablet, desktop, TV...)
For example, blocks could be stacked on mobile, side by side on desktop, and an hybrid version on tablets: it's just CSS configuration!
That's what makes PocketGrid so flexible and powerful!
Mobile layout
This kind of layout is very simple with PocketGrid: you just have to put all your blocks in a block-group, without any CSS configuration (because the default width of blocks is 100%, and blocks automatically go to a new row after 100%, so blocks are stacked by default):
- each block has a
block
class - a
block-group
wraps all these blocks - blocks are stacked by default (width: 100%)
<div class="block-group"> <div class="header block">Header</div> <div class="nav block">Nav</div> <div class="main block">Main</div> <div class="links block">Links</div> <div class="footer block">Footer</div> </div>
/* Nothing in the CSS! */
Basic layout
<div class="block-group"> <div class="header block">Header</div> <div class="nav block">Nav</div> <div class="main block">Main</div> <div class="links block">Links</div> <div class="footer block">Footer</div> </div>
.nav { width: 20%; } .main { width: 80%; }
Making it responsive
For example, we want to have this behaviour:
- All blocks are stacked on mobile
- The navigation and main blocks are side by side on tablet (width > 768px)
- The navigation, main and links blocks are side by side on desktop (width > 960px)
<div class="block-group"> <div class="header block">Header</div> <div class="nav block">Nav</div> <div class="main block">Main</div> <div class="links block">Links</div> <div class="footer block">Footer</div> </div>
/* Smartphone version Nothing to do: blocks are stacked by default. */ /* Tablet version */ @media (min-width: 768px) { .nav { width: 20%; } .main { width: 80%; } } /* Desktop version */ @media (min-width: 960px) { .nav { width: 20%; } .main { width: 60%; } .links { width: 20%; } }
Nested grids
You just have to put a block-group inside another block-group!
Let's modify the previous example, replacing the main block with a nested grid (containing items in 5 columns):
<div class="block-group"> <div class="header block">Header</div> <div class="nav block">Nav</div> <!-- nested grid --> <div class="main block-group"> <div class="ngrid block">Nested grid</div> <div class="item block">1</div> <div class="item block">2</div> <div class="item block">3</div> <div class="item block">4</div> <div class="item block">5</div> <div class="item block">6</div> <div class="item block">7</div> <div class="item block">8</div> <div class="item block">9</div> <div class="item block">10</div> </div> <div class="footer block">Footer</div> </div>
.nav { width: 20%; } .main { width: 80%; } /* Items in 5 columns (20% each) */ .item { width: 20%; } /* Give a height of 200px to the nav, to make it more realistic! */ .nav { height: 200px; }
Nested grids responsive
We will change the number of columns of our grid according to the device width.
Keeping our previous nested grids example, we will display the nested items in 2 columns on mobile, 4 columns on tablet and 5 columns on desktop!
Resize your browser and see the layout changes in the preview below.
Well, with PocketGrid, it's very simple: to dynamically change the number of columns of a grid, you just have to redefine the block width according to media queries:
<div class="block-group"> <div class="header block">Header</div> <div class="nav block">Nav</div> <!-- nested grid --> <div class="main block-group"> <div class="ngrid block">Nested grid</div> <div class="item block">1</div> <div class="item block">2</div> <div class="item block">3</div> <div class="item block">4</div> <div class="item block">5</div> <div class="item block">6</div> <div class="item block">7</div> <div class="item block">8</div> <div class="item block">9</div> <div class="item block">10</div> </div> <div class="footer block">Footer</div> </div>
/* Common to smartphone, tablet and desktop */ .nav { width: 20%; height: 200px; } .main { width: 80%; } .item { width: 50%; } /* 2 columns */ /* Tablet version: items are displayed in 4 columns (25% each) */ @media (min-width: 40em) { .item { width: 25%; } /* 4 columns */ } /* Desktop version: items are displayed in 5 columns (20% each) */ @media (min-width: 60em) { .item { width: 20%; } /* 5 columns */ }
Offsetting columns
To do so, you just have to set the margin-left or margin-right properties of a block to the size of one or more columns.
For example, here the "Left offset" block has a left offset of 1 column (
margin-left: 20%
) and the "Right offset" block has a left offset of 2 column (margin-right: 40%
)
<body class="block-group"> <div class="b1 block">B1</div> <div class="b2 block">B2</div> <div class="b3 block"><div class="box" title="margin-left: 20%">Left offset</div></div> <div class="b4 block">B4</div> <div class="b5 block">B5</div> <div class="b6 block">B6</div> <div class="b7 block">B7</div> <div class="b8 block">B8</div> <div class="b9 block">B9</div> <div class="b10 block">B10</div> <div class="b11 block"><div class="box" title="margin-right: 20%">Right offset</div></div> <div class="b12 block">B12</div> </body>
.b1, .b2, .b3, .b4, .b5, .b6, .b7, .b8, .b9, .b10, .b11, .b12 { width: 20%; } /* Left offset: just give the margin-left the size you want! Note: For IE6 compatibility, you have to put "!important" also. */ .b3 { margin-left: 20% !important; } /* Right offset: just give the margin-right the size you want! */ .b11 { margin-right: 40% !important; } /* Some styles for better readability */ .b3 .box { background-color: #CDF; font-size: .8em; } .b11 .box { background-color: #CFC; font-size: .8em; }
Push / Pull (swap columns)
- Set the
position
property of the blocks to "relative
" - Set the
left
property of A to the width of B - Set the
left
property of B to minus the width of A
<body class="block-group"> <div class="b1 block">B1</div> <div class="b2 block">B2</div> <div class="before block">BEFORE</div> <div class="after block">AFTER</div> <div class="b5 block">B5</div> <div class="b6 block">B6</div> <div class="b7 block">B7</div> <div class="b8 block">B8</div> <div class="b9 block">B9</div> <div class="b10 block">B10</div> </body>
.b1, .b2, .b5, .b6, .b7, .b8, .b9, .b10 { width: 10%; } .before { width: 30%; /* Move the column to the right (left == AFTER's width) */ position: relative; left: 20%; } .after { width: 20%; /* Move the column to the left (left == -BEFORE's width) */ position: relative; left: -30%; } .before .box { background-color: #CDF; } .after .box { background-color: #CFC; }
Gutters
PocketGrid can manage horizontal and vertical gutters.
To use gutters, you have 2 options:
Option 1: paddings all around the blocks
This first option is quite intuitive: to make gutters, we apply padding all around blocks:
(to illustrate it, you can see the padding of the first and last blocks in red below)
.block-group { margin: -verticalGutter -horizontalGutter; } .block { padding: verticalGutter horizontalGutter; }For example, if we want an vertical gutter of 2px, and a horizontal gutter of 6px:
.block-group { margin: -2px -6px; } .block { padding: 2px 6px; }(note that block-group margins are negative values)
Advantage of this option: it's intuitive and it's easy to make a mouseover effect on blocks for example.
Drawback of this option: because of the bottom and right paddings, it could display scrollbars when the grid is 100% of the page width.
Option 2: paddings only on left and top sides
This second option consists of putting paddings only to the left and top sides of blocks to avoid the scrollbars problem of option 1:
(to illustrate it, you can see the padding of the first and last blocks in red below, and compare it to option 1)
.block-group { margin: -verticalGutter 0 0 -horizontalGutter; } .block { padding: verticalGutter 0 0 horizontalGutter; }For example, if we want an vertical gutter of 2px, and a horizontal gutter of 6px:
.block-group { margin: -4px 0 0 -12px; } .block { padding: 4px 0 0 12px; }(note that margins and paddings have been multiplied by 2 in comparison to option 1,
because they are only on left and top sides)
Advantage of this option: avoids the scrollbars issue.
Drawback of this option: you cannot put a background or a border on these blocks, because it would not be centered.
Which option is better? It depends! ;)
Option 1 (paddings all around the blocks) can be more appropriate if you want to put backgrounds directly on blocks (because paddings are equally spread around blocks).
Option 2 (top and left paddings) is more appropriate for global site layout if your layout is 100% page width (to avoid scrollbars).
Try playing with both, and see which one feels better for you.
You could even mix these 2 options! With PocketGrid, you are free!
To go further with gutters, you can see these 2 examples:
- Mixing option 1 and option 2 gutter types: use the best of both worlds!
- Responsive gutters! (yes, with PocketGrid, even gutters can be responsive!)
Automatic rows in real life!
However, when blocks have different heights, the "automatic rows" layout could seem broken (but of course, it's not!)
To make it clearer, let's see how PocketGrid works internally:
For layout positioning, PocketGrid uses floats (like most CSS grids): each
block
has a "float: left
" property.By default, float elements are displayed side by side, and with PocketGrid's "automatic rows" feature, when there is not enough space in the row to display all elements, the next elements go to a new line (like words in a text):
<div class="block-group"> <div class="block b1">1</div> <div class="block b2">2</div> <div class="block b3">3</div> <div class="block b4">4</div> <div class="block b5">5</div> <div class="block b6">6</div> <div class="block b7">7</div> <div class="block b8">8</div> <div class="block b9">9</div> <div class="block b10">10</div> <div class="block b11">11</div> <div class="block b12">12</div> </div>
Here, block 2 has a greater height:
A simple (but poor) solution could be to use a "block-group" for each row (like Twitter Bootstrap does, with its "row" class), but this solution would loose the "automatic rows" benefits:
<div class="block-group"> <div class="block b1">1</div> <div class="block b2">2</div> <div class="block b3">3</div> <div class="block b4">4</div> </div> <div class="block-group"> <div class="block b5">5</div> <div class="block b6">6</div> <div class="block b7">7</div> <div class="block b8">8</div> </div> <div class="block-group"> <div class="block b9">9</div> <div class="block b10">10</div> <div class="block b11">11</div> <div class="block b12">12</div> </div>
clear: left
" on blocks that should be on a real new row (here: block 5):
.b5 {
clear: left;
}
clear: left
" on each block that should be on a real new row, we can use the nth-child
pseudo selector to force the clear every 4 blocks:
.block:nth-child(4n+1) { clear: left; }
clear: left
" solution is not compatible with IE6 and IE7.For these browsers, you should force the blocks to have the same height, or you should use a "block-group" for each row.