Theming The Sidebar – Drupal Style (Part 1)

OK, so here's the situation:
You've done a nice Drupal theme design in Photoshop, or the like, which incorporates a sidebar containing some kind of background image, maybe rounded corners, maybe a left–to-right gradient – you get the picture. Now this is great if the content fits within the sidebar image comfortably, but what if you want to add more content, or a user increases the default text size thereby increasing the area covered by the sidebar content? The result will be content which effectively spills over the background image and messes up your lovely sidebar design. What you could really do with is a sidebar which contains a background image that re-sizes automatically to fit the content…

Here's one solution (I'll outline another solution in Part 2).

Solution 1 - The Sidebar Sideways Sliding Door

Basically, the first solution takes the famous A List Apart sliding doors of CSS method and turns it on its side.

Without recapping the entire article, the basic theory is that you have two images, and one effectively slides under the other as the content area changes in size. This makes it appear as if the sidebar is dynamically resizing to fit the content.

In our sidebar's case we can achieve the same thing using one image for the top of the sidebar and another for the bottom. The sidebar_top.jpg image will stay on top and the sidebar_bot.jpg image will slide underneath.

There are two steps to incorporate this effect:

  • Step 1: Create the images
  • Step 2: Edit the page.tpl.php file and the style.css file

Step 1: Create the images

Firstly, we take our original sidebar image and split it to create two images – sidebar_top.jpg and sidebar_bot.jpg.

Drupal sidebar theming images

One thing to keep in mind is that you can make the sidebar_bot image as long as you like depending upon how far you think it's going to need to slide (as the content changes in size).

Step 2: Edit the page.tpl.php file and the style.css file

Lets say we currently have the following code for our left sidebar in the page.tpl.php file of our Drupal theme

<?php if ($sidebar_left): ?>
    <div id="sidebar_left">
        <?php print $sidebar_left ?>
    </div>
<?php endif; ?>

As we can only place one background image into one div we are going to need another wrapper div for the sidebar to hold the other image. So, we add the extra code

<div id="sbarleft_top"></div>

to our original code, giving us

<?php if ($sidebar_left): ?>
    <div id="sidebar_left">
        <div id="sbarleft_top">
            <?php print $sidebar_left ?>
        </div>
    </div>
<?php endif; ?>

Now we have a situation whereby two divs (sidebar_left and sbarleft_top) both wrap around our entire sidebar. The positioning of #sbarleft_top is inside of #sidebar_left in our code, meaning that whatever background image we place into #sbarleft_top will sit on top of whatever background image we place into #sidebar_left.

Having edited the page.tpl.php file, we now just need to add the following css to our Drupal theme's style.css file so that our background images show up in the right place

#sidebar_left {
    background: url(images/sidebar_bot.jpg) no-repeat left bottom;
}
#sbarleft_top {
   background: url(images/sidebar_top.jpg) no-repeat left top;
}

and that's it. You should now have a sidebar whose background images resize to hold the content. Stay tuned for another solution, coming soon in Part 2.

3 comments

1
elvSeptember 27th 2007 @ 10:13PM

If your background has no transparency, you can also use a single image, with rounded corners on both top and bottom. Then you have to add a padding so the background in #sbarleft_top won't cover the bottom of #sidebar_left.
The benefits are:
- you only need to edit one image if you have to make changes
- less image files, useful if you use this solution on a lot of differently themed blocks

The CSS would then be:

#sidebar_left {
background: url(images/sidebar_bot.jpg) no-repeat left bottom;
padding-bottom: 10px; /* or more if your image height is bigger*/
}
#sbarleft_top {
background: url(images/sidebar_top.jpg) no-repeat left top;
}

2
Chad C.October 7th 2007 @ 01:32AM

Great Article. Love the site.. would love to see new content added on a regular basis! How about a quick how-to on editing a vote module?

Example: Theming the Vote-Up/Down Module .. or Theming 5 Star.

You could make a killing! :)

3
LaurenceOctober 8th 2007 @ 06:06PM

Hi guys,

elv - nice suggestion, thanks.

Chad - that's a good idea. I did a little theming of the vote up/down module recently, and imagine that quite a lot of people are now implementing voting stuff on their sites. Not sure about making a killing though...if only I charged...;)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options