Add to My Yahoo! | Google Reader or Homepage | Add to Windows Live | Add to Windows Live Alerts

Wictor Wilén

Microsoft Certified Master (MCM) - SharePoint 2010 | Microsoft Most Valuable Professional (MVP) - SharePoint Server MVP | Author

Creating custom themable CSS files for SharePoint 2010

Posted at 2010-02-17 02:57 by Wictor Wilén in Web Parts , SharePoint 2010 with 8 comments.

SharePoint 2010 has a completely rebuilt theme engine. Instead of having to modify .inf files, create folders and copy and pasting CSS files and images in the SharePoint root we can now create a theme in PowerPoint and upload it to the server. The default SharePoint CSS files are tagged with comments that tells SharePoint to substitute colors and images with the values from the theme. When you switch theme SharePoint generates a new set of CSS files and images with the result of this substation.

What if you have the need to create a custom CSS for you solution or Web Part? How can you use this theme? It's actually quite easy!

Let's take a look at a sample Visual Web Part that is going to have a custom CSS file.

Create the Visual Web Part

First of all we need to start the new awesome/fantastic/productive new Visual Studio 2010 and create a Visual Web Part Project. Add a simple button to the user control and attach a custom CSS class to it.

Button with custom CSS

Add the CSS

Once we have this button with the CSS class we need to create the CSS file and define the class. This is done by adding the SharePoint "Layouts" Mapped Folder to the Project. This will also add a folder with your project name under the Layouts folder - remove that one. Then add a folder structure like this 1033/Styles/Themable to the Layouts folder.

To the Styles folder you then need to add the CSS file with your class. It should look something like this:

.MyCustomClass {
/* [ReplaceColor(themeColor:"Light2-Lightest")] */ border:solid 1px #d9dde1;
/* [ReplaceColor(themeColor:"Light2")] */ background-color:#f5f6f7;
}

The ReplaceColor tag in the CSS comment will replace the colors in the CSS styles using the named color of the theme. For full reference check out corev4.css in the SharePointRoot.

Then copy this file so it has a duplicate in the Themable CSS folder, your solution should look like this:

The solution!

SharePoint will use the CustomStyle.css in the Styles folder, when no theme is activated, so this is your default CSS. When you activate a theme it will take the CustomStyle.css from the Themable folder and substitute the colors as you wrote in the CSS file. Note that if you already have a theme enabled you have to re-enable it or if you make changes to the CSS you also need to re-enable the theme.

Register the CSS

We have one more thing to do to get this thing going - and that is to tell the Visual Web Part to use this CSS. This is done using the CssRegistration control which we add to the Visual Web Part user control like this:

CssRegistration control

You need to specify the Name attribute, which is the name of your CSS file. The EnableCssTheming attribute is not necessary since the default value is true. If it's set to false the CSS in the Styles folder will always be used. If you leave it out or set it to true and provide no CSS in the Themable folder then your CSS will not be loaded.

Try it out!

Now deploy your Visual Web Part and then add it to a page. Then go to Site Settings and change between themes. Isn't it beautiful!

No theme Bittersweet theme Vantage theme

To get some more details - make sure to get my book once it hits the streets!

Comments and trackbacks

#  Social comments and analytics for this post by Trackback
Screenshot from websnpr This post was mentioned on Twitter by wictor: Just blogged: Creating custom themable CSS files for SharePoint 2010 http://bit.ly/9GRVuX
#  Good post, thax by Christian Ståhl
Screenshot from websnpr Looking forward to read more about this in your book when it hits the streets!
#  @Christian by Wictor
Screenshot from websnpr Thanks mate!
#  Don't need two copies of the CSS file by Elisabeth Olson (MSFT)
Screenshot from websnpr You actually only need the copy of the CSS file in the themable folder, which you then reference in the CSSRegistration control as "themable/mycss.css". The reason the way you're referencing it works is actually just an implementation detail that may or may not hold true in future versions of SharePoint.
#  @Elisabeth by Wictor
Screenshot from websnpr Thanks for the info. We'll probably be aware of how it actually works within a few days when RTM is out...
#  I added a new CSS by puting it into Style Library of my Sharepoint 2010 Site. Styles are applied when there is no theme for the site. But styles are not applied if I select a theme. Could you please help me to resolve this. Do I have to add my CSS into _l by chatroulette
Screenshot from websnpr I added a new CSS by puting it into Style Library of my Sharepoint 2010 Site. Styles are applied when there is no theme for the site. But styles are not applied if I select a theme. Could you please help me to resolve this. Do I have to add my CSS into _layouts/1033/styles/themeable directory manually?
#  MOSS 2007 custom theme in SPS 2010 by mohsin
Screenshot from websnpr Hi, i copied my custom theme developed in MOSS 2007 to new SPS 2010 machine and attached my content DB from MOSS 2007 to SPS 2010. the website is opening fine in V3 mode. I used preview option to check how it will appear using new UI features and found all images (header, background, menu etc), styles are not no longer there. What is the reason? Do I have to update my master page OR the updated UI is no longer referring my custom theme. What is easiest way by which i can get same look of my website? Do I have to convert my 2007 theme in 2010 theme? Create new 2010 theme just like the old one? use new V4 master page and related CSS file, Brand my site as per requirement and ignore theme. it is there any different of my site collection is of "publishing" type or "Team site" please note that in MOSS 2007 we have 3 web apps in our farm using the same custom theme.
#  EnableCssTheming by Tsvetelin Pavlov
Screenshot from websnpr EnableCssTheming works in any location. Only for Style Library/Themable it is set true by default. For any other location EnableCssTheming must be explicitly set to true and the theme engine will process the file, no matter where it is situated.
Make a comment on this post:
Subject:  

Your name:  
Your Url:  
Note: submissions may have to be approved before being visible, so don't submit your comment multiple times.