Coding a stats component
Overview of my approach to code a stats component for the Institute of the Environment and Sustainability's website based on the UCLA brand guidelines for the web.
I use it as a prototyping tool and reference for when I code the UCLA Institute of the Environment and Sustainability’s site.
I'm working with a few colleagues from across campus to make a bespoke UCLA design system designed to fit in the Bruin culture. This is a marathon not a sprint and so we take our time and talk through questions and issues with like minded designers and developers.
To me, the guidelines inspire creativity and embrace diversity. A brand doesn't mean we all look the same, but that we aspire to the adopt core values of excellance and inclusivity. The guidelines provides design constraints, so everyone starts from the same font family, color palette and logo lockup. How these design elements are arranged can and should vary. To developers this means there isn't one standard. Technology changes to quickly and there are too many use cases to only use one tool. We need to have multiple tools in our tool kit. Whatever technologies are in use around the world, someone is sure to be using it at UCLA. That’s ok by me. I do think we have an opportunity to build a design system that can support this diversity and that is simple by design.
For a living design system at UCLA I think it should be platform agnostic. I'm advocating that we create UCLA specific names. Names could be inspired from Bruin history and now that we're celebrating our centinnial seems a good time to begin. Over the next year or two we can create our own unique framework.
This post covers how my solution to building one component from the guidelines. I started from a shared a scss file for a stats block and I went from there.
I added a ucla-c
prefix to namespace the component.
Previously I added to the starter kit a modular scale for Helvetica, a color palette in multiple formats (hex, RGB and HSL as SCSS variables and CSS Custom Properties). I’ve recently added the first component called a stats block. I’ve only supplied my HTML and CSS, but on the IoES WordPress site I help our creative content editors by build simple data entry fields using the advanced custom fields. I held back the CMS code because there are many content creating systems (a new term for content management system) across campus and each developer can adapt the component as they see fit.
The original scss file wasn’t named spaced ucla-c-
and I took creative license to rename the class names and change css properties that had floats and px measures to fit the previous work I did based the CSS. In this post I include version one of the html and css along with my adaptions. I think we should adopt a name space unique for UCLA.
Of course there are many ways to code this block. My approach is only one solution. I prefer to use flex box and css grid and keep my markup concise. I didn’t want to deviate far from the original code shared so in a simple starter kit for UCLA sites, I posted two sample solutions. One uses BEM for the CSS with several HTML variations. The second example is my preferred solution to test and discuss with developers before implementation. I also share some links from the W3C spec and sample solutions from frameworks currently in fashion.
Example One
The first example uses BEM, <div>
s and <span>
s with several markup solutions and two ways to code the CSS.
HTML approach #1
HTML approach #2
HTML approach #3
CSS
CSS approach #1 uses floats.
CSS approach #2
Uses Flexbox and CSS grid
Example Two
A simpler solution uses one <div>
and a single class name .ucla-c-stat-block
. The <figure>
wraps the number or metric and <figcaption>
for the label and an optional link. The look remains the same as sample 1 which uses BEM, but I think is easier to understand and has better semantics.
HTML
Using figure and figcaption
I decided to use a <figure>
and <figcaption>
because the stat value is illustrative and the description provides further explanation. It appears in the flow of a page but could be moved away from the main flow. This follows the spec defining what the <figure>
tag can be used for.
From the W3C spec
The figure element represents a unit of content, optionally with a caption, that is self-contained, that is typically referenced as a single unit from the main flow of the document, and that can be moved away from the main flow of the document without affecting the document’s meaning.
Richard Clarke wrote about it on html5doctor.com back in 2010.
The
<figcaption>
element is optional and can appear before or after the content within the<figure>
.
No need to constrain your
<figure>
to images and code examples. Other content suitable for use in<figure>
includes audio, video, charts (perhaps using<canvas>
or<svg>
), poems, or tables of statistics.
And from Mozilla Developer Network
Usually a
<figure>
is an image, illustration, diagram, code snippet, etc., that is referenced in the main flow of a document, but that can be moved to another part of the document or to an appendix without affecting the main flow.
Another bit of interesting information about the figure element comes from HTML The Living Standard on whatwg.org
Finally, here are the typical default display properties for <figure>
and <figcaption>
.
Examples from Bootstrap, Foundation and Semantic UI.
Stats are fairly common on the web and most often used in dashboards and pricing tables. I haven’t found any definitive solution and provide samples and links here for further research and comparison.
Bootstrap
Closest thing in bootstrap to a stat component that I found was a pricing card. Bootstrap used <h1>
on the number value. I'd rather use a heading or paragraph tag than divisions and spans to provide some semantic meaning.
Foundation
Uses class names on <div>
, <span>
and <p>
tags with class names that are descriptive but only if you see the code.
Semantic UI
Semantic UI has a statistic block which uses <div>
on the number value and label. Again, the class names don't help with semantics.
Links
Conclusion
I think using a single class name on the parent of a component and style child elements is an elegant solution that keeps my markup simple and offers a nice balance of global and local effects.
What do you think? Let me know on twitter