Monday, February 18, 2008

HOWTO: Add a custom CSS class to Blogger

The layout and the presentation (i.e. styling) of Blogger blogs are handled by cascading style sheets (CSS). If you ever found that the default styles available from the Blogger's editor are not enough for the type of content you write, you can easily add a custom style sheet in to your template.

Even if you don't know much about CSS and HTML, this illustrated tutorial will guide you on how to add a given style class to your Blogger template.

Here's how to do it. This tutorial will show you how to add a style class to display code snippets. The code for the CSS class given below is also styled using the same class.

/*
Custom CSS class to display code snippets.
http://bguide.blogspot.com/
2008 Feb 03
*/
.mycode
{
border:1px dashed #aaaaaa;
background:#eeeeee;
white-space:pre;
font-family:monospace;
font-size:9pt;
line-height: 10pt;
height:auto;
width:auto;
padding:5px;
margin-left:10px;
overflow:auto;
}

Firstly, the above CSS class must be added to your blog template. Open the Blogger Template Editor and go to the Edit HTML mode. Locate the skin element of your template that has all the CSS code.

<b:skin><![CDATA[/*

/* All CSS code goes in between here */

]]></b:skin>

Copy the above CSS class in to the template anywhere in between the above two lines.


Once you do this the CSS class mycode is available to all the posts in your blog.

The next thing is to apply this style in to the code snippet that needs to appear in your article.

Switch to the Edit Html mode in the Blogger editor and place the code snippet inside a div element, the generic container element of HTML. Then set the class attribute of the div element as mycode. The mycode CSS class will preserve all the whitespace and therefore make sure that all the whitespace of the code snippet are arranged according to the way you want.

<div class="mycode">

/* Paste the code snippet here */

</div>

The following figure illustrates the Edit Html mode and how the code snippet is put inside a div element.


Once you do that the code snippet will be styled similar to the snippets in this article. You can also add other style sheets by following the same steps given in this tutorial.

Note: You are free to use the above CSS class. If you choose to do so, please link to this article from your blog/site.

11 comments:

Jesdisciple said...

This is another very useful line for that class:

overflow: auto;

ids said...

Oh yes. I had it on my blog. But forgot to update the article.

Thanks for reminding.

waton said...

Thank's for this guide. To display html snippet code on blogger.com htmlcode must be escape with something like http://www.blogcrowds.com/resources/parse_html.php
before posted it and display with blockquote

Mycoolestgifts said...

the guide is detailed and clear. Highly appreciate your option on that: my blog is http://www.electronics345.blogspot.com

Prakash said...

WoW thanks for the post. Good post to apply the snippet css

weichong78 said...

Thanks for your post really. I am very bad at web/css programming as I am more into low level programming. This really helps for me to post my code snippet. Thanks a hundred.

Parag said...

Thanks for posting this guide. It helped me customize my blog.

maidul said...

Thanks for your cool tip.Can you help me on how to add external style sheet on blog!

ids said...

@ maidul,

The article on how to add a tree control for blogger has an example on installing an external style sheet.

Sandeep said...

good post for new bloggers www.techieway.blogspot.com

Anonymous said...

thank you so much for making it understandable .added you to favorite.

Post a Comment

Thanks in advance for all your comments and suggestions. They are very much appreciated.

IMPORTANT:
If you have a specific technical question, please email me with a description of the problem and the blog's URL.