Monday, September 29, 2008

How to use the !important tag in CSS

"CSS" stands for: "Cascading Style Sheets'. 

This means that the styles cascade and are applied in order as they are read by the browser. The browser reads CSS from top down. The first style is applied and then the second and so on. So if a style appears at the top of a style sheet and then is changed lower down in the document, the last one wins!

For example, in the following style sheet, the div class "text" will be white, even though the first style property applied is black:

#text{ color: #000000;}

#text{ color: #FFFFFF;}

The !important rule is a way to force styles out of order. A rule that has the !important property will always be applied no matter where that rule appears in the CSS document. So if you wanted to make sure that a property always applied, simply add the !important property to the tag.

So, to make the div class text always black, simply add the
"!important" tag right before the closing semicolon.

#text{ color: #000000 !important;}

#text{ color: #FFFFFF;}

To relate this back to SharePoint here are  few examples where you might need to use the !important tag to get your styles listen.

Example 1: You have applied a custom theme to a site. But on some of your pages the colors are inconsistent. Use the !important tag to force the class attributes so that all color behaviors act as expected.

Example 2: You are in the middle of creating your custom brand and notice that around the content area there is some hard coded margins. You could either create your own custom master page to fix this or you could force the margins the way you want them by using the !important tag.

I do not suggest using the !important tag for everything. Its a bit of a cop out. But there are some cases where it really does help keep your design functioning as your want it.

2 comments:

Anonymous said...

u have opened my eyes

Sqiar BI said...

It's actually a cool and useful piece of info. I am glad that you simply shared this useful info with us. Please keep us up to date like this. Thanks for sharing, You can also check out this http://www.sqiar.com/services