How to build a threaded comment block with HTML5 and CSS3

Default avatar.
January 18, 2013
How to build a threaded comment block with HTML5 and CSS3.

ThumbnailBlogs and forums often have threaded comment replies built into their network. Popular social networks Reddit and Hacker News both support threaded comments, which allows users to directly reply inline with comments, voicing an opinion without getting lost in the shuffle.

We're going to look at building a simple HTML5 and CSS3 threaded comments layout. We won't be using any jQuery effects on the comment blocks (although it is possible to extend this functionality). We will look into structuring the HTML5 document and how to position elements using CSS. From this base template it should be easy for developers to pick up their own customizations and implement a threaded comment block into any website layout.

Before we get started, take a look at the demo to see what we'll be building. If you want to, you can download the source from here.

Constructing the webpage header

I want to start by looking over the basic HTML template used in this layout. I am sticking to the HTML5 doctype coded in UTF-8 along with some other HTML5 elements. For web browsers older than Internet Explorer 9 we include a copy of the HTML5shiv script hosted on Google's dev servers.

<!doctype html>
<html lang="en-US">
 <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
 <title>HTML5/CSS3 Threaded Comments Block</title>
 <meta name="author" content="Jake Rocheleau">
 <link rel="shortcut icon" href="">
 <link rel="icon" href="">
 <link rel="stylesheet" type="text/css" href="styles.css">
 <link rel="stylesheet" type="text/css" href="">
<!--[if lt IE 9]>
 <script type="text/javascript" src=""></script>

This will allow newer elements such as <header>, <footer>, and <section> to render properly in all browsers. The only issue would be running into web browsers with JavaScript disabled, but in my opinion you can't drive yourself mad catering to every single user. I have included another external resource for the Wellfleet Google webfont which styles the page header text.

(Isn't it great that we live in a time where all of these resources are free for developers to use? It allows us to rapidly prototype a comments template without too much hassle.)

Inner body content

Inside the <body></body> tags we can find a whole bunch of inner comment divs. The whole page layout is wrapped inside a div with the ID #w for wrapper. Everything is centered on the page with a fixed width of 700px.

The #container div is what holds the comment system in place away from the header text or other page elements. Each thread of comments is held inside an unordered list where the root <ul> has the ID #comments. Each internal element is given the class .cmmnt and replicates much of the same inner content. Here is a sample comment from the page layout:

<div><a href="javascript:void(0);"><img src="images/pikabob.png" width="55" height="55" alt="pikabob photo avatar"></a></div>
<header><a href="javascript:void(0);">Pikabob</a> - <span>posted 6 days ago</span></header>
<p>Listen you are going to get a kick out of this one. I've got to tell one of the funniest jokes of all time.</a></p>

The inner comment contains a floating avatar image along with another div using the class .cmmnt-content. This inner div container helps to distinguish from the avatar column, so that way our content is placed solely on the right side. This also allows room to place <header> and <footer> tags inside the comment body, which may hold additional links for editing or sharing.

The inner comments use internal unordered lists with the class .replies attached onto each one. This allows us to duplicate comment levels 3 or even 4 threads deep. For this demo I have only created styles to support 3-tier level comments. But it is very simple to customize threads to go even deeper.

 <div><a href="javascript:void(0);"><img src="images/dark-cubes.png" width="55" height="55" alt="DarkCubes photo avatar"></a></div>
 <header><a href="javascript:void(0);">DarkCubes</a> - <span>posted 1 week ago</span></header>
 <p>Ut nec interdum libero. Sed felis lorem, venenatis sed malesuada vitae, tempor vel turpis. Mauris in dui velit, vitae mollis risus. Cras lacinia lorem sit amet augue mattis vel cursus enim laoreet. Vestibulum faucibus scelerisque nisi vel sodales. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis pellentesque massa ac justo tempor eu pretium massa accumsan. In pharetra mattis mi et ultricies. Nunc vel eleifend augue. Donec venenatis egestas iaculis.</p>
 <div><a href="javascript:void(0);"><img src="images/pig.png" width="55" height="55" alt="Sir_Pig photo avatar"></a></div>
 <header><a href="javascript:void(0);">Sir_Pig</a> - <span>posted 1 day ago</span></header>
 <p>Sed felis lorem, venenatis sed malesuada vitae, tempor vel turpis. Mauris in dui velit, vitae mollis risus.</p>
 <p>Morbi id neque nisl, nec fringilla lorem. Duis molestie sodales leo a blandit. Mauris sit amet ultricies libero. Etiam quis diam in lacus molestie fermentum non vel quam.</p>

Notice that even the lower-level threaded replies still follow the exact same formatting as top-level comments. This is obviously helpful if you are wrapping these styles around a CMS such as Wordpress. Then you can dynamically generate comments to fill in the same template.

CSS Stylesheet resets

I have created another document, styles.css, which holds all our basic CSS code for positioning and designing page content.

I'm starting off with my own customized CSS resets which are based on Eric Meyer's code snippets. This will reset all fonts, margins, padding, and more importantly box-sizing for all elements on the page in every major browser. You may also notice I am using some properties for dynamically updating the CSS highlight color.

html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
 margin: 0;
 padding: 0;
 border: 0;
 font-size: 100%;
 font: inherit;
 vertical-align: baseline;
 outline: none;
 -webkit-box-sizing: border-box;
 -moz-box-sizing: border-box;
 box-sizing: border-box;
html { height: 101%; }
body { background: #e3e0ef url('images/bg.png'); font-size: 62.5%; line-height: 1; font-family: Arial, sans-serif; padding-bottom: 65px; }

::selection { background: #d7d0f3; }
::-moz-selection { background: #d7d0f3; }
::-webkit-selection { background: #d7d0f3; }

article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; }
ol, ul { list-style: none; }

blockquote, q { quotes: none; }
blockquote:before, blockquote:after, q:before, q:after { content: ''; content: none; }
strong { font-weight: bold; } 

table { border-collapse: collapse; border-spacing: 0; }
img { border: 0; max-width: 100%; }

As we move further down I have included code for resetting the basic styles on h1 elements and paragraphs in the page. We need to setup a core structure for the wrapper and container divs. On the container I have actually included a few CSS3 box shadow properties to stand out from the background.

h1 { font-family: 'Wellfleet', 'Trebuchet MS', Tahoma, Arial, sans-serif; font-size: 2.85em; line-height: 1.6em; font-weight: normal; color: #756f8b; text-shadow: 0px 1px 1px #fff; margin-bottom: 21px; }

p { font-family: Arial, Geneva, Verdana, sans-serif; font-size: 1.3em; line-height: 1.42em; margin-bottom: 12px; font-weight: normal; color: #656565; }

a { color: #896dc6; text-decoration: none; }
a:hover { text-decoration: underline; }

/* page layout structure */ 
#w { display: block; width: 700px; margin: 0 auto; padding-top: 35px; }

#container { 
 display: block; 
 width: 100%; 
 background: #fff; 
 padding: 14px 20px; 
 -webkit-border-radius: 4px; 
 -moz-border-radius: 4px; 
 border-radius: 4px; 
 -webkit-box-shadow: 1px 1px 1px rgba(0,0,0,0.3);
 -moz-box-shadow: 1px 1px 1px rgba(0,0,0,0.3);
 box-shadow: 1px 1px 1px rgba(0,0,0,0.3);

Styling the page comments

Now this last bit of code to look over will setup all our comments on the page.

#comments { display: block; }

#comments .cmmnt, ul .cmmnt, ul ul .cmmnt { display: block; position: relative; padding-left: 65px; border-top: 1px solid #ddd; }

#comments .cmmnt .avatar { position: absolute; top: 8px; left: 0; }
#comments .cmmnt .avatar img { 
 -webkit-border-radius: 3px; 
 -moz-border-radius: 3px; 
 border-radius: 3px; 
 -webkit-box-shadow: 1px 1px 2px rgba(0,0,0,0.44);
 -moz-box-shadow: 1px 1px 2px rgba(0,0,0,0.44);
 box-shadow: 1px 1px 2px rgba(0,0,0,0.44);
 -webkit-transition: all 0.4s linear;
 -moz-transition: all 0.4s linear;
 -ms-transition: all 0.4s linear;
 -o-transition: all 0.4s linear;
 transition: all 0.4s linear;

#comments .cmmnt .avatar a:hover img { opacity: 0.77; }

To start each .cmmnt list item is designed to be pushed over to the left with each internal <ul> container. I have thepadding-left: 65px; value setup so that each block is pushed aside regardless of length or height. Each avatar image is positioned absolutely to the left side, so having that additional padding keeps content away from the edge.

I also applied some unique CSS3 border-radius effects and transitions onto the avatar images. As you hover over each one the opacity will be gradually reduced to 77% over 400 milliseconds. This is a very interesting effect to have with only a few short lines of code. It used to require lengthy JavaScript to even replicate something close to this transition.

#comments .cmmnt .cmmnt-content { padding: 0px 3px; padding-bottom: 12px; padding-top: 8px; }
#comments .cmmnt .cmmnt-content header { font-size: 1.3em; display: block; margin-bottom: 8px; }
#comments .cmmnt .cmmnt-content header .pubdate { color: #777; }
#comments .cmmnt .cmmnt-content header .userlink { font-weight: bold; } 
#comments .cmmnt .replies { margin-bottom: 7px; }

In this latter part of the CSS code, all we are doing is positioning content to have extra padding and updating the color schemes.

Final thoughts

The realm of frontend web development is huge and encompasses a large variety of user interfaces. In this tutorial we have looked over some options which allow users more direct contact with each other. This is probably the easiest solution for presenting comments because of how layered conversations appear on a monitor.

Have you built a comment system for a site? What challenges are involved in designing for conversations? Let us know in the comments.

Featured image/thumbnail, comments image via Shutterstock.

Jake Rocheleau

Jake is a writer and user experience designer on the web. He publishes articles discussing HTML5/CSS3 and jQuery coding techniques. Find out more on his website or you can follow his updates on Twitter @jakerocheleau

Read Next

3 Essential Design Trends, February 2024

From atypical typefaces to neutral colors to unusual user patterns, there are plenty of new website design trends to…

Surviving the Leap from College to Real-World Design

So, you’ve finished college and are ready to showcase your design skills to the world. This is a pivotal moment that…

20 Mind-Bending Illusions That Will Make You Question Reality

Mind-bending videos. Divisive Images. Eye-straining visuals. This list of optical illusions has it all. Join us as we…

15 Best New Fonts, February 2024

Welcome to February’s roundup of the best new fonts for designers. This month’s compilation includes some innovative…

The 10 Best WordPress Quiz Plugins in 2024

Whether it’s boosting your organic search visibility or collecting data for targeted email marketing campaigns, a great…

20 Best New Websites, February 2024

It’s almost Valentine’s Day, so this latest collection is a billet-doux celebrating the best of the web this month.

Everything You Need to Know About Image Formats In 2024

Always trying to walk the tightrope between image quality and file size? Looking to branch out from JPGs and PNGs this…

The 10 Best Logos of 2023 - Ranked

From vintage aesthetics to innovative new color schemes, we’ve seen a lot of creative logo designs this year. In this…

Exciting New Tools for Designers, February 2024

2024 is well underway and shaping up to be an interesting year. So, to help you keep your focus on working hard and…

The Art of Engineering AI Prompts

In the rapidly evolving world of artificial intelligence, the ability to communicate effectively with AI tools has…

15 Best New Fonts, January 2024

In this month’s roundup of the best new fonts, we find a large number of vintage, retro, and revival typefaces. Is it…

5 Web Design Disasters That Will Ruin Your 2024

Welcome to 2024, where web design is as fast-paced as ever. You know how crucial a killer website is nowadays — your…