Custom Styles for SharePoint 2010 Rich Html Field

October 27 2010 27 comments

A typical requirement from a client is to be able to incorporate their own custom styles for the Rich Html Field. Allowing their public relations department to apply nice text formating for paragraphs with some Comic Sans Hotness might not be the best practice in here, so let’s be careful out there. The best scenario is only to allow custom formatting for headings, paragraphs and maybe a few different table styles but let’s go!

First, open up the page layout containing the HTML editor and add PrefixStyleSheet property for the RichHtmlField:

<PublishingWebControls:RichHtmlField
    id="Content"
    FieldName="PublishingPageContent"
    PrefixStyleSheet="my-rte"
    runat="server"
/>

Now, the default value for the property is ms-rte. We are going to redefine this to my-rte. Do not use any uppercase characters in the property value as apparently those wont work. Deploy the page and open it in edit mode to see the desired effect. The styles collections are now empty in the Ribbon.

We now have to define our own styles prefixed with the string my-rte. I’d suggest that you put these style declarations in a separate stylesheet to avoid javascript errors caused by complicated and hard-to-parse stylesheets. Let’s name our new stylesheet rte.css and add a few declarations there.

The Markup Collection

Let’s say I want to have an option to add a level 1 heading or an H1 tag for the given page via the Markup Styles dropdown list:

Step 1 – introduce the tag:

H1.my-rteElement-H1
{
  -ms-name:"my level 1 heading";
}

Step 2 – add the styles to the tag:

.my-rteElement-H1
{
    font-size: 150%;
}

Save the rte.css and make a reference for it in your master page, page layout or even use an alernative css. If you want your clients to be able to edit the styles themself, you could upload the rte.css in the Style Library and reference it there.

I have saved the file directly in the 14-hive /_layouts/my/ -folder and use a quick reference in the master page:

<SharePoint:CssRegistration
    ID="CssRegistration1"
    runat="server"
    Name="/_layouts/my/rte.css"
/>

Deploy and reload the page, fire up the edit mode and navigate to the ribbon where you can see your new style in action.

Your first custom style


Adding new elements is easy once you get to know the logic. Here’s an example of a parapgraph with nice rounded corners and superb font on modern browsers (note that I have combined the introduction and style values in one declaration):

P.my-rteElement-P
{
    -ms-name:"My CSS Rounded Paragraph";
    background: #808;
    color: #fff;
    border: 3px solid #f0f;
    text-transform: uppercase;
    text-align: center;
    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;
    box-shadow: 0 0 20px #000;
    -moz-box-shadow: 0 0 20px #000;  
    -webkit-box-shadow: 0 0 20px #000;
    padding: 10px;
    font: bold 2em Comic Sans MS;
}

Even Grammatical Errors Wont Silence the Comic of Sans

The Styles Collection

The Styles collection can be populated with the same way by changing the Element-suffix into Style ie.:

.my-rteStyle-MePinky
{
  -ms-name:"I am pink";
  color: #f0f;
}

This (P)arapgraph is Pink

Other Items

I’d suggest that you check out the default HTML editor styles in /Style Library/en-us/Themable/Core Styles/htmleditorstyles for quick reference how to style the other elements, including table styles on the Ribbon.

Popularity: 12% [?]

27 comments to “Custom Styles for SharePoint 2010 Rich Html Field”

  1. Marcy Kellar says:

    You guys are killing me with the comic sans!!

    But great blog post! kudos!

  2. Brad says:

    Comic Sans – That’s the only font I’ll use from now on. You guys rock!

    Once I realised that this is great for “Everything but IE” I had a quick hunt around and found this – http://css3pie.com/ – works alongside the -WebKit and -moz functionality and kicks the goals I want. Useful for people developing solutions for clients with an IE install-base.

    Cheers!

  3. Daisy says:

    Great article~! But how can we open up the page layout containing the HTML editor at the first place?

  4. Han says:

    This is great, I got this working as described for the Rich Html Field in a page layout, but how do we show the custom styles for the Content Editor (Rich Text) web part when inserted by the author?

  5. Shawn Z says:

    Custom HTML Tags in SharePoint
    To add custom HTML tags such as meta description, keywords, and titles, you will want to edit the page in SharePoint Designer. We first need to start be verifying the existing page and template are in good form and readable by SharePoint. Skipping this step can lead to erroneous error.
    1) Open SharePoint Designer
    2) Edit in Advanced Mode
    3) Goto Design View — This will test the pages and all problems denoted need corrected.
    4) Go to code view and add the custom Title in “asp:Content “ named “PlaceHolderPageTitle”. This should be the TITLE Content /TITLE, omit the title tags.
    5) META Description goes in the “asp:Content” named “PlaceHolderAdditionalPageHead”. This will be the actual HTML code.
    When I followed the directions provided by others, I had a small problem in the ASP which provided an accurate error by following steps 1-3.

    Shawn Zernik
    Internetwork Consulting

  6. Shawn Z says:

    To add custom HTML tags such as meta description, keywords, and titles, you will want to edit the page in SharePoint Designer. We first need to start be verifying the existing page and template are in good form and readable by SharePoint. Skipping this step can lead to erroneous error.
    1) Open SharePoint Designer
    2) Edit in Advanced Mode
    3) Goto Design View — This will test the pages and all problems denoted need corrected.
    4) Go to code view and add the custom Title in “asp:Content “ named “PlaceHolderPageTitle”. This should be the Content, omit the title tags.
    5) META Description goes in the “asp:Content” named “PlaceHolderAdditionalPageHead”. This will be the actual HTML code.
    When I followed the directions provided by others, I had a small problem in the ASP which provided an accurate error by following steps 1-3.

  7. Tom K. says:

    Thank you for this post!

  8. Hmmm says:

    How about the Position dropdown (Fin: “Sijainti”) for an image: which styles belong under that? I made a test and I got the button blank now. Image *Styles* I got back by finding and adding appropriate styles from the original htmleditstyle file. But I thought the original ms-rte-Position-1, -2, and so on would be about the Position button. I renamed them according my new file, but the button stays empty.

  9. Hmmm says:

    This was solved by me, and it was about naming and alternative css…

  10. Bo Huge says:

    I’ve looked up the table styles in the above mentioned css file. And have tried to define my own set og styles. Which works like a charm. But for some reason, I cannot get the table styles to show in the table style menu. My prefix is added and the styles I have added are there, but I cannot select among them in the menu. Any ideas?

  11. Cutty says:

    Thank you, thank you, thankyou. Have been trying to get my custom styles in the drop down for weeks and this is the only article that mentions that the css style needs to have “Style” appended to the custom style prefix in order for it to appear in the list.

    Something so simple, yet Microsoft make it so frustrating…grrrr.

  12. Yeah its a lil’ off topic lol you spotted a couple of typos about halfway thr

  13. Soni says:

    Anyone know how to let the content author remove a style/markup.

    I can get the styles/markup into the dropdowns, but i can’t remove the inserted class, without going into the html-Edit html source dialog.

  14. Soni says:

    Never mind, finally figured out that selecting my custom style with the Select Command and then using Clear Format will remove my style. Apparently i never got the style selected properly :(

  15. Victor says:

    I am developing website with a customized masterpage.
    Anyone knows how I can set PrefixStyleSheet property for RichHtmlField

    As I did not use a pagelayout for the site.

    Thanks

  16. Thierry says:

    I need to change default font size for the wiki pages of the site I am community manager in my (large) company. Verdana 8pts is definitly unreadable ! Does this post permit to do it for my site only or will other sites be impacted as well ? My IT department told me that it is impossible to change default font for my site without changing it for others. What do you think ?

  17. Tuukka Uskali says:

    Thierry, you could change the font by using an alternative css for that site(with publishing features enabled) or just add a few wiki-page specific common css rules for your default css. Use your browser of choice and fire up developer tools to look for common elements only visible on the wiki pages and override their default styles on your own custom stylesheet.

    With enterprise wiki it usually goes like:

    .col-fluid-1 p { font-size: 1.1em !important; } or .edit-mode-border { … }

    ..or you could use the adjacent sibling selector:

    .ewiki-margin+div { font-size: 14px; }
  18. This piece of writing is really a good one it assists
    new the web viewers, who are wishing in favor of blogging.

  19. clear says:

    This article will help the internet visitors for
    creating new website or even a blog from start to end.

  20. Issac Hsueh says:

    Needed to send you this tiny observation so as to say thank you as before just for the precious concepts you’ve contributed on this website. This is so shockingly generous of you giving unreservedly exactly what numerous people would’ve offered for sale as an e-book to help make some profit for their own end, and in particular now that you could possibly have tried it in the event you desired. The strategies in addition served to become a great way to be sure that most people have the same interest really like my personal own to learn significantly more on the subject of this issue. I’m certain there are lots of more pleasant opportunities ahead for folks who view your website.

  21. Ofer Gal says:

    I have this working for my customer but now he wants one of the styles to be the last in the drop-down.
    In the special CSS file, I have it as the last entry but SharePoint Does not care.

    Anyone know how to force an order?

  22. Nha Trang says:

    Thank you so much a bunch with regard to giving that with all of persons you truly determine what you happen to be discussing! Bookmarked. I implore you to moreover seek advice from this site Is equal to). Organic meat have a link transform set up involving us all

  23. Krutika says:

    Hi,
    I am trying to achieve same functionality for Wiki page library.
    I have modified EnterpriseWiki.aspx in sharepoint designer and have added the PrefixStyleSheet=”my-rte” in the same.

    My CustomStyle.css file is stored in the StyleLibrary.

    I have added class as mentioned above for hte rounded paragraph in my css.
    When opening page in edit mode from wiki page library. It still shows the preexisting styles. and doesnt show up the style as described above.

    Am I doing something wrong ? Please help!

    Krutika

  24. In addition they have a wide range of parts you can purchase.
    CORPORATE PROFILE: Since 1989: Restoration Environmental, one
    of Canada’s leading GREEN specialty contractors in Asbestos Removal, Demolition and Disaster Recovery,.
    The four latest artworks include ‘The Trunk’, ‘Franklin and Chop’, ‘Stop and Frisk’ and ‘Vinewood’.

  25. Striped candies and the color bombs are your friends here.

    The second best combination is to mix a wrapped candy with a striped candy,
    so that you can clear entire rows and columns when detonated.
    Then may take some practice, but it is definitely worth the effort.

  26. Again, just make sure you input the required information – your name on Facebook and a link to the tweet, respectively – so we
    can verify your entries are valid. Next question is probably where do you store that piece of paper
    or file. Security hacks have occurred at Microsoft,
    Google, Microsoft and Amazon.

  27. While other sites place a heavy emphasis on social connections or participating within the
    development of new objects, Blue Mars offers plenty of
    tools to serious gamers that want to develop new video games.
    Social video games are moreover extremely popular on this website.
    If possible try some more and let your head touch your knees, again without bending your knees.

Leave a Reply