Introduction to Pure Grids
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 (pure-u
orpure-u-*
) - The widths of the units are fractions
- Units have various class names that represent their widths. For example,
pure-u-1-2
has a width of 50%, whereaspure-u-1-5
would have a width of 20%. - All child elements of a grid must be units
- Child elements contained within an element with a
pure-g
classname must be a grid unit with apure-u
orpure-u-*
classname. - 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>
Grids Units Sizes
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 fractions listed below. For example the unit classname for 50% width is: pure-u-1-2
.
5ths-Based Units
24ths-Based Units
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 Responsive Grids
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.
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/purecss@3.0.0/build/grids-responsive-min.css" />
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 |
---|---|---|---|
None | None | Always | .pure-u-* |
sm | @media screen and (min-width: 35.5em) | ≥ 568px | .pure-u-sm-* |
md | @media screen and (min-width: 48em) | ≥ 768px | .pure-u-md-* |
lg | @media screen and (min-width: 64em) | ≥ 1024px | .pure-u-lg-* |
xl | @media screen and (min-width: 80em) | ≥ 1280px | .pure-u-xl-* |
xxl | @media screen and (min-width: 120em) | ≥ 1920px | .pure-u-xxl-* |
xxxl | @media screen and (min-width: 160em) | ≥ 2560px | .pure-u-xxxl-* |
x4k | @media screen and (min-width: 240em) | ≥ 3840px | .pure-u-x4k-* |
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 em
to px
is pretty simple:
1em == 16px *
* The em
to 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.