Font-based Digital Clock Theme Using Only CSS

Ever since Daniel posted his binary mod of my digital clock, I’ve been thinking about other ways to push the limits of the clock using only CSS. At JQCon, I also talked to a few folks between sessions who had wondered the same thing. So, I’ve decided to give it a go.

One of the first ideas I had was to try to use fonts instead of the LED divs to show the clock digits. So, here you go.

It turned out to be fairly easy, and required zero changes to the Javascript view controller. The view controller simply passes the same messages to the view as it did before. In other words, the view controller relies on a message-passing-based view API to which the OOCSS responds. That API is unchanged in this version. To verify, you can hop on over to Daniel’s binary mod, which references my JS directly, and see that it still works.

It also required only superficial changes to the HTML:

  1. I added the button for the new theme in the row of theme buttons.
  2. I added “hour” and “minute” classes to the digits representing hours and minutes. This change was not necessary to support the new font-based theme, but makes the hours and minutes consistent with the seconds, which already had the class “second”.

The key changes were, of course, in the CSS, and here are some of the most relevant bits.

The comments pretty much say it all, but basically it hides the LEDs and then uses :before content to inject the font-based digits.

One thing I hadn’t thought about before I actually ran it the first time, was that the “1” digit is much thinner than all the others, so I had to forcibly set a specific width for it (in both hours/minutes and the smaller seconds). Without that specialization, the clock looks too sparse when there is a “1” (or several “1”s) being displayed. Yet another win for the OOCSS base and specialization pattern.

I think this theme looks pretty good (although I’m still partial to the LEDs), but if you have suggestions for how to tweak it, I’d love to hear them. Also, if you have an idea for how to push the envelope, feel free to leave a comment, or tweet it up!

Stay tuned for more envelope pushing …

OOCSS for Javascript Pirates at JQCon 2010

I had a lot of fun presenting OOCSS for Javascript Pirates with John Hann at JQCon 2010 in Boston this past weekend.  I came away feeling like the talk was very well received, and that people felt it was helpful.   The slides and the digital clock, including the jqcon-explode mod that we coded live during the talk are all up, as is Daniel Lamb’s binary clock mod.

If you want to read even more about OOCSS, you can also check out a few of our blog posts on the subject:

One of my favorite things about the conference was, as Paul Elliot (@p_elliott) put it, the hallway track.  I got to meet and talk with so many cool people between the sessions.  John and I both had quite a few people tell us (in addition to a few good pirate jokes) that they had been thinking and doing some of the things from our talk, and they were glad to hear that other people were thinking many of the same things as validation that these techniques work.  Hearing that was good validation for us as well.

I really appreciate all the feedback so far on the presentation, and on the digital clock as well, on which I have received many compliments.  Thanks, everyone!  If you haven’t already rated our talk, John and I both would really appreciate your feedback over at SpeakerRate

I also have to say that overall, the conference showed how much energy there is in not only the jQuery community and the larger Javascript community, but also in front-end engineering as a whole.  It’s a great time to be a front-end engineer.

Thanks to the jQuery team and the conference organizers for putting together and running an excellent conference.  I had a blast.  Nice job all around.

The Power of MVC: Binary Clock Mod

When I created my digital clock, one of the things I wanted to show was how OOCSS and MVC could be used to provide a clean, clear separation of concerns between the View, i.e. HTML and CSS, and the View Controller, i.e. Javascript. Along those lines, I had always intended for it to be possible to slap a different View onto the clock with no mods to the View Controller, but the proof is in the pudding, right?

Along came Daniel Lamb, who put that to the test, and created his spiffy binary clock mod.

He created a slightly modified View structure for the binary clock display in his HTML, and then cleverly applied OOCSS principles to inherit from my original OOCSS. Most interestingly, though, notice that he didn’t change a single line of code in the JS View Controller. In fact, he referenced my JS View Controller directly in his script tag. Because CSS classes are used as a message passing mechanism, and his View responds to the same messages, the binary clock works perfectly.

I’d like to thank Daniel for creating such a cool mod and perfectly illustrating the powerful separation of concerns that OOCSS and MVC can provide.

The OO in OOCSS, and Direct Style Manipulation

In my recent post, Building a digital clock with OOCSS and MVC, I mentioned that I believe direct style manipulation is not a good engineering practice, and then promptly punted on explaining why until a later post. This was going to be that post, and then John Hann went and laid out many of the points I was going to make.

I have more I’d like to add, so jump over to his post, cujo.js — OOJS, OOCSS, and OOHTML — Part 1 (OOCSS for Engineers) and come back. He specifically touches on direct style manipulation in the sections OOCSS State and OOCSS decreases risk.

The major points he makes are that direct style manipulation:

  1. leads to complex branching code as runtime specialization and state inheritance becomes wider and deeper, as it does in real applications,
  2. puts presentation logic in Javascript, thus thwarting many benefits of separation of concerns and expertise among the team of CSS designers and Javascript engineers,
  3. tightly couples things that shouldn’t be,
  4. is usually less cpu-efficient than fully leveraging a good browser’s CSS engine,

These are great points, and I agree with them.  I’d like to expand on #s 1 and 2 a bit, and talk about a few other reasons in the context of the “OO” in OOCSS.


Object-oriented means “with a focus on objects”. It is a way of thinking about a problem and how to structure potential solutions. There are programming languages, such as Java, C++, C#, and even Javascript, that provide features to make it easier to apply and enforce OO principles, but a good developer can apply these principles in any language.

Some of the fundamentals of OO are abstraction, inheritance, polymorphism, and encapsulation, among others. Yeah, they’re all 30 point scrabble words, but more importantly, they are time-tested software engineering principles.

If OO means “with a focus on objects”, it seems logical to say OOCSS means, “with a focus on CSS objects”, and I believe there is a huge amount of value to be had in thinking of HTML and CSS as defining View Objects. I’d love to write about how various OO principles apply to OOCSS, and maybe someday soon I will, but for now, I’d like to look at how direct style manipulation violates two of them in particular: inheritance and encapsulation.


I previously wrote about the power of ancestor specializations and state changes affecting changes in descendants, and John goes into even more detail about it. Part of the reason it is so powerful is that it works in harmony with the “C” in CSS, the Cascade.

Using direct style manipulation logic essentially moves specialization and state inheritance from the CSS cascade to procedural Javascript. I think this is bad for two reasons:

  1. Duplicating the cascade moves the mechanism from the browser’s super-fast style engine, to the fairly-fast-but-way-slower-than-the-browser Javascript VM. Don’t think there’s a difference? Check out scripty2’s comparison of hardware accelerated CSS3 transitions vs. Javascript-driven animation

  2. Procedural code is less-easily checked by IDE’s, and is more risky to change than declarative CSS rules. I’d also argue that a well-organized set of CSS rules will visually communicate the cascade, and thus specialization order, more quickly and clearly than procedural branching. To draw an analogy with another popular OO language, which is faster and easier to get right: declaring a Java subclass via “class MySubclass extends MySuperclass” and letting the compiler do the heavy lifting, or writing the Java code that generates the bytecode for MySubclass?


Encapsulation is the principle of bundling the state with operations that retrieve and modify that state, as well as the idea that only those operations should access the state directly. To put it in terms of objects, an object is responsible for maintaining and controlling access to its own state by exposing only those operations which other actors are allowed to perform on its state.  The other actors in the system must send messages (via exposed operations) to an object to request state changes. The object itself elects how to affect the state change, or even whether to affect it at all.

Without this access control and message passing, it would be much easier to “reach in” and alter the internal state of an object, potentially corrupting it if you don’t understand all the intricacies of its invariants. With access control, an object is protected against corruption, and your application is protected against a corrupted object wreaking havoc.

Most Object Oriented programming languages have built-in mechanisms for declaring access control and enforcing encapsulation, e.g. public, private, protected, and default or package-level access in Java. Even in Javascript, which is a much more malleable, you can use closures to achieve private encapsulation—different mechanism, similar effect.

There is no encapsulation mechanism built into HTML or CSS. The only mechanism that exists is engineering diligence.

Encapsulation, OOCSS, and View Objects

The objects defined by OOCSS are View Objects. The HTML node ancestor/descendant relationships, in conjunction with the OOCSS specializations and state, define these objects. Their “state”, in the OO/encapsulation sense, is their style. For example, consider an HTML/OOCSS View Object that is a stylized button. It’s encapsulated state may contain height, width, background-color, background-image, background-position (maybe using CSS sprites for button states), borders margins, padding, etc. These were probably carefully crafted by a CSS designer to produce a button that looks great and has interesting and useful visual cues on hover, when pressed, etc.

Javascript View Controllers contain logic about when View Objects should change state, and to what states they should change. When View Controllers use direct style manipulation, they are “reaching in” and directly changing the encapsulated state of View Objects, potentially corrupting their presentation state by breaking the layout and presentation invariants setup by the designer.

With enough time and care, a JS engineer could certainly duplicate the invariants across some or all possible presentation states, such as, in the case of a button, idle, :hover, and :active, but then would also have to account for other axes of change, such as browser differences (e.g. box model, rgba or hsl colors, opacity, transitions, etc.). The conditionals in the Javascript would start to add up, and probably produce horribly unmaintainable code. John showed in his simple example and the text that follows, how the branching could get to O(n^m) complexity.

Doing so would also spread the presentation details out into at least 2 places, the CSS and the Javascript (or more if the presentation is being modified in several places in the JS!). Each time the presentation needed to be modified, it would require looking in both places, and would probably require involving both the CSS designer and the Javascript engineer.

I’ll also point out again that this kind of conditional logic is essentially duplicating the cascade, which is a bad idea for the reasons I listed above.

OOCSS: The Right Tool for the Job

CSS has a powerful inheritance mechanism in the cascade, and its declarative style, IMHO, provides a simple and expressive way to setup presentation across many view states.  It is, in that regard, a declarative language for presentation state machines. The “OO” in OOCSS is a powerful way of thinking about HTML + CSS as View Objects, and gives designers and developers the right tools to declare and manage them.

Our New Fountain

We stopped by Gardener’s Eye, one of our favorite local shops, in the
early afternoon today and came home with this great cast concrete

Some Fun Cruisers at the Bike Shop

My dad and I stopped by Dirty Harry’s in Verona today because I
noticed they had a Trek belt-drive bike in the window a few weeks ago,
and I wanted to try it out. Unfortunately, it was already sold and
gone. So we just looked around for a bit, and noticed they had some
really cool cruisers.

Oh, and an $8k Specialized S-Works. Ridiculous. Awesome, but
ridiculous. I think I like the cruisers better, anyway.


Building a Digital Clock With OOCSS and MVC

I’ve been thinking a lot about OOCSS and MVC-in-the-browser over the past months. These certainly aren’t things I invented, but what I found is that my own philosophy of building applications for the web had evolved to include these concepts. It was only later that I learned that some really smart folks had also been thinking about these things, and had given them names.

So, I created this digital clock app as a simple example of some of the concepts I have been applying to build apps with OOCSS and MVC.

Very shortly after that, I got a walkthrough of John (unscriptable) Hann’s ambitious cujo.js project, and I was blown away by two things. First, he and I had basically come to believe many of the same things about applying OOCSS and MVC, and second, he had actually wrapped those things up in an incredibly simple and intuitive API inside cujo.js.

Let’s get down to business, and look at a few of these techniques in practice. I’ll hope that some theory will fall out of it as I write.

“Is A” and Specialization

Here’s a bit of HTML from the clock.

Some of the classes here set up “is a” relationships. Even though the order of classes in HTML doesn’t matter, I’ve arranged them left to right from general to specific, because I think that makes the most sense. The node “is a” slot—an admittedly awful name for an area in which the clock will display something. Also, it “is a” digit, which in this case, is a specialization of slot that will display a digit.

Farther down, there is another specialization of digit:

This node is still a digit, but more specifically, it’s a second, as opposed to a minute or hour. Looking at the rendered clock, you can see that seconds are smaller than minutes and hours—the presentation of seconds has been specialized to be smaller.

States, Views, and View Controllers

If you watch the DOM while the clock is running you’ll notice the digit nodes getting the classes d0 through d9. Obviously, this is driven by Javascript. That Javascript is acting as a View Controller. The digit is the View, and the Javascript driving, or controlling, it is the View Controller.

The classes d0 – d9 represent the possible states that the digit may be in. By changing the class, the View Controller is telling the View to change state. The View still “is a” digit, but it has changed state, for example, from a zero to a one. I guess you could say it “was a” zero and now it “is a” one, and that it’s actually mutating to another specialization rather than changing state. I think that’s a reasonable way to think about it—it’s just not how my brain works, so for me, it’s state.

The digits and the state transitions manifest themselves in the browser with the help of CSS. Here’s a bit of the CSS.

This CSS describes what a digit looks like when it’s in state d0 and d1. In addition to describing the resulting state’s presentation, it is also describing the state transition itself—that is, how (in the visual sense) the digit moves from one state to another using CSS3 transitions.

Specializations, State, and Ancestry

One thing that is subtle, but I feel is extremely important here, is that by changing the state of the digit, the state of the elements within the digit (i.e. the glowing bars) is being affected. There are no direct state changes to the bars, yet they are changing. The current state of each bar is defined by the hierarchy of classes above it in the DOM plus its own classes. Or, to put it in more general terms:

The current “whole state” (borrowing a term from a recent conversation with John) of a particular View is defined by the specializations and state of its ancestors plus its own specializations and state.

There’s no direct manipulation of nodes at the leaf level via Javascript. I’ll talk about why I believe doing direct style manipulation, such as $.css or, is not a good engineering practice in another post, but, the key here is that by simply issuing a state transition on an enclosing View, state changes can be affected on its sub-Views.

Let’s look at why that’s interesting and useful on a practical level.

Back to Practical

So, the clock has a Javascript View Controller telling the View to change state which consequently alters the state of its sub-Views, HTML which is defining the structure of that View, and CSS that is describing the presentation of the states and the transitions between them. IMHO, that’s a very powerful separation of concerns.

Imagine you wanted to change the look of the digits by giving the bars beveled ends as some digital displays have, or you wanted to make the entire clock larger or small, or size it using percentages instead of pixels, or introducing a radical new presentation and color theme. You would not need to touch the View Controller. There are several reasons that is a good thing, IMHO, two of which are:

  1. The changes could be made by the team’s CSS designer, without involving a JS engineer, and
  2. Changing procedural code, like JS, is typically more risky than changing declarative code, like CSS.

This separation of concerns provides similar benefits on the View Controller side. When I decided to add support for 24 hour display, all I had to do (ignoring adding the new View components for selecting 12 or 24 hour time, and storing the preference) was to make a small change to the hour computation in the View Controller Javascript, issue slightly different state changes for the hour digits, and ensure that the AM or PM elements are always in the off state.

I didn’t need to make any changes to the CSS or HTML. Engineers can craft the JS, and the designers can craft the CSS. Sure, you might play both roles, but that’s not the case with every team, especially in large, complex apps, with many Views, company wide design standards, branding, and a small army of awesome designers complimented by an equally awesome army of software engineers.

I am very excited about building apps using these techniques, and I am especially excited after seeing John’s work so far on cujo.js. If it turns out to embody these concepts like I think it will, it’s gonna be a very powerful platform. I’ll certainly be keeping an eye on it.

CSS3DC Gets a New Home, 24hr Time, localStorage, All in Less Code

I decided to give the CSS3 digital clock a new home, and in the process, couldn’t help but hack on it a bit more.  It has a setting for 12 or 24 hour time, and uses localStorage to remember both your 12/24 and color settings, so you can keep the time you want, in the color you want, whenever you want.  Despite all of that, the HTML, CSS, and JS are a bit lighter and more streamlined now as well.

Oh, and it looks great as an OS X dashboard widget.  You can use Safari’s web clip feature to snag it and put it on your dashboard!