Pure Grids are easy to work with, and very powerful. There are a few simple concepts to keep in mind:
- Grid classes vs. unit classes
Pure Grids consist of two types of classes: the grid class (
pure-g) and unit classes (
- The widths of the units are fractions
Units have various class names that represent their widths. For example,
pure-u-1-2has a width of 50%, whereas
pure-u-1-5would have a width of 20%.
- All child elements of a grid must be units
Child elements contained within an element with a
pure-gclassname must be a grid unit with a
- Content goes inside grid units
- All content which is visible to people needs to be contained inside a grid unit. This ensures that the content will be rendered properly.
Let's start with a simple example. Here's a grid with three columns:
<div class="pure-g"> <div class="pure-u-1-3"><p>Thirds</p></div> <div class="pure-u-1-3"><p>Thirds</p></div> <div class="pure-u-1-3"><p>Thirds</p></div> </div>
Pure ships with both a 5ths and 24ths based grid. Depicted below are the available units that can be appended to the
pure-u-* classname where
* is one of the unit factions listed below. For example the unit classname for 50% width is:
Custom Unit Sizes
We are working on building tools to allow people to customize Pure Grids. The first low-level tools, the Pure Grids Rework Plugin, is available to use today—we use this tool to generate Pure's built-in unit sizes.
Pure has a mobile-first responsive grid system that can be used declaratively through CSS class names. It's a robust and flexible grid that builds on top of the default grid.
Including on Your Page
Since media queries cannot be over-written, we do not include the grid system as part of
pure.css. You'll have to pull it in as a separate CSS file. You can do this by adding the following
<link> tag to your page.
<!--[if lte IE 8]> <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/grids-responsive-old-ie-min.css"> <![endif]--> <!--[if gt IE 8]><!--> <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/grids-responsive-min.css"> <!--<![endif]-->
Even though Pure Grids are designed to be mobile-first, you may want to show the "desktop-experience" to users who view your website in IE 8 or below. Just include the
grids-responsive-old-ie.css file inside an IE conditional statement and Pure Grids will adapt to show the desktop experience.
Pure's Regular Grid vs. Responsive Grid
The best way to understand the difference between Pure's regular grid and a responsive grid is through an example.The snippet below shows how regular Pure Grids are written. These grids are unresponsive. They'll always be
width: 33.33%, irrespective of the width of the screen.
<div class="pure-g"> <div class="pure-u-1-3"> ... </div> <div class="pure-u-1-3"> ... </div> <div class="pure-u-1-3"> ... </div> </div>
Next, let's look at a responsive grid. Elements within this grid will be
width: 100% on small screens, but will shrink to become
width: 33.33% on medium-sized screens and above.
<div class="pure-g"> <div class="pure-u-1 pure-u-md-1-3"> ... </div> <div class="pure-u-1 pure-u-md-1-3"> ... </div> <div class="pure-u-1 pure-u-md-1-3"> ... </div> </div>
Default Media Queries
When using Responsive Grids, you can control how the grid behaves at specific breakpoints by adding class names. Pure's default responsive grids comes with the following class names and media query breakpoints.
|Key||CSS Media Query||Applies||Classname|
Using Relative Units for Widths
You may have noticed that we use
em for our default CSS Media Query widths instead of
px. This was a conscious decision since it allows the Media Queries to respond appropriately when people zoom the webpage. Check out this article by Brad Frost for some background information on using relative units within Media Queries.
If you do want to use units other than
em, you can always modify the default Media Queries on the Get Started page. Converting from
px is pretty simple:
1em == 16px *
px conversion is based on the browser's default font size, which is generally
16px, but can be overridden by a user in their browser settings.
An Example of Pure Responsive Grids
The example below leverages Pure's Responsive Grid to create a row with four columns. The columns stack on small screens, take up
width: 50% on medium-sized screens, and
width: 25% on large screens.
This is done by adding the
.pure-u-1 class for small screens,
.pure-u-md-1-2 for medium-sized screens, and
.pure-u-lg-1-4 for large screens. Resize the page to see the grid respond to the screen size.