SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

SharePoint's core.css stylesheet: Put it to where it belongs!

When rendering CSS stylesheets, SharePoint puts core.css always as the last stylesheet. This post is about how to change this sometimes unwanted behaviour.

 OK, why bother with how CSS Stylesheets are rendered? Imagine you create your own Stylesheet overriding a lot of the SharePoint styles defined in core.css (and as you know, there are really "some" styles you might override Cool). Now, if you follow the SharePoint "standard" approach and add your StyleSheet using the <SharePoint:CssRegistration> webcontrol, you'll always have the problem that the Core.css stylesheet (the one you're actually overriding...) is rendered AFTER your custom stylesheet. Result: All your work is for nothing, since the last one wins when styles will be applied to your web page... So, our friend "core.css" will always be the winner.

 There are several approaches how to handle this problem, they are very well documented by Heather Solomon. My approach is somewhat different, though.

 I've examined (thanks to .NET Reflector once more), the webcontrols CssRegistration and CssLink. CssRegistration is used to add custom CSS stylesheets using a "standard" approach with WebControls (thus not hardcoding it). CssLink does not accept any input but just renders all registered Stylesheets that are present in the SPContext where the page is rendered. The problem is that most of the methods, properties etc. are only internal and/or private. It is therefore practially impossible to really work with these types in the page declaration - not even in code...

However, there is one nice trick you could do - and in fact it works really well! Let's create a class that inherits from CssLink:

using System;
using System.IO;
using System.Web.UI;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;

namespace MichaelHofer.SharePoint.PublishingEnhancements
{
    public class EnhancedCssLink : CssLink
    {
        public EnhancedCssLink() : base() { }

Now override the render method and do the "magic": Let the base class render its content, split the rendered "<link>" tags (they are separated by a line-break) and put the last element (= always core.css) on top. But this is not all, you could also do some more stuff. Example? Why not skip rendering of core.css when the current user is an anonymous user who looks at your internet page in "view-only" mode? In these cases, you won't need the core.css most probably... So why not take the overhead away? See this nice blog post for details!

Anyway, back to business, this is the override of the "Render" method:

        protected override void Render(System.Web.UI.HtmlTextWriter output)
        {
            // Let base render the stylesheets
            StringWriter sw = new StringWriter();
            base.Render(new HtmlTextWriter(sw));
            string renderedOutput = originalSw.ToString();

            // Split the styleSheets into an array
            string[] styleSheets = renderedOutput.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
            if (styleSheets.Length == 0)
            {
                output.Write(renderedOutput);
            }
            else
            {
                string[] reorderedStyleSheets = new string[styleSheets.Length];
                for (int i = 1; i < styleSheets.Length; i++)
                {
                    reorderedStyleSheetsIdea = styleSheets[i - 1] + "\n";
                }
                // core.css is always the last stylesheet --> put it in front!
                reorderedStyleSheets[0] = styleSheets[styleSheets.Length - 1] + "\n";
                output.Write(string.Concat(reorderedStyleSheets));
            }
        }
    }
}

That's all! Now compile and deploy (put to GAC and register as safe control, or build a SharePoint solution; see my previous posts for details).

In your master page, declare your assembly. Example:

<%@ Register Tagprefix="PublishingEnhancements" Namespace="MichaelHofer.SharePoint.PublishingEnhancements" Assembly="MichaelHofer.SharePoint.PublishingEnhancements, Version=1.1.0.0, Culture=neutral, PublicKeyToken=7ce575c89ea427a4" %>

And instead of  <SharePoint:CssLink> use now your own web control. Example:

<PublishingEnhancements:EnhancedCssLink runat="server"/>

Here you go, all problems with core.css solved, this is what you'll get rendered: 

 <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/core.css?rev=5msmprmeONfN6lJ3wtbAlA%3D%3D"/>
<link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/controls.css?rev=EhwiQKSLiI%2F4dGDs6DyUdQ%3D%3D"/>
<link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/HtmlEditorCustomStyles.css?rev=8SKxtNx33FmoDhbbfB27UA%3D%3D"/>
<link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/HtmlEditorTableFormats.css?rev=guYGdUBUxQit03E2jhSdvA%3D%3D"/>
<link rel="stylesheet" type="text/css" href="/Style%20Library/schleuni.css"/>

 


Posted 07-16-2007 12:25 AM by mhofer1976

Comments

Richard wrote re: SharePoint's core.css stylesheet: Put it to where it belongs!
on 07-16-2007 12:41 PM

What you could also use is a HTTPModule like this one: www.ie-soft.de/.../PermaLink,guid,968b0588-f306-467b-be51-54f7a8f2079d.aspx

This would allow you to remove core.css totaly (e.g. for public available websites)

Philip Waters wrote re: SharePoint's core.css stylesheet: Put it to where it belongs!
on 08-27-2007 10:21 AM

in your sample code is originalSw actually supposed to be "output"? I get an error originalSw does not exist in current context.

Also I'm guessing the line reorderedStyleSheets = styleSheets[i-1]  should be

reorderedStyleSheetsIdea = ....

Right?

PhilipWaters wrote re: SharePoint's core.css stylesheet: Put it to where it belongs!
on 08-27-2007 10:22 AM

Aha!

I see now, it didn't copy the light bulb

Clever Workarounds » SharePoint Branding - How CSS works with master pages - Part 1 wrote Clever Workarounds &raquo; SharePoint Branding - How CSS works with master pages - Part 1
on 09-24-2007 3:41 PM

Pingback from  Clever Workarounds &raquo; SharePoint Branding - How CSS works with master pages - Part 1

CleverWorkarounds » SharePoint Branding - How CSS works with master pages - Part 1 wrote CleverWorkarounds &raquo; SharePoint Branding - How CSS works with master pages - Part 1
on 10-08-2007 12:04 AM

Pingback from  CleverWorkarounds &raquo; SharePoint Branding - How CSS works with master pages - Part 1

CleverWorkarounds » SharePoint Branding - How CSS works with master pages - Part 2 wrote CleverWorkarounds &raquo; SharePoint Branding - How CSS works with master pages - Part 2
on 10-10-2007 5:10 PM

Pingback from  CleverWorkarounds &raquo; SharePoint Branding - How CSS works with master pages - Part 2

Placelight Blog wrote I ain't afraid of no ghost (unghosted vs. ghosted files in SharePoint)
on 10-10-2007 9:42 PM

I ain't afraid of no ghost (unghosted vs. ghosted files in SharePoint)

Sjoert wrote re: SharePoint's core.css stylesheet: Put it to where it belongs!
on 10-16-2007 1:50 PM

Nice one! This is a much better solution than to stick the css in the page layout.

Mohamed Zaki wrote re: SharePoint's core.css stylesheet: Put it to where it belongs!
on 11-11-2007 9:42 AM

originalSw should be sw only

string renderedOutput = sw.ToString();

travislowdermilk.com » Blog Archive » Customizing the order of your stylesheets wrote travislowdermilk.com &raquo; Blog Archive &raquo; Customizing the order of your stylesheets
on 12-31-2007 2:12 PM

Pingback from  travislowdermilk.com  &raquo; Blog Archive   &raquo; Customizing the order of your stylesheets

Derek wrote re: SharePoint's core.css stylesheet: Put it to where it belongs!
on 04-17-2008 9:22 AM

Brilliant work. So nice to see a non-workaround solution, so many people seem to forget that most Microsoft .Net products are designed to be extensible.

Quote: .Net helps those who help themselves.

test wrote re: SharePoint's core.css stylesheet: Put it to where it belongs!
on 06-02-2008 10:49 AM

Idea

Paisleygo wrote re: SharePoint's core.css stylesheet: Put it to where it belongs!
on 06-17-2008 3:07 PM

it seems if you DON'T follow the SharePoint "standard" approach and add your StyleSheet using the <SharePoint:CssRegistration> webcontrol

you can avoid this altogether

This is what I use and my styles override as you would expect-

<link rel="stylesheet" type="text/css" href="<%$SPUrl:~SiteCollection/Style%20Library/mystyles.css

Did I miss something?

Arjun wrote re: SharePoint's core.css stylesheet: Put it to where it belongs!
on 07-09-2008 1:18 PM

I went ahead and wrote the code for it.. essentially replaced Orginalsw with sw and changed the other line to reorderedStyleSheetsIdea = styleSheets[i - 1] + "\n";

After following these steps, nothing gets rendered. Am I missing out on something ?

lightbulb test wrote re: SharePoint's core.css stylesheet: Put it to where it belongs!
on 07-22-2008 10:22 AM

Idea

lightbulb test wrote re: SharePoint's core.css stylesheet: Put it to where it belongs!
on 07-22-2008 10:23 AM

Idea

lightbulb for blind people wrote re: SharePoint's core.css stylesheet: Put it to where it belongs!
on 07-22-2008 10:30 AM

[_i_]

and remove underscore

(sorry for double post)

self-made wrote re: SharePoint's core.css stylesheet: Put it to where it belongs!
on 08-29-2008 2:28 PM

I have applied your solution. It seems be pulling my last LINKED style sheet and moving it to the top then appending the core.css on the bottom still.  Do you know whey this might be happening?

Ayman El-Hattab wrote re: SharePoint's core.css stylesheet: Put it to where it belongs!
on 08-30-2008 2:50 PM

Perfectttttttt!!!!!!!!!

Chris wrote re: SharePoint's core.css stylesheet: Put it to where it belongs!
on 10-13-2008 6:14 PM

This is unnecessary. When you specify a alternate stylesheet in the choose master page section it puts it last.

Julius Serdone wrote re: SharePoint's core.css stylesheet: Put it to where it belongs!
on 01-09-2009 1:58 AM

I think Chris is right. I just specified an alternate CSS file in my Masterpage and then checked the generated HTML code of my page, the CSS file I specified was listed last.

Feuille de styles | hilpers wrote Feuille de styles | hilpers
on 01-17-2009 2:12 PM

Pingback from  Feuille de styles | hilpers

April Wolfe wrote re: SharePoint's core.css stylesheet: Put it to where it belongs!
on 03-11-2009 11:17 AM

Chris is right, but core.css is still at the bottom and hinders performance

Ofer gal wrote re: SharePoint's core.css stylesheet: Put it to where it belongs!
on 06-20-2009 5:40 PM

What is wrong with changing the Core.css itself?

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.