Responsive grids
When using layout grids for building full-level layouts, it may make sense to apply responsive web design (RWD) principles to ensure that the layout adapts to a wide range screen widths.
The simplest form of responsive behavior swaps from a stacked layout on narrow screens like a smartphone to the multi-column grid layouts at wider screens. This can be done by targeting styles to specific screen widths by using CSS media queries.
Making the grids responsive
By default, the grid classes will result in a multi column layout across all screen widths. The styles to make the grids responsive are added by applying a media query with rules to switch to the stacked style presentation below a specific screen width.
We normally recommend starting with mobile-first approach for media queries: starting with the styles that apply to the smallest screen sizes in the core widget styles, then progressively layering breakpoints up to larger screens by using min-width
media queries.
However, in the case of grids we can use a max-width
media query to only apply the stacked grid styles below a width breakpoint. This allows us to leverage all the default grid styles but just tweak them at narrow widths.
Without the custom styles, our grid will be a 3 column layout across all screen widths:
In our custom styles, we want this grid to stack at narrow widths, then switch to a standard 3 column layout. At very wide screen widths, we want first column to take up 50% of the width, like this:
To make this responsive, start by adding the my-breakpoint
class to the grid container that references the custom breakpoint in your custom stylesheet:
<div class="ui-grid-b my-breakpoint"> <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-b -->
Adding the stack breakpoint at narrow widths
This class is used to scope the styles within the custom media query so they will only apply when this class is added to the grid container. The media query wraps the conditional styles we only want applied below 50em.
In your media queries, use em units instead of pixels to ensure that the media query will take font size into account in addition to just screen width. To calculate a screen widths in ems, divide the target width in pixels by 16 which is the default font size of the body.
@media all and (max-width: 50em) { .my-breakpoint .ui-block-a, .my-breakpoint .ui-block-b, .my-breakpoint .ui-block-c, .my-breakpoint .ui-block-d, .my-breakpoint .ui-block-e { width: 100%; float:none; } }
Within this media query, we set the width to 100% and negate the float property to make the grid blocks stack below 50em screen widths. These rules are applied to every grid type by stacking up selectors for all the grid classes from ui-block-a
to ui-block-e
on the styles.
That is all it takes to make grids responsive and it's easy to add additional styling rules to each breakpoint to change it up even more. We encourage you to create as many custom breakpoints as you need based on your unique content and layout needs.
Adding a widescreen breakpoint to adjust ratios
Building on the example above, we can add an additional breakpoint to shift the widths to make the first column 50% width and the other two 25% above 75em (1,200 pixels) by layering an additional min-width
media query to tweak widths in our custom style like this:
@media all and (min-width: 75em) { .my-breakpoint.ui-grid-b .ui-block-a { width: 49.95%; } .my-breakpoint.ui-grid-b .ui-block-b, .my-breakpoint.ui-grid-b .ui-block-c { width: 24.925%; } .my-breakpoint.ui-grid-b .ui-block-a { clear: left; } } }
Note the slightly narrower widths set to work around rounding issues across platforms.
Applying a preset breakpoint
Even though we strongly encourage you to write custom breakpoints yourself, the framework includes a single pre-configured breakpoint that targets the stacked style to smaller phones and swaps to the multi-column presentation on larger phones, tablet and desktop devices.
To use this preset breakpoint, add the ui-responsive
class to the grid container to apply the stacked presentation below 560px (35em). If this breakpoint doesn't work for your content, we encourage you to write a custom breakpoint as described above.
<div class="ui-grid-b ui-responsive">
These are standard grids that are made responsive by ui-responsive
class to the grid container:
Grid A (50/50)
Grid B (33/33/33)
Grid C (25/25/25/25)
Grid D (20/20/20/20/20)
A basic example of responsive grids
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>responsive-grid demo</title> <link rel="stylesheet" href="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"> <script src="//code.jquery.com/jquery-1.10.2.min.js"></script> <script src="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> <style> @media all and (max-width: 35em) { .my-breakpoint .ui-block-a, .my-breakpoint .ui-block-b, .my-breakpoint .ui-block-c, .my-breakpoint .ui-block-d, .my-breakpoint .ui-block-e { width: 100%; float:none; } } @media all and (min-width: 45em) { .my-breakpoint.ui-grid-b .ui-block-a { width: 49.95%; } .my-breakpoint.ui-grid-b .ui-block-b, .my-breakpoint.ui-grid-b .ui-block-c { width: 24.925%; } } </style> </head> <body> <div data-role="header"> <h1>Responsive Grid Example</h1> </div> <div role="main" class="ui-content"> <div class="ui-grid-b my-breakpoint"> <div class="ui-block-a"><div class="ui-body ui-body-d">Block A</div></div> <div class="ui-block-b"><div class="ui-body ui-body-d">Block B</div></div> <div class="ui-block-c"><div class="ui-body ui-body-d">Block C</div></div> </div> </body> </html>
Please login to continue.