Go Back

Adding Custom Styles to Sitefinity's RadEditor

Sitefinity ASP.NET CMS is a web-based content management platform and Telerik's RadEditor plays a key role in managing the web site's content.  RadEditor is a WYSIWYG web-based tool that makes it very easy to create rich-text content.

In my last post, I showed how to add new tools to Sitefinity's RadEditor.  Among these tools are tools that allow editors to set the font, font size, font color and background color.

However, it's not recommended that editors be allowed to arbitrarily use any font settings.  Doing this results in extra HTML (embedded styles) and inconsistent presentation.  Instead, it's recommended that editors choose from a list of carefully selected stylesheets.

Identify the Styles to Apply to RadEditor

Before applying a new CSS style to RadEditor it's important to understand what can go wrong.  Take, for example, the following style definition:

body {
    background: #fff url(Images/bg.jpg) repeat-x top left;
    color: black;
    font-size: 25px;
    margin:5px;
}

Notice the background image that will be tiled onto the background of the web page.  Adding this stylesheet to RadEditor means that RadEditor will also use this background:

RadEditor with Bad Custom Styles

This probably isn't the result you're looking for.  RadEditor will read the entire CSS file and apply it to the RadEditor window.  This will include the custom styles we want, but also the styles we don't want.  I recommend keeping RadEditor as clean & free of distraction as possible. 

In order to do this, identify the styles that should be applied to RadEditor.  Once identified, place these styles into a separate CSS file.  Below is an example:

~/Custom/Admin/Common/radeditor.css

/* HTML STYLES */
body {
    color: black;
    font-size: 15px;
}
h1 {
    font-family: Georgia;
    font-size: 35px;
    color: #fff;
    margin: 0 10px;
}

/* CUSTOM STYLES */
.important {
    color: Red;
    font-size: 14px;
    font-weight: bold;
}
.legal {
    color: Gray;
    font-size: 11px;
}
.quote {
    background-color: #eeeeee;
    font-style: italic;
    padding: 10px;
}

Notice:  Because this file resides outside the ~/App_Themes folder it won't be applied to the web site.  In fact, the file above will only be used by RadEditor.  This means these styles will need duplicated in the CSS file(s) used by your web site.

Alternately, you can put this file in your ~/App_Themes theme folder.  Just be aware that these styles will then be applied to your public web site.

Applying Custom Styles to RadEditor

Figuring out what styles to apply to RadEditor is the hard part.  Once finished, it's easy to point Sitefinity's RadEditor to your custom stylesheets. 

Open the ~/Sitefinity/Admin/ControlTemplates/EditorToolsFile.xml file.

Step 1:  Add a mapping to the CSS file by adding a <cssFiles> section to the <root> section:

<cssFiles>
    <item name="~/Custom/Admin/Common/radeditor.css" />
</cssFiles>

This section tells RadEditor how to find your CSS file. 

Step 2:  Add the custom styles that can be applied in RadEditor to the existing <classes> section:

<classes>
    <class name="Important" value=".important" />
    <class name="Quote" value=".quote" />
    <class name="Legal" value=".legal" />
</classes>

Only custom styles need added to this list.  Regular HTML styles (body, strong, p, etc.) will be applied automatically.  These are the styles that will appear in the styles drop-down list.

Step 3:  Add the ApplyClass tool to the RadEditor toolbar by adding the following <tool> item to one of the existing <tools> sections:

<tool name="ApplyClass"/>

Where you place this <tool> will change where the tool visually appears in the RadEditor toolbar.

Conclusion

Configuring custom styles in RadEditor is a wonderful way to empower editors while still producing good HTML and maintaining a consistent visual style.

 RadEditor using Custom Styles

For more information:

External CSS Files in RadEditor

Using RadEditor's ToolsFile.xml

Comments  4

  • Simon Martin 18 Mar

    Is there a reason why you don't advocate separate style sheets e.g. one for layout and another for typography (for example), that way you would just need to point RadEditor at the typography CSS (in App_Themes) and not worry about updating two separate CSS files.
  • Gabe Sumner 19 Mar

    The only (very small) problem I could see with this is that it involves an additional round-trip to the server. In other words, visitors to your web site must fetch 2 files instead of 1 file. If you use Yahoo's YSlow utility it complains about stuff like this. This being said, for the average web site this doesn't matter at all! Your suggestion is probably ideal for 95% of web scenarios.
  • Sean Molam 26 Mar

    Hi Gabe, What do you think the chances are of with Generic Content that the editor would instead of opening up in a new window instead open up within the Page Editor and take on the styles of pages theme etc. It is the one small gripe I get from the less imaginative of my customers who can't imagine how the content will look until after they have saved. And as you say it is quite difficult to maintain 2 style sheets one for editing and one for rendering. I think if Sitefinity could achieve this then it would round off what makes Sitefinity the easiest CMS to use and that is its Page Editing experience. Cheers Sean
  • Steven Harrap 04 Mar

    Is there anyway to have the css classes in the editor associated with the theme that is currently applied to the template in which the editor is being placed?... A bit of a mouthfull. To expand on this:

    - I have 1 template and several themes that I can apply to that template
    - Each theme has a css file which renders the ids and classes of the content in the template in a different way
    - When I open the Generic Editor on a page I would like it to display only the CSS classes that I have defined for the content of the current theme.

    Currently I have to define a reference to the CSS file in the EditorToolsFile.xml and define a list in that same file for which classes should be displayed in the drop down. Perhaps something like this

     

    <cssFiles>
    <usecurrenttheme cssfile="radeditor.css" displaylist="radeditorcssclasslist.xml" />
    </cssFiles

    radeditor.css and radeditorcssclasslist.xml would both sit inside the theme folder of the theme currently in use for the template...

    Cheers,
    Steven.

Post a comment!
  1. Formatting options
       
     
     
     
     
       
  2. I'm sorry for the CAPTCHA. You have spammers to thank for this: