Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

201 lines (149 sloc) 9.64 kb
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery Mobile Docs - Content Grids</title>
<link rel="stylesheet" href="../../css/themes/default/jquery.mobile.css" />
<link rel="stylesheet" href="../_assets/css/jqm-docs.css"/>
<script src="../../js/jquery.js"></script>
<script src="../../docs/_assets/js/jqm-docs.js"></script>
<script src="../../js/"></script>
</head>
<body>
<div data-role="page" class="type-interior">
<div data-role="header" data-theme="f">
<h1>RWD grids</h1>
<a href="../../" data-icon="home" data-iconpos="notext" data-direction="reverse">Home</a>
<a href="../nav.html" data-icon="search" data-iconpos="notext" data-rel="dialog" data-transition="fade">Search</a>
</div><!-- /header -->
<div data-role="content">
<div class="content-primary">
<h2>Responsive grids</h2>
<p>When using <a href="content-grids.html">layout grids</a> 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.</p>
<p>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.</p>
<h2>Making the grids responsive</h2>
<p>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.</p>
<p>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 <code>min-width</code> media queries.</p>
<p>However, in the case of grids we can use a <code>max-width</code> media query to only apply the stacked grid styles <em>below</em> a width breakpoint. This allows us to leverage all the default grid styles but just tweak them at narrow widths.</p>
<p>Without the custom styles, our grid will be a 3 column layout across all screen widths:</p>
<div class="ui-grid-b">
<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><!-- /grid-b -->
<p>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:</p>
<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><!-- /grid-b -->
<p>To make this responsive, start by adding the <code>my-breakpoint</code> class to the grid container that references the custom breakpoint in your custom stylesheet:</p>
<pre><code>
&lt;div class=&quot;ui-grid-b <strong>my-breakpoint</strong>&quot;&gt;
&lt;div class=&quot;ui-block-a&quot;&gt;Block A&lt;/div&gt;
&lt;div class=&quot;ui-block-b&quot;&gt;Block B&lt;/div&gt;
&lt;div class=&quot;ui-block-c&quot;&gt;Block C&lt;/div&gt;
&lt;/div&gt;&lt;!-- /grid-b --&gt;
</code></pre>
<h3>Adding the stack breakpoint at narrow widths</h3>
<p>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. </p>
<p>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.</p>
<pre><code>
<strong>@media all and (max-width: 50em) {</strong>
.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;
}
<strong>}</strong>
</code></pre>
<p>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 <a href="content-grids.html">grid type</a> by stacking up selectors for all the grid classes from <code>ui-block-a</code> to <code>ui-block-e</code> on the styles.</p>
<style>
@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;
}
}
@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%; }
}
}
</style>
<p>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.</p>
<h3>Adding a widescreen breakpoint to adjust ratios</h3>
<p>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 <code>min-width</code> media query to tweak widths in our custom style like this:</p>
<pre><code>
<strong>@media all and (min-width: 75em) {</strong>
.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; }
}
<strong>}
</strong> </code></pre>
<p>Note the slightly narrower widths set to work around rounding issues across platforms. </p>
<h2>Applying a preset breakpoint</h2>
<p>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. </p>
<p>To use this preset breakpoint, add the <code>ui-responsive</code> class to the grid container to apply the stacked presentation <em>below</em> 560px (35em). If this breakpoint doesn't work for your content, we encourage you to write a custom breakpoint as described above.</p>
<pre><code>&lt;div class=&quot;ui-grid-b <strong>ui-responsive</strong>&quot;&gt;</code></pre>
<p>These are standard grids that are made responsive by <code>ui-responsive</code> class to the grid container:</p>
<h3>Grid A (50/50)</h3>
<div class="ui-grid-a ui-responsive">
<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><!-- /grid-a -->
<h3>Grid B (33/33/33)</h3>
<div class="ui-grid-b ui-responsive">
<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><!-- /grid-b -->
<h3>Grid C (25/25/25/25)</h3>
<div class="ui-grid-c ui-responsive">
<div class="ui-block-a"><div class="ui-body ui-body-d">A</div></div>
<div class="ui-block-b"><div class="ui-body ui-body-d">B</div></div>
<div class="ui-block-c"><div class="ui-body ui-body-d">C</div></div>
<div class="ui-block-d"><div class="ui-body ui-body-d">D</div></div>
</div><!-- /grid-c -->
<h3>Grid D (20/20/20/20/20)</h3>
<div class="ui-grid-d ui-responsive">
<div class="ui-block-a"><div class="ui-body ui-body-d">A</div></div>
<div class="ui-block-b"><div class="ui-body ui-body-d">B</div></div>
<div class="ui-block-c"><div class="ui-body ui-body-d">C</div></div>
<div class="ui-block-d"><div class="ui-body ui-body-d">D</div></div>
<div class="ui-block-e"><div class="ui-body ui-body-d">E</div></div>
</div><!-- /grid-d -->
</div><!--/content-primary -->
<div class="content-secondary">
<div data-role="collapsible" data-collapsed="true" data-theme="b" data-content-theme="d">
<h3>More in this section</h3>
<ul data-role="listview" data-theme="c" data-dividertheme="d">
<li data-role="list-divider">Content Formatting</li>
<li><a href="content-html.html">Basic HTML styles</a></li>
<li><a href="content-grids.html">Layout grids (columns)</a></li>
<li data-theme="a"><a href="content-grids-responsive.html">Responsive grids</a></li>
<li><a href="content-collapsible.html">Collapsible content blocks</a></li>
<li><a href="content-collapsible-set.html">Collapsible sets (accordions)</a></li>
<li><a href="content-themes.html">Theming content</a></li>
</ul>
</div>
</div>
</div><!-- /content -->
<div data-role="footer" class="footer-docs" data-theme="c">
<p class="jqm-version"></p>
<p>&copy; 2012 jQuery Foundation and other contributors</p>
</div>
</div><!-- /page -->
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.