About this site's lack of design: Yes, it's supposed to look this way — I'm using a sandbox theme for WordPress (see it on GitHub).

Dan Rubin's SuperfluousBanter

Design, random musings, and the Web. Since 1977

Archive for August, 2008

Regex Patterns for Single Line CSS

Friday, August 29th, 2008

Update: You can now down­load the Text­mate macro file rather than record­ing your own (skip to the down­load »).

There has been plen­ty of dis­cus­sion about the pros and cons of sin­gle-line style sheets, and I’ve been includ­ing them as an option when teach­ing CSS man­age­ment and orga­ni­za­tion in my Web Design World pre­sen­ta­tions in Chica­go, Seat­tle, and lat­er this year in Las Vegas (at Web­Builder) and Boston.

It’s a matter of preference

As a fel­low Side­bar-ian (Side­bar­bar­ian?) Steve has been try­ing to con­vince me to use the sin­gle-line approach for a while of course, and Bryan and Jon have also become fans of this for­mat­ting style for their own work. Although they are enam­ored with it, I haven’t tak­en to it yet, still pre­fer­ring to write my style sheets using the “nor­mal” indent­ed for­mat­ting most of us are used to.

Now, before any­one gets their knick­ers in a twist about why they love, hate, “can’t live with­out” or “will die before they try” sin­gle-line for­mat­ting, let’s just take a step back and remem­ber one thing: it isn’t any­thing spe­cial, just an alter­nate for­mat­ting style that does­n’t affect the way the brows­er inter­prets the style sheet one iota. It’s a per­son­al preference—remember that before jump­ing on or off this par­tic­u­lar bandwagon.

Always keep your options open

Now that I’ve got that out of my sys­tem, let’s talk prac­ti­cal­i­ty: there are indeed ben­e­fits to be had when using sin­gle-line CSS formatting—for exam­ple, I find it can make a dif­fer­ence after a project has been com­plet­ed, at which point I’m usu­al­ly more inter­est­ed in visu­al­ly scan­ning a style sheet for the selec­tors first, and then for a par­tic­u­lar prop­er­ty I’m inter­est­ed in edit­ing. This is where I’ve found sin­gle-line for­mat­ting can come in handy.

But my editor already does that!

This is the point where some peo­ple will start going on about how you could just use Text­mate’s “fold­ings” fea­ture to get the same visu­al result (with­out alter­ing your file), or how CSS Edit has a handy list of all the selec­tors in a col­umn on the left side of its win­dow, or that you could always use your edi­tor’s “find” com­mand and search for the selec­tor you want to edit. Yet while those are all per­fect­ly log­i­cal, sane sug­ges­tions, they don’t account for flex­i­bil­i­ty and choice.

Just another way of doing things

Much like Jon Hicks with his harem of web browsers, I tend to be a bit of a “text edi­tor polyg­a­mist”, bounc­ing from Text­mate to CSS Edit to Coda to BBE­d­it to Trans­mit’s text edi­tor and a host of CLI edi­tors, most­ly depend­ing on my mood (though some­times con­tex­tu­al if I’m at a com­put­er that does­n’t have a par­tic­u­lar application—a Win­dowsXP box with noth­ing but Notepad, for instance). It’s the times when I’m using an edi­tor that does­n’t have “fold­ings” or pret­ty columns of selec­tors that I start to appre­ci­ate sin­gle-line CSS when mak­ing quick edits, so I’ve start­ed con­vert­ing style sheets to a “sim­ple” sin­gle-line for­mat (with­out the left-aligned tab blocks to start each rule’s prop­er­ties) once they are ready to go live.

Patterns fit for a kilt

Edi­tors like Text­mate and BBE­d­it have built-in com­mands for for­mat­ting (the stan­dard, mul­ti-line approach) or com­press­ing (the entire style sheet on one line, osten­si­bly to reduce file size by strip­ping white space) CSS, but no way to con­vert to sin­gle-line for­mat­ting and Text­mate’s “For­mat CSS Com­pressed” bun­dle can for­mat your stylesheet to a sin­gle line per-rule, though it’s all squished togeth­er, mak­ing it dif­fi­cult to scan due to a lack of white­space. Con­vert­ing a style sheet by hand every time would be much too time-con­sum­ing to both­er with, but that’s where reg­u­lar expres­sions come to the rescue.

In Text­mate, you can record a macro using each of these regex pat­terns as a sep­a­rate step (I’m sure oth­er edi­tors have a sim­i­lar fea­ture, so please feel free to post details in the com­ments below). This allowed me to cre­ate a “For­mat CSS Sin­gle-line” com­mand, com­plete with a key­board short­cut, which enables an easy switch between mul­ti- and sin­gle-line formatting.

Unfor­tu­nate­ly, as of this writ­ing Text­mate macros can­not be export­ed and shared For those using Text­mate, get­ting the macro is a sim­ple mat­ter of down­load­ing, expand­ing and dou­ble-click­ing this file:

While sim­i­lar to “For­mat CSS Com­pressed”, this macro retains a sin­gle blank line where your orig­i­nal con­tained two or more blank lines (help­ful if you group your rules), and adds white­space that match­es my stan­dard for­mat­ting pref­er­ences (which I find makes it eas­i­er to scan quick­ly). To switch between each for­mat­ting style, just run each com­mand in turn (via the Bun­dles menu or the key­board shortcuts).

How­ev­er, that would­n’t be much use to every­one who does­n’t use Text­mate, so here are the respec­tive groups of regex I used for the conversion:

\n{3,}
\n\n

[ \t]+
 
(?m)([;:])\s+
$1

\s*}
 }

\s*{\s*
 { 

[ \t]*,[ \t]*
,

@import(.*?);
@import$1;\n\n

What’s missing

In Text­mate and BBE­d­it I can return to mul­ti-line for­mat­ting with a sin­gle com­mand, but that might not be as sim­ple in oth­er edi­tors. What I’d love to see is a pair of shell scripts that con­vert from mul­ti- to sin­gle-line and back, and pos­si­bly a web-based proces­sor that does the same (paste your style sheet into a textarea, select your for­mat­ting, hit “process” and the script pro­duces the result). If any­one would like to take on those tasks, I’ll hap­pi­ly update this post to link to the prod­ucts of your labor.

And in the end…

If you’ve nev­er tried sin­gle-line for­mat­ting, this makes it easy to exper­i­ment with­out com­mit­ting your­self (and I do rec­om­mend giv­ing it a try—you may be sur­prised once you’ve worked with it a few times).

Ulti­mate­ly, because I can return to mul­ti-line with a sin­gle com­mand my pri­ma­ry text edi­tor should I ever feel like it, automat­ing the switch from mul­ti- to sin­gle-line is a con­ve­nient way to get the ben­e­fits of sin­gle-line for­mat­ting with­out back­ing myself or my clients into a for­mat­ting corner.

Categories:

67 Comments

Simple CSS Hover Tab

Tuesday, August 19th, 2008

Update: The orig­i­nal edit of this post and demo file did­n’t work in IE6/7. See the com­ments for my quick expla­na­tion of the fix (the demo now works in FF2/3, Safari 2/3, Opera 9, and IE6/7).

After throw­ing togeth­er a quick lit­tle hover/tab wid­get for my pre­vi­ous arti­cle, I thought I’d share how it was made, in case you find a need for it someday.

Bulletproof lists

The wid­get in ques­tion is just a sim­ple unordered list, with each list item con­tain­ing an anchor and an image—we want the images in this case because I want them to dis­play in my RSS feed and for any­one who can’t (or choos­es not to) view the styled ver­sion of this site.

Note: Feel free to ref­er­ence images in the stylesheet rather than inline if that suits your purposes.

See the wid­get in use in my pre­vi­ous arti­cle, or check out this demo.

The code

First, the markup (with URLs trun­cat­ed to save trees):


Sim­ple, unclut­tered, uncomplicated.

Next, the CSS:

ul#hover-tab-thingy {
  position:relative;
  padding:0;
  width:500px;
  height:498px; }
#hover-tab-thingy li {
  float:left;
  list-style:none; }
#hover-tab-thingy li a {
  float:left;
  padding:9px 21px;
  background-color:#eee;
  color:#999;
  font-size:9px;
  text-align:center;
  text-transform:uppercase;
  border-right:1px solid #fff; }
#hover-tab-thingy li a:hover {
  background-color:#f60;
  color:#fff; }
li#one a,
li#one a:hover {
  background-color:#e5e5e5;
  color:#555; }
#hover-tab-thingy li a img {
  position:absolute;
  left:0;
  top:30px;
  width:500px;
  height:460px;
  clear:left;
  margin-left:-9999px;
  padding:1px;
  border:3px solid #e5e5e5; }
li#one a img,
#hover-tab-thingy li a:hover img {
  margin-left:0; }
li#two a:hover img,
li#three a:hover img {
  border-color:#f60; }

This is all fair­ly straight­for­ward, so here are the high­lights that may help when dupli­cat­ing this on your own:

  1. The entire idea is that you have tabs that are each asso­ci­at­ed with con­tent (images in this case) which are made vis­i­ble when the user hov­ers over the tab. There are more things you could do with this, but that’s your job, grasshopper.
  2. The tabs are float­ed; the con­tent ele­ments (img in this case) clear the floats.
  3. The con­tent ele­ments are set to position:absolute, so they can appear in the same loca­tion for each tab. To accom­plish this, the ul is set to position:relative (in short: an absolute­ly posi­tioned ele­ment will be posi­tioned rel­a­tive to its near­est posi­tioned ancestor—see Doug Bow­man’s great write up for more), and it’s prob­a­bly a good idea if the dimen­sions of your ul (the con­tain­er for your con­tent) have a lot in com­mon with those of your con­tent elements.
  4. IE6 has a prob­lem revert­ing ele­ments that set display:block on :hover to their orig­i­nal state (e.g. display:none). To counter this, use a neg­a­tive left mar­gin as the default posi­tion­ing, and then set margin-left:0; on the hov­er state, which works in all mod­ern browsers.
  5. The width and height is spe­cif­ic to my exam­ple (the dimen­sions of the images I used), dit­to for the padding on the tabs and the top posi­tion­ing on the img ele­ments (to push them below the tabs). Bend them to your will.

Wrap-up

This may be some­thing that you’ll find use for on a reg­u­lar basis—one of those tiny snip­pets of reusable “stuff” that you’ll be glad you don’t have to type every time.

Categories:

15 Comments

I Need Some Spacing

Tuesday, August 19th, 2008

Way back in June over on Sub­trac­tion, Khoi pre­sent­ed a tweaked ver­sion of the Gmail inter­face (and then a fol­low-up arti­cle regard­ing the feed­back on the first), improv­ing spac­ing and align­ment by mak­ing a few small but sig­nif­i­cant changes overall:

…by nor­mal­iz­ing the space between like ele­ments, align­ing ele­ments along sim­i­lar spa­tial planes, mod­er­ate­ly increas­ing the space between stacked items and pay­ing atten­tion to how ele­ments are framed by neg­a­tive space, we can get what is, in my opin­ion, a sig­nif­i­cant­ly more attrac­tive Gmail inter­face.” Khoi Vinh

This is some­thing I’ve want­ed to see for a while in Gmail, and in fact had pre­sent­ed a few exam­ples last year (July 2007) as part of my inter­view process at Google (I real­ly must write about my series of amaz­ing job inter­views last sum­mer, but I’ll leave that for anoth­er time). After reading—and commenting—on Khoi’s post, I had intend­ed to pub­lish my vari­a­tion on Google’s mid-2007 inter­face, but as the ded­i­ca­tion to blog­ging con­tin­ues to elude me, this too fell a few pages back in the Mole­sk­ine to-do list—until now.

The Setup

I took a slight­ly dif­fer­ent approach than Khoi, though my goals were similar:

That last goal was impor­tant and act­ed as a guide to the rest: I want­ed to present improve­ments that could be made right away with­out caus­ing a neg­a­tive reac­tion among Gmail users.

Rather than adjust the line height of the main mes­sage area, I decid­ed to bring as many of the oth­er ele­ments in line with that as I could. You’ll notice spac­ing improve­ments with­in each line how­ev­er, reclaim­ing some poor­ly used hor­i­zon­tal space. Like Khoi, I felt the mes­sage lines were a bit crowd­ed, so to give the impres­sion of more room in those lines, I removed the grey hor­i­zon­tal lines between each item and allowed the back­ground col­or to bleed through (all they need is some separation—further empha­sis on those lines just adds weight). The result is that it feels like there’s a bit more space, with­out actu­al­ly adding any.

The main nav­i­ga­tion lost its under­lines (again, remov­ing unnec­es­sary visu­al weight—the “this is a link” cue is super­flu­ous here) and gained space (match­ing the base­line of the mes­sage list). The “Com­pose Mail” link got an extra high­light by way of a back­ground; the extra padding makes it the only nav­i­ga­tion item that does­n’t align with the base­line, but that also gives it a lit­tle more con­trast ver­ti­cal­ly, thus it stands out more with­out hav­ing to scream for atten­tion. Less impor­tant text links with­in the mes­sage area were made slight­ly small­er (but kept their under­lines), and “Mark as read” was pulled out of the drop-down menu and giv­en its own but­ton: I had spent a week or so watch­ing peo­ple use Gmail before doing this exer­cise, and that was the sec­ond most fre­quent action users need­ed (right behind “Delete”) but was hid­den in the drop-down.

Final­ly, all round­ed cor­ners were cleaned up a bit, foot­er text was aligned to the base­line, and the mes­sage list was enclosed on the right side (maybe it’s just me, but it’s always bugged me how those lines just run right to the edge).

Reveal Already!

I’m still hap­py with how this turned out a year lat­er, and it was well received by the Google inter­view crew. Com­pare the before and after images, and you’ll also notice that the improved spac­ing in this exam­ple actu­al­ly reduces the over­all height of the page, allow­ing two addi­tion­al mes­sage lines to fit in the space of the original.

Both Khoi’s and my realign leave almost all the orig­i­nal ele­ments of the inter­face intact—this is very impor­tant when mak­ing changes (even if only sug­gest­ed ones) to such a pop­u­lar appli­ca­tion, and the same goes for any prod­uct, pub­li­ca­tion or web site that is part of a per­son­’s dai­ly routine.

Whether Gmail incor­po­rates any of these sug­ges­tions is out of our hands, but it’s still nice to have the chance to compare.

Note: many changes have already been made to the Gmail inter­face over the last year, and even in the last few months since Khoi post­ed his exam­ple. Not all are improve­ments, but at least some­one’s mak­ing an attempt.

Categories:

13 Comments