Drupal Tutorial Part 4 - Styling an individual Block

To me, block are at the crux of everything that is good about Drupal. They provide tons of flexibility, add functionality to your site that you wouldn’t be able to get with say….Joomla, and overall learning to use blocks is the most important part of Drupal one could argue.

The only problem, and if you’ve ever worked with Drupal before then you know what I’m talking about, is styling those darn blocks. I was hoping in Drupal 5.0 they would provide an easy styling interface for custom blocks, like maybe a little button next to configure that said “style”. Then when you push the style button it opens up a window like the Google Adsense program
does. That would make this entire post not needed. Anyhow since Drupal does not give us this easy styling mechanism, here is how you would go about styling individual blocks. If you take a look at Criticalsole you can see that I went a bit crazy with styling each and every block, my style.css file is probably over 2500+ lines now.

Step 1 - Find the Block you want to Style

I’m going to take for granted that you know how to create a block, because if you don’t then maybe Drupal isnt’ for you. Just kidding, there are great tutorials for creating blocks on Drupal.org, use them. So finding the block, first I would suggest you fire up your Firefox browser. I’m going to get a lot of arguements here, but the reason I say use Firefox is because it outlines the source code so much better than the other browsers. When you can see a div class in blue that makes a huge difference. Wait maybe I should back up here, if you don’t know CSS then this might be hard to figure out. I’ll try my best to make it easy.

So once you’ve created a block, open up Firefox and View Page Source (you can view the page source by right clicking). Now what you are looking for, assuming you’ve created a block and it’s enabled for that page, will look something like the following:

and it should be in blue. To find out which block this, it is probably really evident what block it is by the text around it, you can go to “admin/block” and scroll over the “configure” button. Look in the bottom of your browser and you will see “admin/block/configure/78″. My block is 78 that’s why I was looking for 78, yours will be some other number or some other name.

Step 2 - Styling the Block - CSS work

Ok now that you found your block that you want to style, open up the style.css file in your theme. It can be located at: domain name/themes/theme name/style.css. Easiest way to do this is to open up an FTP session using your FTP program (Fetch for Mac, Filezilla for Windows) and drag the style.css file to your desktop.Now we are on to styling. To start styling your block add this to the end of your style.css file.

#block-block-78 {

}

This identifies your block. Now lets start styling.

#block-block-78 {

background: #000000;
color: #ffffff;
padding: 10px;
}

and so on. How about if you want to style the Title of the Block or if you don’t even want the title to show? This is often the case with Google Adsense, better to have no title than an “Advertisement or Sponsors” title.

#block-block-78 .title {

display:none;
}

There you go, now you’re off and styling. Save the style.css file and upload it and you’re golden. If you want to style other blocks just repeat the steps. I hope this helped everyone, leave a comment if you need me to clear anything up.

share this story
Add 'Drupal Tutorial Part 4 - Styling an individual Block' to Del.icio.us Add 'Drupal Tutorial Part 4 - Styling an individual Block' to digg Add 'Drupal Tutorial Part 4 - Styling an individual Block' to reddit Add 'Drupal Tutorial Part 4 - Styling an individual Block' to Technorati 

3 Responses to “Drupal Tutorial Part 4 - Styling an individual Block”

  1. Service-Blog » Neue Fundstücke 04/20/2007 says:

    […] webpodge » Blog Archive » Drupal Tutorial Part 4 - Styling an individual Block […]

  2. Mars says:

    Excellent stuff! Keep ‘em coming!

  3. | Programming Tips Tricks says:

    […] Webpodge » Blog Archive » Drupal Tutorial Part 4 - Styling an individual Block - This site is here to help you get your job done. webpodge » Blog Archive » Drupal Tutorial Part 4 - Styling an individual Block […]

Leave a Reply