jason lynes archives

case study

Hot and graceful jQuery dropcaps

Dropcaps are a staple of magazine and newspaper design, but aren’t always the easiest to implement on the web. Using this little jQuery and CSS tutorial, you can easily add hot little dropcaps to your web articles without bothering your developers. This method will also let you sleep at night knowing your caps will degrade gracefully, displaying just fine in browsers without CSS or JavaScript enabled.

Please note, this tutorial is for those who are familiar with jQuery and CSS. No basics taught here. Also nerd alert.

Here we have a flippin’ sweet dropcap on an article page for the new Combating Pornography website we’re building (launching early 2009).

Tadd recently noted that he’s been using some nice CSS 2 selectors in the new General Conference design. In his example, he’s styling the initial character of the article with CSS typography. Noble indeed.

However, I am more of a type snob and wanted my dropcap to be set in Caecilia, matching some other type found throughout this new site.

(Yes, this means saving 26 images of dropcaps in Photoshop. Luckily this took me all of 10 mins. As for translated sites, more on that below.)

So our task is to style the initial character with a background image, with hand-adjustable pixel spacing around each individual letter, and in a way that degrades as gracefully as Audrey Hepburn if she were a nerded-out website.

I also don’t want to bother the developers (who read this blog – hi Mindie!) to put a <span> around the initial character, and I really don’t want to beg them to throw a different class on that span depending on which letter of the alphabet that character is.

Sound like a daunting task? It’s not.

HTML it up

First let’s make sure the HTML is set up nice and easy:

Here we have the initial paragraph from Michael Gardner’s article on Hope, Healing, and Dealing with Addiction (here is the full comp so you have some context). No spans, no special classes. Easy fo’sheezy on the developers.

jQuery action

Next we hook up our page with some jQuery, and start the show.

<head>

<script src="scripts/jquery.js" type="text/javascript"></script>

<script type="text/javascript" charset="utf-8">
$(document).ready(function(){ 

});
</script>
</head>

Inside our jQuery block, we’ll first grab the first paragraph in our article (in my case, inside my “body” div), and add a class of “dropcap” so we can remove the indent and refer to it later:

$(document).ready(function(){ 
    p1= $("#body p:first");
    p1.addClass("dropcap");
});

Next, we need to grab the text from the first paragraph. We do this by taking the actual paragraph element from our p1 object, and asking it for its content using a standard JavaScript function, “textContent.” We also want to strip out any leading spaces or quotes, using some regular expression magic:

$(document).ready(function(){ 
    p1= $("#body p:first");
    p1.addClass("dropcap");
    p= p1[0].textContent.replace(/^\s+/, "").replace(/^"/, "").replace(/^'/, "");
});

Next we need the initial character from that paragraph (whic we know is alpha-numeric), using the standard function “charAt.”

$(document).ready(function(){ 
    p1= $("#body p:first");
    p1.addClass("dropcap");
    p= p1[0].textContent.replace(/^\s+/, "").replace(/^"/, "").replace(/^'/, "");
    c= p.charAt(0);
});

Now comes the money. In one statement, we will replace the html inside our jQuery object (p1) with a new block of code that wraps the initial character in a span, adds a class of “dropcap-” + the initial letter (don’t forget the lowercase), and adds a style with a background image for that specific letter. Check it.

$(document).ready(function(){ 
    p1= $("#body p:first");
    p1.addClass("dropcap");
    p= p1[0].textContent.replace(/^\s+/, "").replace(/^"/, "").replace(/^'/, "");
    c= p.charAt(0);
    p1.html('<span class="dropcap-' 
        + c.toLowerCase() 
        + '" style="background-image: url(images/drop-' 
        + c.toLowerCase() 
        + '.jpg)">' 
        + c 
        + '</span>' 
        + p.substring(1, p.length));
});

Beautiful. The resulting HTML:

CSS magic

The code above adds a specific class and (gasp!) an element-level style for one reason: keep it simple for the developers (and me).

By giving each letter its own unique class, I can adjust the pixel padding as I see fit. By defining the background image on the element level, I don’t need dozens of lines of code in my CSS file to define each letter class and each letter background. jQuery does it for me.

In our CSS file, I have the following block of code to manage the paragraph:

/* dropcaps */
#body .dropcap{
    text-indent: 0;
}
.dropcap-a, 
.dropcap-b, 
.dropcap-c, 
.dropcap-d, 
.dropcap-e, 
.dropcap-f, 
.dropcap-g, 
.dropcap-h, 
.dropcap-i, 
.dropcap-j, 
.dropcap-k, 
.dropcap-l, 
.dropcap-m, 
.dropcap-n, 
.dropcap-o, 
.dropcap-p, 
.dropcap-q, 
.dropcap-r, 
.dropcap-s, 
.dropcap-t, 
.dropcap-u, 
.dropcap-v, 
.dropcap-w, 
.dropcap-x, 
.dropcap-y, 
.dropcap-z {
    width: 75px;
    height: 80px;
    display: block;
    text-indent: -5000px;
    float: left;
    margin-top: 5px;
    background: transparent top center no-repeat;
}

.dropcap-a{ width: 95px;}
.dropcap-d{ width: 85px;}
.dropcap-h{ width: 85px;}
.dropcap-i{ width: 56px;}
.dropcap-j{ width: 56px; height: 85px;}
.dropcap-k{ width: 85px;}
.dropcap-m{ width: 100px;}
.dropcap-n{ width: 85px;}
.dropcap-q{ width: 85px; height: 90px;}
.dropcap-u{ width: 85px;}
.dropcap-v{ width: 85px;}
.dropcap-w{ width: 120px;}
.dropcap-x{ width: 85px;}
.dropcap-y{ width: 85px;}

I’ve removed the text-indent on this first paragraph, and defined the basic styles for each dropcap, including custom pixel widths for a few.

This method ensures that for anything not defined, like a Chinese or Spanish character, the styles won’t apply and you still have a perfectly fine-looking page no matter what your first letter is. It also means that if you were so inclined, you can define styles and images for any character (as long as your server / target browser supports those characters):

Not only is this saving me hassle, this is saving a decent amount of Java (or Ruby or PHP) code for our developers, and enables me to hand over an even more complete prototype.

Big ups to Pete Lasko, who nerded out with me and helped with some of the code.

posted by jason on Tuesday, Dec 02, 2008

Zoom far into a document in the new Photoshop CS4 and pixel lines automatically appear, a nice touch to help define specific colors and fine tune your designs. (Above is 18pt Caecilia LT Std at 3200%.)

posted by jason on Tuesday, Nov 25, 2008

Vintage RobFosterâ„¢ style illustrations by Patrick Leger. Above: “After the Heist”. Rob, I want to see one of these in your lounge ASAP.

posted by jason on Tuesday, Nov 25, 2008

Subtle marketing strategies from Threadless.

posted by jason on Monday, Nov 24, 2008

I’m going to stop listening to you because I disagree with your politics, even though you aren’t talking about politics

Today we received an email from a concerned subscriber:

It’s a very sad day – I have always found NorthTemple a great, entertaining, and informative read.

But, today I am removing you from my feed, and will no longer be sharing with my team.

I cannot reconcile the Mormon church’s views and directives on Prop 8 with my own, and refuse to support anything associated.

Good luck to all of you – you have a great team.

Northtemple is made up of 30 someodd designers who do work for the LDS Church, yes. But our About page has said for 2 years:

Northtemple.com is not an official publication of the Church of Jesus Christ of Latter-day Saints. The websites linked, opinions expressed, quotes cited, and articles written do not necessarily express the views of the Church. We bear sole responsibility for, and firmly stand behind, the content featured here.

A few points I wanted to make clear this morning:

  1. This is a design blog. We write about design.
  2. We rarely, if ever, talk about politics. The occasional Obama link has always had a design or tech element to it. We have never voiced an opinion on Proposition 8.
  3. The Church does not own Northtemple.com, and has no control over what we post. The site is not an approved publication of the Church, and is hosted and owned privately. We just all happen to work here.
  4. To reject Northtemple because of the Church’s stance on Prop 8 is like refusing to watch the Utah Jazz because a Mormon owns it, or stay in a Marriott hotel because a Mormon owns it, read any of the Twilight books (or watch the movie this weekend) because a Mormon wrote it, listen to Glenn Beck or Gladys Knight, or watch any of the many professional sports teams that LDS members play for.

While we respect the political views of our readers, we just don’t care about them on this blog. If you care about design, read us. If not, don’t. It’s a design point, not a political one.

posted by jason on Wednesday, Nov 19, 2008

“It’s so hard to find things.
I’m finding it hard to not blame you.”
My wife Caryn, experiencing the joys of using lds.org.

posted by jason on Tuesday, Nov 18, 2008

Motrin provokes mommy bloggers with an insensitive ad, gets nailed on twitter, removes the ad and issues and apology, and it all happened over the weekend. The best part – when a few bloggers contacted the advertising company, they had never heard of twitter and were totally clueless as to the online backlash.

Sounds eerily similar to a few of the ad agencies we’ve worked with..

posted by jason on Monday, Nov 17, 2008

Obama might have to give up email:

The president’s e-mail can be subpoenaed by Congress and courts and may be subject to public records laws, so if a president doesn’t want his e-mail public, he shouldn’t e-mail, experts said. And there may be security issues about carrying around trackable cell phones.”

posted by jason on Monday, Nov 17, 2008

You’d do yourself right to pick up this new offering from mountain folkster Ray LaMontagne, called Gossip in the Grain. It’s the 3rd CD of his in high rotation around here, following Trouble and Till the Sun Turns Back.

He’s packed this thing full of his trademark raspy soul, with some upbeat funk thrown in (“You are the best thing”), a full out confession of love to Meg White (“Meg White”), a little banjo plucking action (“Hey me, hey mama”), and some heel-tappin’ blues, complete with reverb’d harmonica (“Henry nearly killed me [It’s a shame]”).

You need this. Buy now and save yourself future embarassment.

posted by jason on Friday, Nov 14, 2008

Obama announced he will be holding weekly YouTube fireside chats, what his people are saying is “just the beginning of the digital, transparent presidency.” Related: Obama’s staff photographer’s behind the scenes photos of election night, which has just over 2 million views in 10 days.

posted by jason on Friday, Nov 14, 2008

Jim Coudal is twittering an experiment called startwithagrand – he started yesterday with $1000 worth of stock, and he gets “one trade per day. One stock in portfolio. No tears.”

posted by jason on Thursday, Nov 13, 2008

Carsonified launches their site for the Future of Web Apps – Dublin.
They’ve taken a page’s worth of information and blown it up to three and a half pages of design. Web 2.0 goodness, or a bloated excuse for a brochure? I can’t decide if I like it or not..

Just a note – I love these Carson workshops. I don’t exaggerate when I say they are the most professionally produced conferences out there. This note isn’t meant as criticism of their biz, just a note on the design direction of these latest sites of theirs.

posted by jason on Thursday, Nov 13, 2008

How to spot a hidden handgun, a sweet infographic from Megan Jaegerman, as analyzed by Ed Tufte.

posted by jason on Thursday, Nov 13, 2008

posted by jason on Wednesday, Nov 12, 2008

Target’s new gift cards are also 1.2mp digital cameras.

posted by jason on Wednesday, Nov 12, 2008

northtemple journal of design  ~  November Issue

The Designer's Sixth Sense

Exploring the inner workings of a designer’s mind is no picnic. Stop over-analyzing and go with your gut.

posted by jason on Monday, Nov 10, 2008

CD cover for Focus Jazz, designed by Barral Fabien

posted by jason on Friday, Nov 07, 2008

How the son of a Kenyan economist became an Illinois Everyman.
A New Yorker piece from 2004 on the newly elected.

posted by jason on Friday, Nov 07, 2008

“Doing 4% less does not get you 4% less.
Doing 4% less may very well get you 95% less.”
Seth Godin, The sad lie of mediocrity

posted by jason on Friday, Nov 07, 2008

Election coverage

Here are some handy sites for those of us tuned in tonight:

posted by jason on Tuesday, Nov 04, 2008