Fun with CSS in WordPress

I just had an email exchange with an old friend and fellow web developer (and WordPress user) regarding some techniques for CSS trickery on home pages in WordPress themes. Up until this week, I had been running a version of my theme that just featured brief excerpts of articles on the home page. I was doing this by brute force in PHP, truncating the post text with the substr() function, and then cleaning things up using the strip_tags() function, which removes all HTML tags from a string.

It got the job done, but as he and I were discussing, it wasn’t pretty: it stripped out the “dangerous” stuff — that is, unclosed HTML tags (cut off during truncation) that would have screwed up the formatting of the rest of the page. But it also stripped out desirable styling (bold, italics, links) and paragraph breaks.

The ideal situation would be to have a way to show just the first two paragraphs of each post, retaining all of their original formatting. Of course, WordPress has a feature to handle this: if you put a <!--more--> comment tag in your post, your page template can truncate the post at that point, with a link to the single-post page to display the rest of the content. But I’ve never liked having to put that <!--more--> into my posts. I want a completely automated solution.

And then it hit me… this could be done with CSS. It took a little trial and error, but I came up with the following:

#content .entry p,
#content .entry h3
{
display: none;
}

#content .entry h2:first-child,
#content .entry h2:first-child + p,
#content .entry h2:first-child + p + p
{
display: block;
}

A few things to note:

  • This assumes that your entire loop is wrapped inside <div id="content">...</div>. You may need to come up with a specific ID to use just for this block in your index page, and be sure not to use that in your single-post page, or your posts will never appear in their entirety.
  • This also assumes that you’re using the WordPress convention of wrapping your posts in a pair of <div> tags with the attributes class="post" and class="entry" (though technically, class="entry" is the only one that matters here).
  • Your post title should be in an <h2> tag, immediately following <div class="entry">.
  • The first definition may need to be extended to include other HTML tags you want to hide on your index page. In this example, it’s only hiding content that is inside <p> or <h3> tags.
  • If you want to hide every HTML tag except the ones you explicitly specify, you could change the first block to #content .entry *, but keep in mind that will also remove styling like bold and italics, and it will remove links. Probably not what you want.

The specifics may vary depending upon how your WordPress theme is set up; I just know that with the way mine is set up, which pretty closely follows standard convention, this CSS worked to get the index page to list the posts and only show the first two paragraphs of each. (It also retained the images that I embed at the start of each post, and also retains any embedded video from YouTube or Vimeo, since — at least the way I insert them — those are not wrapped in <p> tags.

Note that all of the HTML content for each post is still loaded by the browser — we’re just using CSS to tell the browser not to show it on the page. This is not going to help with performance; it’s strictly aesthetic.

Incremental redesign

If you are the a regular reader of this blog, you may notice things look slightly different than they did before. The light blue striped background behind the right sidebar is gone; there’s navigation at the top; the left sidebar on the featured item on the home page has now become a standard element of all of the posts and, hey, the home page is now back to a standard one-post-after-another blog layout. Also, the sidebar widgets are different, and if you scroll waaaaaay down to the bottom, that’s different too. But since you’ve never bothered to do that before now, you probably wouldn’t have noticed anyway.

Incremental redesign seems to be more common with websites these days than the biannual complete overhaul we’ve grown accustomed to from sites large and small over the years. Why? Perhaps it’s a sign of maturity (of the designs, not the designers): designs stabilize over time as they’re refined based on user feedback. Perhaps it’s inertia: sites are so much more complex these days that, despite the benefits of semantic HTML and CSS, it can still be a massive undertaking to redesign a website from the ground up. And perhaps it’s strategic: designers have an idea where they’re going, but it can be jarring to users to have the proverbial rug pulled out from under them with abrupt and large-scale changes to a site’s design.

So, which is it in my case? Honestly, it’s probably a combination of all three. Anyway, I hope you like the refinements, and if not, feel free to let me have it in the comment section!

No design is good design

Design.

I’ve been noticing an interesting trend of late with blogs I find interesting: their design is minimal to non-existent. And sites that did have elaborate designs are severely scaling them back or removing them entirely.

A few examples:

Daring Fireball
Of course. This is one of the most “designed” of the sites I’m talking about today. It actually has color. Well, if you call gray a color.

43 Folders
Merlin Mann’s site may be the inverse of my own, and not just in name. It was once loaded up with “clever” design elements suitable to its “get your (creative) work life organized” philosophy: manila folders, paper clips, etc. But in its latest incarnation it’s stripped down to a bare minimum of design. The only remnant of the old look is the red color of the title at the top of the page, and outside of images and a subtle yellow highlight hover state for links, that’s the only color on the site.

rc3.org
Rafe Colburn’s site is the very definition of minimalist design. White background, black text. But clearly a lot of thought went into the choice of font (yes, even though it’s Helvetica), relative text sizes, and the sporadic use of shades of gray for things like block quotes.

Subtraction.com
Khoi Vinh’s site is masterfully designed, even though (or perhaps specifically because) it’s black-and-white-and-Helvetica-all-over. Oh, except for the slap in the face of bright orange when you mouse over a link. Pow! Bort! Mint! Snuh! (That one’s for CheckersSpeech.)

Jeffrey Zeldman Presents The Daily Report
Zeldman’s site is an explosion of designy-ness compared to the others on this list. But compared to something like Web Designer Wall it’s strikingly spartan, and deliberately retrograde. (And, in trying — unsuccessfully — to find his old post where he discussed this latest incarnation of the site, I discovered that he also covered this exact topic six months ago. Looks like I’m late to the party, as usual.)

If less is more, then none is all.

Sneak peek at a new design

Sneak peekYes, it’s only been about 10 days since my last redesign, but I’m already at it again. I always knew that the current design was little more than a palate cleanser… or a rebound relationship.

Ultimately, what it came down to for me was that I just hadn’t put the kind of care and attention to detail into the design that I really want. With blogs it’s easy to put 99% of your design energy into the header, and then kind of just let the rest of it fall together in the default structure provided by the blog software. In other words, a long, undifferentiated stream of posts on the home page.

Well, I’m trying to give things more structure, as well as making the new design both more functional and more fun. And yes, I’m reusing every design trend from 2007. Is it too soon for the shiny floor effect to seem “retro”? Hopefully the sparkles convey an adequate level of irony. No, they won’t be animated.

Due to the level of attention I’m giving this design, I’m not going to be able to finish it in an evening like most of my past redesigns. But I’m too excited about it to hold back just a teaser (the screenshot detail shown here). Plus maybe this will keep the pressure on me to actually finish it.

Update: “Is this it?” Yes and no. I’ve gone ahead and rolled out the new look and feel with the old page structure. So it looks like the new site design, but it’s not. The changes that remain to be made are mostly on the home page, and involve significant modifications to the WordPress theme templates. It’s another level of tinkering under the hood than the superficial design changes I’ve rolled out up to this point. And I want to take the time to get it right. I’d also like to see if I can really implement Helvetica Neue Condensed Black as my headline font, without having to embed it. So, for now, it looks different but acts the same. More changes to come…

Trying out a new look

I’m trying out another new look for this blog. This design will probably evolve over time, but I am excited about the new direction — most significantly, the new colors, and the custom fonts using @font-face in CSS. The fonts are from a site I just discovered and am very excited about: The League of Moveable Type (no relation to Movable Type, the blogging software).

Of course, Internet Explorer won’t support it, so the fonts degrade to more common, standard, and boring options.

Let me know what you think!