Using multiple column layouts isn't generally recommended on a mobile device because of the narrow screen width, but there are times where you may need to place small elements side-by-side (like buttons or navigation tabs, for example).
The jQuery Mobile framework provides a simple way to build CSS-based columns through a block style class convention called
There are two preset configurations layouts — two-column (using the
ui-grid-a), and three-column (using the
ui-grid-b) — that can be used in any situation that requires columns. Grids are 100% width, completely invisible (no borders or backgrounds) and don't have padding or margins, so they shouldn't interfere with the styles of elements placed inside them.
To build a two-column (50/50%) layout, start with a container with a
ui-grid-a, and add two child containers inside it classed with
ui-block-a for the first column and
ui-block-b for the second:
<div class="ui-grid-a"> <div class="ui-block-a"><strong>I'm Block A</strong> and text inside will wrap</div> <div class="ui-block-b"><strong>I'm Block B</strong> and text inside will wrap</div> </div><!-- /grid-a -->
The above markup produces the following content layout:
As you see above, by default grid blocks have no styles for appearance; they simply present content side-by-side.
Grid classes can be applied to any container. In this next example, we add
ui-grid-a to a
fieldset, and apply the
ui-block classes to the two buttons inside to stretch them each to 50% of the screen width:
<fieldset class="ui-grid-a"> <div class="ui-block-a"><button type="submit" data-theme="c">Cancel</button></div> <div class="ui-block-b"><button type="submit" data-theme="b">Submit</button></div> </fieldset>
And, grid blocks can adopt presentation styles from the theming system — by adding a height and color swatch reference to the grid blocks, we can achieve this style appearance:
The other grid layout configuration uses
class=ui-grid-b on the parent, and 3 child container elements, each with its respective
ui-block-a/b/c class, to create a three-column layout (33/33/33%).
<div class="ui-grid-b"> <div class="ui-block-a">Block A</div> <div class="ui-block-b">Block B</div> <div class="ui-block-c">Block C</div> </div><!-- /grid-a -->
This will produce a 33/33/33% grid for our content.
And an example of a 3 column grid with buttons inside:
A four-column, 25/25/25/25% grid is created by specifying
class=ui-grid-c on the parent and adding a fourth block.
A five-column, 20/20/20/20/20% grid is created by specifying
class=ui-grid-d on the parent and adding a fourth block.
Grids are designed to wrap to multiple rows of items. For example, if you specify a 3-column grid (ui-grid-b) on a container that has nine child blocks, it will wrap to 3 rows of 3 items each. There is a CSS rule to clear the floats and start a new line when the
class=ui-block-a is seen so make sure to assign block classes in a repeating sequence (a, b, c, a, b, c, etc.) that maps to the grid type: