How To Override Styles in a RadControl for ASP.NET AJAX' Embedded Skin

Tuesday, June 17, 2008 by ASP.NET AJAX Team | Comments 19
In the era of the "classic" RadControls for ASP.NET modifying a skin required opening

~ / RadControls / [ControlName] / Skins / [SkinName] / styles.css

and making the necessary changes. The new RadControls for ASP.NET AJAX (formerly known as "Prometheus") by default use skins, which are embedded in the assembly. This simplifies deployment but sacrifices customization. Or does it?

Actually, making a change to an embedded skin of a RadControl for ASP.NET AJAX is quite easy, given that one keeps in mind a couple of notable things. Namely:

(1) Since the embedded skin cannot be modified, the custom styles need to be placed elsewhere. For example  in a <style> tag in the <head> section of the page or in an external CSS file (no matter whether in a АSP.NET theme or not). Using external CSS stylesheets is highly recommended, so that structure and content are properly separated from presentation.

(2) The embedded skins are registered automatically on the web page using <link> tags, which are appended at the end of the <head> section of the web page source HTML. In other words, the embedded skins are always applied last by the browser.

 

Why does (2) matter? Browsers adhere to certain rules, when deciding which one of two (or more) conflicting CSS style declarations to apply. Simply put, if two CSS rules have the same specificity, the latter specified wins. (Very roughly stated, specificity is the number of HTML elements, CSS classes and Client IDs, which take part in a CSS selector. In turn, a selector is a single comma-separated chunk before the opening bracket "{" of a CSS rule. If there is only one chunk, there are no commas.)


So, since RadControls' embedded skins are always the last stylesheets to be appended, you must supply a rule with greater specificity in order to override an embedded skin style. Supplying a CSS rule with the same level of  specificity will not work. Here is an example:

In the Default RadGrid skin there is a CSS rule:

.RadGrid_Default 
    border:1px solid #828282
    background:#fff
    color:#333


The specificity of the CSS selector is 10 points (one CSS class is worth 10 points). If we want to override this CSS rule, we should use for example:

div.RadGrid_Default 
    backgroundyellow
    colorblue


The above selector has a specificity of 11 points (1 + 10) and this is enough to override the skin. Adding "div" to the selector is just a small trick - the RadGrid_Skin CSS class is always applied to divs, so the CSS selector will select the same HTML elements, but now the CSS rule has higher weight than the skin's rule.

Some people are aware that by using the !important clause one can a CSS rule of random specificity. For example:

.RadGrid_Default 
    backgroundyellow  !important ; 
    colorblue  !important ; 


However, using !important is not recommended, firstly because it is not needed, and secondly, because further overriding it later may become rather hard in certain occasions.

Here is another example, which discusses a slightly different aspect of skin overriding. We will use again RadGrid's Default skin:

.RadGrid_Default .rgAltRow 
    background:#f2f2f2

(specificity 20)

As we mentioned earlier, we can override the above CSS rule by adding an appropriate HTML element to the selector:

 
.RadGrid_Default tr.rgAltRow
    backgroundblack ; 
    coloryellow ; 

(specificity 21)

However, this will influence all RadGrids with the Default skin in our web application. We might want to override only several instances of RadGrid.

In this case the smart thing to do is assign a custom CSS class to all RadGrids to be modified:

<telerik:RadGrid  CssClass="MyCustomClass"  /> 


Now this CSS class can be used to create a custom CSS rule with a higher specificity:

 
.MyCustomClass  tr.rgAltRow
    backgroundblack ; 
    coloryellow ; 

(specificity 21)


Probably, there are 2 questions that come into your mind after reading the above:

1. How can one find out the specificity of the CSS rule, which has to be overridden?
We strongly recommend using Firebug for Firefox and/or Web Developer Toolbar for Internet Explorer. They are able to display all styles being applied to a selected HTML element, and where they come from.

2. How can one find out more about a control's HTML output and CSS classes, so that creating custom CSS rules becomes easier?
You can consult the related help articles in our online documentation, for example: RadGrid CSS classes, RadCalendar CSS classes, etc. In addition, all embedded CSS files for all RadControls are included in the installation packages - look for the Skins subfolder in the root folder of the installation.

Finally, here are some CSS specificity-related articles worth reading:

CSS Specificity Things You Should Know About

Cascading Order (W3C CSS Standard)

CSS: What Happens When a Conflict Occurs


19 Comments

  • M. Suresh 25 Jun 2008
    This is a great article! Thank you very much
  • Rasin Vadim 22 Jul 2008
    Great Article indeed. I just don't understand the benefits of the "Embedded Skin" any small change you want to implament to the appropriate control, the css gives you a very painfull headache!
  • Dimo, Telerik 23 Jul 2008
    Hi Rasin, The major benefit of embedded skins is that you need only one file to run RadControls for ASP.NET AJAX - the assembly. There is no RadControls folder to take care of. In addition, you can use RadStyleSheetManager or RadScriptManager to combine all CSS or Javascript resources to a single file, which works only if the resources are embedded in the assembly. On the other hand, if you prefer, RadControls for ASP.NET AJAX can also be used with external CSS and Javascript files, just like the classic RadControls for ASP.NET.
  • towps 10 Dec 2008
    nice.
  • Madhu Menon 10 Mar 2009
    Dear Friends,

    I am new to telerik RadControls for ASP.Net Ajax. My development team started using the same and we are very impressed with the scope.

    We have an issue for which we need a solutions. We are using the RadGrid for the master files to do the Add, Edit, Delete and Search on the same screen. All actions has to be done using Web services. We put our best efforts to get a solution for the same using ObjectDataSource. We have a dropdown control in the RadGrid. When we save the data only the Dropdownlist value is passed to the web services and not the Text. Is there a way to also transit the Text of  a dropdown to the web service.

    Please respond urgently. Any help will be high appreciated.

    Thank you
    Warm Regards
    Madhu Menon
  • Dimo, Telerik 05 May 2009
    Hello Madhu,

    For technical questions and support, please use our Forums or send us a formal support ticket from your account. Thank you.
  • Mike 26 Jul 2009
    This was a huge help.  I was struggling with the best way to override the telerik styles for a while and then came across this article.  This should be prominently featured within the help chm file.

    Thanks!
    Mike
  • E 25 Jan 2010
    cancelling the old mechanism of having a RadControls directory has caused us great difficulties. since we were creating different skins for different clients (each had his own theme). It's a pity telerik does not offer some backward compatability. Now we have to use all kinds of hackings instead..
  • Jaimie Lisa Stow 18 Mar 2010
    i have visited your site lots for guidance - the best description of how to override those darned rad controls - i dont use them but i need to design around and replace alot of them which our coders use..... wonderful help - thank you
  • Jaimie Lisa Stow 18 Mar 2010
    i have visited your site lots for guidance - the best description of how to override those darned rad controls - i dont use them but i need to design around and replace alot of them which our coders use..... wonderful help - thank you
  • Phi Hoang Hai 20 May 2010
    Hi
    I've just created my custom skin of RadMenu.
    How can I override class
    ul.rmRootGroup {
    display:block;
    /* padding-top : 3px;  I want to add this line */
    defined in WebResource.
    Thanks you.
  • Dimo 21 May 2010
    Hello Phi Hoang Hai,

    Normally such questions should be posted in the forums or in a support thread.

    In your case you have the following CSS rules in the RadMenu base stylesheet, which should be overridden:

    .RadMenu  .rmRootGroup
    {
        padding:0;
    }

    .RadMenu  ul.rmVertical,
    .RadMenu  ul.rmHorizontal,
    .RadMenu  ul.rmRootScrollGroup,
    .RadMenu_Context  ul.rmHorizontal
    {
        padding:0;
    }

    The highest specificity is 21, so you should have a CSS selector with a specificity of 22 or more:

    div.RadMenu_SkinName  ul.rmRootGroup
    {
        padding-top : 3px;
    }

  • Sanjay Patel 16 Jul 2010
    Hello ,

    I have Radcombo box, when i select value from list, text of selcetedItem is bottom align, i have to give some bottom padding so i can see selected text in middle of combo.

    I have try to override following script of radCombo box, but it not override , it always get script from web resouces CSS.



    RadComboBox .rcbInputCell .rcbInput
    {
    background:none repeat scroll 0 0 transparent;
    border:0 none;
    outline:0 none;
    padding:2px 0 4px;
    vertical-align:middle;
    width:100%;
  • Dimo 16 Jul 2010
    Hello Sanjay,

    As suggested above, such questions should be posted in our forums or in a support ticket. While doing so, please include also the CSS code that you are using to override the RadComboBox embedded styles. Thank you.
  • Kabuitu 10 Nov 2010
    thanks dimo, u've saved my day!!!
  • Jim 14 Feb 2011
    Dimo, your article is very helpful, thanks.

    I am in the process of customizing multiple telerik controls, RadGrid and RadTabStrip so far... This means I will register more than one css file via the <link tag, like this:

        <link href="../RadControls/Grid/Skins/Custom41/Styles.css" rel="Stylesheet" runat="server" type="text/css" />
        <link href="../RadControls/Tabstrip/Skins/Custom41/styles.css" rel="Stylesheet" runat="server" type="text/css" />


    May my CSS files use the same name?  That is, may I use styles.css for the grid and styles.css for the radtabstrip?  They are in two different locations so the path to each will be unique. 

    Thanks.
    Jim
  • Dimo 15 Feb 2011
    Hi Jim,

    Yes, you can register numerous CSS files with the same name, because their location and URLs are different.

    Regards
  • Regin 04 Apr 2011
    Great! Really helped me override a particular part of CSS for a control. Thanks.
  • R. Rivas 29 Mar 2012
    As helpful as this is, it shows an over complex solution to what should be a simple issue.  If the system does something like this where one is wasting their time setting up all those properties in the rad grid "designer" then it really means that the framework is very flawed.  The instruction to "use firebug" to look up settings is a waste of time if it should have been DONE and DOCUMENTED by Telerik a LONG time ago (it has been a long time and more than enough time for a temp to write it up ... or to get the architect who designed this "system" to doi their job and finish DOCUMENTING their work.  This like this make us turn back to older or controls or pure HTML with JQUERY because the UX designers out there are not going to know this kind of nuance and explaining it to a non control user makes you sound, frankly, bad. 

Add comment

  1. Formatting options
       
     
     
     
     
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)