View Full Version : Sidebar Hack in Javascript

04-15-2012, 09:19 PM
(Disclaimer: This is just something interesting I figured out, I'm not looking for help. If this is the wrong section, I'm sorry.)

I was writing a little CSS 3 demo page, and I wanted the sidebar to be the same size as the main content. I couldn't figure out any way to do it using HTML and CSS, so I hacked together this little snippet of Javascript:

<script type="text/javascript">
var sidebar = document.getElementById("sidebar");
var new_size = document.getElementById("main").clientHeight;
if (new_size > sidebar.clientHeight) {
sidebar.setAttribute("style", "height: " + new_size + "px;");

To use it in your own code, change "sidebar" to the id of the sidebar, and "main" to the id of main content. Then put it at the end of the body tag.