Using Salesforce Styles

Many Visualforce components already have the look and feel of the same components in Salesforce, such as the related list in a detail page, or a section header. However, part of the styling of these components is based on the tab on which the component appears, including the component's color scheme. You can specify the tab style that should be used to style a component by associating a page with a standard controller or by setting the tabStyle attribute on the <apex:page> or <apex:pageBlock> tags:For more information on customizing the Salesforce user interface, see Customizing User Interface Settings.

Extending Salesforce Styles

You can use the <apex:stylesheet> tag to add additional styles and style classes to page components. This way you can extend the Salesforce styles with your own.

The following markup shows a very basic page. The <apex:stylesheet> tag references a CSS style sheet that is saved as a static resource named TestStyles in Your Name | Setup | Develop | Static Resources. It is referenced by the $Resource global variable in the <apex:stylesheet> tag's value attribute. The styleClass attribute of the <apex:outputText> tag uses the sample style class defined in the style sheet.

<apex:page>
   <apex:stylesheet value="{!$Resource.TestStyles}"/>
   <apex:outputText value="Styled Text in a sample style class" styleClass="sample"/>
</apex:page>

This is the style sheet used for this example:

.sample {
        font-weight:bold;
}

Identifying the Salesforce Style Your Users See

When you're creating a Visualforce page, it's often useful to know the Salesforce look and feel your user expects, in order to render a page that matches their style. For example, some users have the choice to customize their look and feel. You'll need to design your Visualforce pages to take these differences into consideration.

There are two global variables that can help you identify which style a user sees: $User.UITheme and $User.UIThemeDisplayed. The difference between the two variables is that $User.UITheme returns the look and feel set by the user, while $User.UIThemeDisplayed returns the actual look and feel. For example, a user may have the permissions to see the new user interface theme look and feel, but if they are using a browser that doesn't support that look and feel, $User.UIThemeDisplayed returns a different value.

Both these variables return one of the following values:
  • Theme1—Obsolete Salesforce theme
  • Theme2Salesforce theme used prior to Spring '10
  • PortalDefaultSalesforceCustomer Portal theme
  • WebstoreSalesforceAppExchange theme
  • Theme3—Current Salesforce theme, introduced during Spring '10
Suppose a developer has hard coded some CSS styles to resemble Salesforce. In order to preserve the same look and feel on the Visualforce page for new styles, the developer needs to select between several stylesheets to handle the preferences of the user. The following example shows one possible way of accomplishing this:
<apex:page standardController="Account">
    <apex:variable var="newUI" value="newSkinOn" rendered="{!$User.UIThemeDisplayed = 'Theme3'}">
        <apex:stylesheet value="{!URLFOR($Resource.myStyles, 'newStyles.css')}" />
    </apex:variable>
    <apex:variable var="oldUI" value="oldSkinOn" rendered="{!$User.UIThemeDisplayed != 'Theme3'}">
        <apex:stylesheet value="{!URLFOR($Resource.myStyles, 'oldStyles.css')}" />
    </apex:variable>
    <!-- Continue page design --> 
    
</apex:page>
Notice in this example that:
  • Using the rendered attribute you can “toggle” which sections display
  • Since the <apex:stylesheet> tag doesn't have a rendered attribute, you'll need to wrap it in a component that does
Even if a new look and feel is enabled for your users, they may not be running the right browser or accessibility settings to see it. Here's a code example that makes use of the $User.UITheme variable to present alternate information to the user:
<apex:page showHeader="true" tabstyle="Case">
    <apex:pageMessage severity="error" rendered="{!$User.UITheme = 'Theme3' && 
                                                    $User.UIThemeDisplayed != 'Theme3'}">
    We've noticed that the new look and feel is enabled for your organization. 
    However, you can't take advantage of its brilliance. Please check with 
    your administrator for possible reasons for this impediment.
    </apex:pageMessage>
    <apex:ListViews type="Case" rendered="{!$User.UITheme = 'Theme3' && 
                                           $User.UIThemeDisplayed = 'Theme3'}"/>
</apex:page>
Notice that although $User.UITheme equals Theme3, $User.UIThemeDisplayed doesn't, and so the page won't render to its full potential.

Using the Salesforce Cascading Style Sheets

Warning
Salesforce stylesheets are not versioned, and the appearance and class names of components may change without notice. Salesforce.com recommends that you use Visualforce components that mimic the look-and-feel of Salesforce styles instead of referencing Salesforce stylesheets.

Salesforce uses different stylesheets (.css files) throughout the application to ensure that every tab conforms to the Salesforce look and feel. When you specify true for the header attribute of the <apex:page> tag (or leave it blank, as the default is true) these stylesheets are automatically included. You can reference these stylesheets to further customize the components on your page. This is relevant when you use a custom controller and you do not set the tabStyle attribute on the page.

The following stylesheets contain the style classes that you can reference. They are located in the /dCSS/ directory of your salesforce.com instance.
  • dStandard.css – Contains the majority of style definitions for standard objects and tabs
  • allCustom.css – Contains style definitions for custom tabs

More information on the Salesforce style sheets is available on the Developer Force website at http://wiki.developerforce.com/page/Using_the_Salesforce_CSS_in_Your_Apps.

© Copyright 2000–2012 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.