Zooiblog

Search the Zooiblog

Follow the discussion below and participate.

Support this site.

CSS Comment Highlighting

Okay, since Mathias linked me on this, I’ll post up an article. What’s it about? Comment highlighting on the one you’ve selected. It works through the magics of anchors and some spiffy CSS3 (currently only supported by Firefox 1.0).

Markup

Hee hee! Don’t mess around with the markup! That’s why we have CSS…

Okay, style then

A screenshot of the CSS funkiness in action – it's CSS3, people

Yes, style. The code is fairly simple, and can be extended to any funny in some kind of sick way.

This is the situation: I’ve got an ordered list, called #commentlist. That contains list items, marked up with an id. Now, I know one thing of this id: it’s unique. Hooray. That’ll allow you to link to it, and that’s where the magic comes in.

Firefox 1.0 supports the new selector :target, and that’s cool. Really cool. (Blake Scarbrough has a good post on the new features. So does Andreas. Andreas inspired me to implement this, by the way.)

Now, for the comment, you should know what the class of the comment is, or at least what type of element you want to edit. I have a definition list for my comments, so I have dt, as it contains the author’s name. With some cool CSS, you can enter text, different styling or even just hide the comment, if you’re so queer. Lemme give you an example, as it is in use at this blog.

#commentlist li[id^="comment"]:target dt::after {
	content: "This selected comment";
	margin-left: 15px;
	padding: 1px;
	background: #B5D7F2;
	border: 1px solid #9FCCEF;
	-moz-border-radius: 2px;
	text-transform: lowercase;
	font-size: 12px;
}

I’ll split this up for ya. content is what I want as an indicator. It’s fairly simple. Remember to add ::after to your selector, as Firefox doesn’t support content on normal elements yet. Next. margin yadda yadda yadda. padding is important (a bit), as it increases the size of the visual indicator. Always handy. The text-transform is a bit pointless, as I could’ve entered the value of content in lowercase. *shrugs* That should be it.

Of course, it all depends on your blog system, or for whatever you want to use it, but key is that you know how to do it. And it’s even cooler to change the background of the comment, or the whole comment, or to add an image in a diplay: blocked ::after. Just some suggestions. To see it in action, check this out.

Anne told me ::after is better. So there you have it.

(And while you’re at it, sign up for a nicely recognizable Gravatar.)

16 comments

  1. Ironically, this entry has no comment to test on :-)

  2. That’s so cool --- I wouldn’t have linked it if it wasn’t, but hey.

    For now, I’m still using a JavaScript to highlight a selected comment, but that only works when the page is loaded with a #comment- anchor in the URI. Your CSS method works in-page (or how do they call that), which is definitely much better.

    I’ll play around with it, and probably use it eventually on my blog.

    Thanks for the write-up!

  3. Alright, so I managed to achieve the result I’ve been wanting. A selected comment now has a different background colour on my blog. Like it had with the JavaScript, but now muchos better...

  4. Mathias, nice implementation. Now we should all write a post on reasons to switch from IE and other crappy browser to firefox one point aught. Anyone made any cool deriviations of this?

  5. Well, I guess I could come up with some links:

    • Microsoft is, erm... helpful;
    • It comes down to this, quite simply: Internet Explorer sucks. It’s buggy, shares responsibility for many of the security issues in Windows, offers far to many ways for people to compromise your system, and doesn’t support modern web standards.
    • There won’t be an Internet Explorer 7; if you want a better browser, you’ll have to pay for the next release of Windows, which is scheduled for release in 2005, and most corporate desktops won’t get there for a couple of years after that.
    • Internet Explorer can make your computer unsafe.
    • IE can’t even display PNGs.

    Crap, crap, crap. Choose Life. Choose a job. Choose a career. Choose a family. Choose a fucking big television, choose washing machines, cars, compact disc players and electrical tin openers. Choose good health, low cholesterol, and dental insurance. Choose fixed interest mortgage repayments. Choose a starter home. Choose your friends. Choose leisurewear and matching luggage. Choose DIY and wondering who the fuck you are on a Sunday morning. Choose sitting on that couch watching mind-numbing, spirit-crushing game shows, stuffing fucking junk food into your mouth. Choose rotting away at the end of it all, pishing your last in a miserable home, nothing more than an embarrassment to the selfish, fucked up brats you spawned to replace yourself. Choose your future. Choose life... But please, do not choose IE.

  6. Hahahaha! That last paragraph is simply brilliant Mathias.

    I’m going to stick that on my blog if you don’t mind. (Let me know if you do).

  7. He he he, same here ;-)

  8. Turnip, of course I don’t mind. A link would be nice though.

  9. I re-jigged it to include a direct link to you :).

  10. the last paragraph is from Trainspotting, so if anything, you should link to the movie’s website, not this one.

    -jamal, your neighborhood asshole

  11. Jamal, off course it’s from Trainspotting. But it’s the last sentence that made me smile :-)

    And Trainspotting is a fucking great movie.

    But that terzijde :P

  12. Jamal, you are one funny guy.

    (Rogier, terzijde = aside? Not too sure though.)

  13. sorry...i was unaware that everyone knew of this famous monologue and its source. now i feel like a moron. thx guys.

  14. First of all... Website under construction.

    I was looking at this page and this seems very interesting. But I have no idea what this piece of CSS is doing?!

    First I thought this was something to highlite searched-text, but afterwards I thought that the hilite text was done via PHP...?

    Please tell me, what does this great “Comment highliting” do?

  15. Hey Silence, view this site in a real browser (anything but IE), and click on a comment permalink. Look how cool, it says this selected comment next to your name!

  16. Say, I’ve got a blogmarks section on my site. Those pages consist of one huge definition list (dl), of which every dt has its own ID a la link-123, and of course a corresponding ID-less dd. Thanks to Rob, I got this CSS Highlighting Thing working on these suckaz in such a way that when a dt is directly accessed through an anchor in the URI, not only the dt gets highlighted, but also the corresponding dd. Yay!

    dt[id^="link"]:target, dt[id^="link"]:target+dd {
    background: #e1f0fb;
    }

    dt[id^="link"]:target, dt[id^="link"]:target+dd {
    margin: 0;
    }

    dt[id^="link"]:target+dd {
    padding-left: 40px;
    }

Participate, yes!

(Rock out with Textile; it's what the cool people use!)