R2i DotNetNuke Blog

 

CSS Placement Tip for Performance Enhancement

Monday, May 03, 2010 by Ian Robinson

Recently, a person I follow on Twitter was expressing concern over the fact that their custom CSS file wasn’t being placed in the appropriate part of the website’s generated markup.

initial tweet

And then a friend of mine provided a quick and easy solution:

follow up tweet

While this solution was something I knew about for some time, I took a look at a site I was working on and noticed I wasn’t even using it. There it was, a CSS file being referenced right in the middle of the page’s source code. If you’re wondering why this is an issue, or what we can do to address it, read on.

Yahoo! published a wonderful article called Best Practices for Speeding up Your Website, which outlines several performance oriented rules to follow while you’re constructing your website. One of those rules is “Put Stylesheets at the Top.

They also provide a tool, YSlow, which is a plug-in for the FireFox browser that helps developers analyze their site to see how well they follow these widely accepted (if not definitive) rules for performance.

YSlow provides a concise description on why you should follow the “Put Stylesheets at the Top” rule:

Moving style sheets to the document HEAD element helps pages appear to load quicker since this allows pages to render progressively.

So, you may be thinking that this rule is easy enough to follow, except for when it comes to your DotNetNuke websites. Not so fast!

The core DotNetNuke framework does, in fact, provide us with a very easy way to put the stylesheet at the top. If you’re referencing this stylesheet in a DotNetNuke skin or module you can simply call a framework method called “AddStyleSheet.”

This method allows you to supply the value for the id and href properties of the link element.

So, if you wanted to embed the following:

  html testcss

You could do the following in a user control in DotNetNuke:

testcss.jpg

Here is the method signature for the method being called. You can see it has two parameters, one sets the id of the generated link element, and the other sets the href parameter.

add stylesheet 1

In addition, you have the ability to determine whether the CSS file should be placed before the other CSS files present in the head. In that case, you would provide a third parameter with the value of true or false. Read more about CSS precedence and the particulars of CSS precedence in DotNetNuke.

add stylesheet 2

 By following these simple steps, you should achieve a better grade on your YSlow report card.

yslow css report

 
Comments

2500 characters remaining

Add Comment
Recent Comments

5 June 07, 2010

Thanks for the tip Ian, I didn't know about that method. I just want to say that using Snapsis PageBlaster is one of the quickest, easiest ways to boost your Yslow/Google Page Speed ranking - it does CSS merging AND minifies/compresses it AND moves it all to the header (in one css file) AND adds the cache expiry header to it... (in addition to 101 other cool performance things). I am not sure how much their free version can do but I would really be curious as too why someone is not running PageBlaster to make their life easier and improve performance? (ps - I know this is slightly off topic regarding a way to add .css to a page, but I thought I would mention it and got carried away ;)

4 May 27, 2010

Hey Ian, Cuong and Co.

I've been wanting a solution like this for some time now, so this article seemed like good news... However, I tried placing your control into my .ASCX skin file as follows:

<% ( (DotNetNuke.Framework.CDefault) this.Page) .AddStyleSheet ("TestCssFile", "test.css") %>

and it threw out errors. I'm not sure if the syntax is wrong, or if this is even how it's meant to be used in the first place.

Essentially, I'm looking for a solution that would place my additional skin CSS files in the head. But also, and more importantly, my jQuery calls in the head. Does this solution allow for this..? Is there another way to do what I'm looking to do..?

I have jQuery calls in my .ascx skin file as follows currently:

<script src="<%=skinpath%>js/jquery-sliderbox.js" type="text/javascript"></script>

This is not ideal. Ideally, these would be in the head.

Any help, direction and examples would be appreciated.

Thanks
Jon
 

Most Discussed

 

Subscribe to our blog

 

New York, NY • Baltimore, MD • Vienna, VA • St. Louis, MO • Seatle, WA • 410.327.0007 • info@R2Integrated.com

Bookmark & Share Bookmark and Share