Unlimited Wordpress themes, plugins, graphics & courses! Unlimited asset downloads! From \$16.50/m

# Build a Modular Dashboard Interface With Pure

Today, we will be making a beautiful, minimal dashboard using Pure (view the project on GitHub), the new mini-CSS module library from Yahoo!. We will be talking about and using some of the principles of SMACSS (Scalable and Modular Architecture for CSS). We'll also be using icons from WeLoveIconFonts.com.

Note: Before we go any further, there is a minimum emphasis on graphs for the dashboard we will be creating. Graphs are best executed with something more JavaScript heavy, and in order to keep the focus on CSS and minimal JavaScript, this tutorial will generally avoid graph drawing techniques.

## A Bit About Pure

Pure was built by the team at Yahoo! and YUI to be a very lightweight set of modules (less than 6k). Think of this as an extended Normalize that also includes styles for very common things like grids, forms, buttons, tables, and menus.

The YUI team built Pure to be a starting point, not a complete solution. From this starting point, they expect developers to expand and build their custom CSS as an extension of this base.

For this reason, a lot of what we teach in this tutorial are techniques for extending a basic foundational library like Pure to suit application interaction, like when using a dashboard.

## A Bit About SMACSS

SMACSS, short for Scalable and Modular Architecture for CSS, and the brain child of Jonathan Snook, is a set of practices designed for building CSS that is easily understood, easy to extend, and avoids the CSS specificity war.

This is futher assisted by not using IDs for styling and limiting the nesting of selectors as much as possible. The basic idea is that CSS can be categorized as base, layout, modules, states, and themes. Structuring CSS around these categories and following naming conventions that allude to these categories makes SMACSS readable and immediately meaningful. This avoids class names that have arbitrary styles associated with them. The basic idea of categorization follows this naming convention.

• Base: no name (default elements, no classes)
• Layout: .layout, .grid, .grid-column, .grid-column.five.columns
• Modules: .btn (module name), .btn-default, .btn-calltoaction; .modal, .modal-header
• State: .is-hidden, .is-active (implies JavaScript-trigger class changes)
• Theme: .theme-background, .theme-border, .font-1, .font-2

These types of classes describe their functionality and category. The rules of SMACSS are more "guidelines" and ways of thinking; look through the source of Pure CSS to see examples of SMACSS in action.

## Diving in

First, let's create a simple project directory using the HTML5 Boilerplate, and drop Pure in in place of Normalize.css. For this tutorial, we will be using Pure's YUI CDN link, <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.1.0/pure-min.css">. We'll also drop in Font Awesome, Twitter's icon font collection, and add a line to our CSS (instructions taken from weloveiconfonts). Throughout the markup, you will see icons defined with classes like "fontawesome-calendar".

We're ready to start using these tools in design! Let's start out by defining some skeleton CSS to hold our elements in. Let's put this in our 'body'.

We've decided to use a color palette we found on colourlovers called Yellow Tree Frog by user LJK.

For now, we are going to reference them in a comment at the top of the CSS file.

Tip: You might benefit from starting out by using the CSS color keywords and find/replace the hex codes later so you don't have to copy and paste the hex code constantly.

## Defining the Menu

PureCSS gives us a drop-in menu module that we can use to define a menu for our dashboard. We begin by using Pure's class-based module naming, following the guidelines of SMACSS.

Note that this menu isn't going to be functional for this tutorial. Viewing the page, we can see our menu has been created with default horizontal styling. Let's go ahead and change the background of the menu to a semi-transparent black.

Here, we see an adoption of the conventions used by SMACSS and Pure. We define a two new classes, 'pure-menu-blackbg' and 'pure-menu-fixed', and then the sub-styles needed to support that class. We also see some overriding of underlying classes (like the existing 'pure-menu' class to change to a lighter font weight).

## Creating a Dashboard Module

Next, we will create a dashboard module. We'll use some basic styles for our base-level dashboard "block" piece.

Now that we have this base module defined, we will extend this by creating new classes with the 'dashboard-piece' prefix. First, we will create background color classes.

Next, we will define the markup for the different dashboard pieces. We include an inline SVG of the Envato logo, which will stretch the entire width of the grid.

There's a lot to cover in this markup. First, we're using 'pure-u-1-3' (and other pure-u-* classes) inside a grid row element, 'pure-g-r'. These classes are a part of the Pure grid system. To understand these completely, check out the full YUI grid documentation; the basic concept is that the grid class pure-u-1-3 is 1/3rd the width of the full grid; a class of pure-u-3-4 would be 3/4th the width of the grid. The module with the logo in it has the class pure-u-1, which allows it to span the full width of the grid.

The other elements of the dashboard pieces are used to show different types of metrics. The following CSS rules are used to present these metrics and other internal parts of the dashboard pieces.

We also define the horizontal and vertical bar classes separately from the dashboard-content modules.

Finally, we use more of Pure's built-in modules to create a styled table for an "events" module, and we reuse the horizontal bar class for the weather module. We extend these classes with customized CSS as well. Here is the markup for the last two modules.

And the extended Pure table classes.

Lastly, we want to slightly pad the top of the weather module so that it fits more in line with the events module. We do so by simply utilizing the extra "weather" class and adjusting the top padding. Along with these last pieces, we'll add some simple footer styling to match the top nav menu.

## Extending Responsiveness

Pure comes with built-in responsive grid elements. They are defined with the following selector.

This is a tricky looking selector (take a look at The 30 CSS Selectors you Must Memorize for in depth explanations) which first points to direct children of ".pure-g-r" elements. Those children must then have a class attribute which starts with the "pure-u" prefix. Phew.

It points, for example, to this div:

This selector is used inside @media queries to collapse the grid. However, these unit elements collapse to 100% below 767px. We want to change this to allow the elements to collapse down to 50% between 480 and 767. We do so with the following @media queries.

## A Splash of JavaScript

In 'main.js', we will write some JavaScript to make sure all of the modules on a single row are the same height.

This JavaScript defines a function that loops through each of the elements with a class of "dashboard-piece" in each row, and subsequently finds the tallest module in that row. It then sets all elements in that row to the height of the tallest element in the row.

## Conclusion

This tutorial only shows some of the modules defined in Pure. With patterns like those used in this tutorial, you can create easily maintainable, readable, and scalable CSS. Using a small library like Pure allows you to leverage a powerful, tested, and well documented solution to common problems.

What else would you do with Pure? Let us know in the comments!