Download Source Code: AnimatedCollapsiblePanel.zip - 16.03KB
| First from a series of articles on HTML panels or boxes, that you can freely use for your own website. Step-by-step design of a HTML panel with caption text, gradient background colors, shadow effects, collapse/expand features and animation on collapse/expand. Presentation of main design techniques, using TABLE or DIV elements, CSS and JavaScript code for dynamic behavior. No server-side code required, so you can use these boxes in plain HTML files. | ||
Overview
You can easily find several places on the web where you can build such panels. However, most of the solutions I found are either too complex, use IDs or client-side cookies, or do not have a nice presentation format. We tested current solution on latest versions of Internet Explorer and Firefox, including their Print Preview screens.
With our step-by-step design approach, we hope to clarify why certain techniques are required for such a HTML panel. We also hope you may eventually use just a part of the final panel design, so you can stop at an intermediate step and use, let's say, just a fixed panel with gradient background and shadow, if the collapsing functionality is not interesting for you.
The last chapter presents techniques of reverse-engineering similar HTML panels from external web sites. The goal is to detect not just the portion of HTML code that implements the box, but also all related images, styles and script code.
Our downloadable project includes a HTML page with all the panels designed here, an external CSS file, an external JS file for the JavaScript code, and all images grouped under an images subfolder. If you will gonna use a different file structure, don't forget to change all references to image locations to your own, in both the HTML files and CSS file. As sample image, we used the thumbnail of a nice photo from Switzerland (hope you like it), downloaded for free from BigFoto.com
While HTML panel design does not require server-side code or a particular web development environment, by keeping it generic you will be able to use this code in any kind of web-based project: ASP, ASP.NET, PHP, ColdFusion or just simple HTML local or published pages.
Fixed Panel Design
| Title goes here |
![]() Content goes here... |
This should be trivial, is this right? I mean, it's so easy and obvious to create and use a HTML TABLE with two rows, first row with a caption text and a different background color. To look nice, we should also add a border.
It is easy indeed, but most web programmers spend a lot of time figuring out what the best attribute values should be, for the best color scheme, layout and definition. This is where templates may help, because other people before us did the job.
There are also some possible traps you may get into. For instance, the border=1 setting for a TABLE element does not give you a solid thin one-pixel wide border. And tables with default outlined borders look different in Internet Explorer and Firefox. You should rather use CSS, with style="border: solid 1px".
<html> <body> <!-- Immediate TABLE-based HTML panel with border=1 --> <table cellspacing="0" cellpadding="5" width="170" border="1" style="border-color:#336699; text-align:center;"> <tr><td bgcolor="#336699" style="color:#ffffff;"> Title goes here </td></tr> <tr><td bgcolor="#f5f5f5"> <img width="150" height="100" src="../images/sample_photo.jpg" alt="This is an image" title="This is an image" /><br /> Content goes here... </td></tr> </table> </body> </html>
It's also recommended to use DIV, rather than TABLE, with the same CSS style setting for border. Both DIV and TABLE are block HTML elements, but DIV rendering is considered more reliable. And you don't have to use TR and TD elements either. Anyway, most our techniques use either DIV, or TABLE, or combinations of both.

Content goes here...
No need of images, JavaScript code, external files or other code sequences. We used a color scheme with a dark blue for the caption bar and the thin border - in tone with the dominant color of the photo - and a white smoke (light gray) for the background. The contrast is strong enough and the lines sharp.
Remark the usage of float:right style in the DIV element, which allows us to present the box aligned to the right and inline with our text.
Most tricky and sometimes confusing layout attributes are those related to margins between sibling and nested HTML elements. TABLE cellpadding - which by default is not zero - defines the margins kept within each table cell. TABLE cellspacing - also not zero by default - defines the space between adjacent cells and rows. If you don't need them, turn them off by explicitly setting them to 0.
It's better to define them through CSS anyway. This works for both TABLE and DIV, and you can eventually declare them individually for each side (top, bottom, left and right). For a TABLE, padding is similar to cellpadding, but there is no spacing CSS attribute. There is the margin attribute, but this defines the outer space - not the inner space - left between the element and its parent.
Here is the HTML code for the simple panel with a really thin border, using a TABLE container and inline style declarations:
<html> <body> <!-- TABLE-based HTML panel with thin border --> <table cellspacing="0" cellpadding="0" style="width:170px; border: solid 1px #336699; text-align:center;"> <tr> <td style="background-color:#336699;color:#ffffff;padding:5px;"> Title goes here </td> </tr> <tr><td style="background-color:#f5f5f5;padding:10px;"> <img width="150" height="100" src="../images/sample_photo.jpg" alt="This is an image" title="This is an image" /><br /> Content goes here... </td></tr> </table> </body> </html>
And here is the equivalent version using a DIV container:
<html> <body> <!-- DIV-based HTML panel with thin border --> <div style="width:170px;border:solid 1px #336699;text-align:center;"> <div style="background-color:#336699;color:#ffffff;padding:5px;"> Title goes here </div> <div style="background-color:#f5f5f5;padding:10px;"> <img width="150" height="100" src="../images/sample_photo.jpg" alt="This is an image" title="This is an image" /><br /> Content goes here... </div> </div> </body> </html>

Content goes here...
We'll now keep our latest DIV-based version and we'll add two new elements: (1) a gradient background for the caption bar and (2) a shadow image, below the content panel.
Gradient backgrounds show a base color in different nuances, from light to dark, on the vertical or horizontal axis. What's great with gradients, you need in fact a very small image, which gets repeated. Our blue-based gradient image here is a 20x60 PNG, which will get extended to whatever the size of our caption block will be. All you need is to replace the background-color style attribute for the caption title with a background-image attribute. And don't forget background-repeat:repeat-x, to auto-repeat the image on the horizontal axis.
There are many techniques to create a shadow, but here is a very simple and visually efficient one, using a single and simple image, to be added immediately after the panel. The image must be large enough to be used in the largest possible panel you may create. When shown within a IMG tag, its width must be set to the same width as the panel. Its height must be also be resized with the same factor, to keep the aspect ratio.
<html> <body> <!-- DIV-based HTML panel with gradient background and shadow --> <div style="width:170px;border:solid 1px #336699;text-align:center;"> <div style="background-image: url(../images/gradient_blue.png); background-repeat: repeat-x; color:#ffffff; padding:5px;"> Title goes here </div> <div style="background-color:#f5f5f5;padding:10px;"> <img width="150" height="100" src="../images/sample_photo.jpg" alt="This is an image" title="This is an image" /><br /> Content goes here... </div> </div> <img width="170" height="20" src="../images/shadow.gif" alt="" /> </body> </html>
August 13, 2007 at 03:32 PM
August 13, 2007 at 09:51 PM
August 14, 2007 at 09:13 AM
August 14, 2007 at 12:36 PM
A quick solution can be applied for the first collapsible panel in page 2 (sorry I don't have time enough now for the code - tomorrow I go in vacation :)).
In the generic togglePannelStatus JavaScript function, insert code, at the beginning, to identify all DIVs with squareboxgradientcaption class name and collapse all panels from the page.
September 13, 2007 at 02:44 PM
September 17, 2007 at 05:41 AM
I found your 'Animated Collapsible Panel' tutorial extremely interesting and was wondering if it can be tweaked so that the panels extend and collapse horizontally instead of vertically as in the sreenshot link below:
http://www.igolfscorer.com/widget.jpg
Any advice would be great! Thanks again for the great tutorial!
Kind regards,
Declan
October 12, 2007 at 12:10 PM
October 18, 2007 at 11:26 AM
April 07, 2008 at 07:08 PM
April 18, 2008 at 07:12 PM
April 18, 2008 at 09:45 PM
September 22, 2010 at 12:00 AM
September 26, 2010 at 05:44 PM
I was wondering how to get the expand and collapse image to work in Safari? Any suggestions?
Thanks
September 26, 2010 at 09:57 PM
March 03, 2011 at 04:33 AM
:
< /div >< div class="squareboxcontent" > //this works fine
:
< /div >
< div class="squareboxcontent" > //stops working
:
March 03, 2011 at 05:58 AM
March 03, 2011 at 04:44 PM