Visualforce css developer guide(by forcetree.com)

23,409 views

Published on

This document guides you through your first days of styling your Visualforce Pages with a custom look and feel.

Published in: Technology, Education
0 Comments
9 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
23,409
On SlideShare
0
From Embeds
0
Number of Embeds
7,333
Actions
Shares
0
Downloads
273
Comments
0
Likes
9
Embeds 0
No embeds

No notes for slide

Visualforce css developer guide(by forcetree.com)

  1. 1. Forcetree.com<br />Visualforce CSS User Guide – Tips for CSS implementation in Visualforce Pages<br />
  2. 2. VisualForce offers the ability to customize the look and feel of your VisualForce Pages. This guide is intended to cover the methodologies to be followed, and some best practices while implementing your own look and feel using custom styles.<br />Do visit http://www.forcetree.com (my personal blog) for more examples and articles.<br />Abstract<br />
  3. 3. <apex:page>is the topmost tag in any VisualForce Page. This tag determines the styles to be used in your VisualForcepages. Let us start by creating a simple page with some basic styles. <br /><apex:pageshowheader="false" sidebar="false"> <br /> <apex:form><h1> This is my first VisualForce Page! </h1><br /> </apex:form></apex:page><br />Let’s walk through the code line by line <apex:pageshowheader="false" sidebar="false">When you set showheader=“false” the sidebar is also removed by default, however when you set sidebar=“false” the header will still display (obviously )<br />The point to note here is when the header is removed Salesforce default stylesheets is no longer applied.<br />Hence if you paste ordinary HTML code it will work perfectly.<br />Getting Started<br />
  4. 4. One step further…..<br />Let’s modify the code and add a button..<br /><apex:pageshowheader="false" sidebar="false"><br /> <apex:form><br /> <h1> This is my first VisualForce Page! </h1><br /> <apex:commandButton value="Done"></apex:commandButton><br /> </apex:form><br /> </apex:page><br />What did you notice? You’re right, the size of the text has reduced when you have not done anything to it.<br />Why is this so?<br />It is because we have used <apex:commandbutton> tag. This causes salesforce Standard stylesheets to be used. Not only this tag, but there are many others which cause a conflict. Hey!!, don’t worry there’s a way out there.<br />Getting Started (cont..)<br />
  5. 5. <apex:pageshowheader="false" sidebar="false" standardstylesheets=”false”><br />This will remove the salesforce standard stylesheets and you can now use your own styles without any worries. But, of course there are many more problems to be faced… Now lets add some color and effects… <br /><apex:pageshowheader="false" sidebar="false"><br /> <apex:form><br /> <h1 style="text-align:center;color:blue;"><br /> This is my first VisualForce Page! </h1><br /> </apex:form><br /></apex:page><br />Output:<br />This is called INLINE STYLING, it is called so because we have used style=” “ attribute inside the <h1> tag<br />STYLING….<br />
  6. 6. When not to use INLINE STYLING????<br />Suppose if you have ten <h1> tags in your page and you want all the text displayed in BLUE. Ideally you would have to add INLINE styles in all your ten <h1> tags.Toovercome this, we use <style> tag. We will modify our previous example using <style> tag.<br /><apex:pageshowheader="false" sidebar="false"><br /><style><br /> h1<br /> {<br />text-align:center;<br />color:blue;<br /> }<br /></style><br /><apex:form><br /> <marquee> <h1> This is my first VisualForce Page! </h1> </marquee><br /> <h1> This text is displayed without INLINE styles </h1><br /></apex:form><br /></apex:page> OUTPUT:<br />STYLING….<br />
  7. 7. USING STYLE CLASS….<br />How about a more complicated page with styleclass?. Adding many styles into a single holder is called a styleclass. You can then use the styleclass anywhere in your page<br /><apex:pageshowheader="false" sidebar="false" standardcontroller="Account" standardstylesheets="false"><br /><style><br />.red<br />{ background-color:red;}<br />.green<br />{ background-color:green;}<br />.blue<br />{ background-color:blue;}<br /></style><br /> <apex:form ><br /> <h1 style="text-align:center;color:blue;"> This is my first VisualForce Page! </h1><br /> <apex:datatable value="{!Account.Contacts}" var="con" border="1" cellspacing="5" cellpadding="5" columnClasses="red,green,blue"><br /><apex:columnheadervalue="Contact FirstName"> <apex:outputfieldvalue="{!con.FirstName}"/> </apex:column><br /><apex:columnheadervalue="Contact LastName"> <apex:outputfieldvalue="{!con.LastName}"/> </apex:column><br /><apex:columnheadervalue="Contact Name"> <apex:outputfield value="{!con.Title}"/></apex:column><br /> </apex:datatable><br /> </apex:form><br /></apex:page> OUTPUT:<br />
  8. 8. USING STYLE CLASS…. cont<br />In the above code sample when you say columnclasses="red,green,blue" in <apex:datatable> tag it means that the styleclass "red" would be applied for the first column, "green" would be applied forthe second column and "blue“ would be applied for the third column. When you have a fourth column "red" styleclass would be applied again, because it works in a repetitive fashion. You can use as many classses as you need for any number of columns.<br />
  9. 9. CSS with Visualforce<br />CSS – stands for Cascading Style Sheets. It is a file which ends with the extension “.css” (Ex: mystyles.css)<br />Why use CSS?<br />Readability : Using a CSS makes your code more readable. It separates your style definitions from your Visualforce page.<br />Reusability : You can use your CSS file across multiple visualforce pages, and whenever you need modifications to your styles changing the CSS file would change all your underlying Visualforce pages.<br />Let us modify the previous example using STYLECLASS and achieve the same using CSS.<br />Creating a CSS file:<br />Step 1: Open Notepad, or any text editor.<br />cont ….<br />
  10. 10. CSS with Visualforce (cont …..)<br />Step 2: Paste the following code<br />.red<br />{ background-color:red;}<br />.green<br />{ background-color:green;}<br />.blue<br />{ background-color:blue;}<br />Save this file as, “mystyle.css”<br />Step3: Upload this file into Static Resources, name the resource as “mystyle”.<br />
  11. 11. CSS with Visualforce (cont …..)<br />Step 2: Create a visualforce page with the following code<br /><apex:pageshowheader="false" sidebar="false" standardcontroller="Account" standardstylesheets="false"><br /><apex:stylesheet value="{!URLFOR($Resource.mystyle)}"/><br /><apex:form ><br /> <h1 style="text-align:center;color:blue;"> This is my first VisualForce Page! </h1><br /> <apex:datatable value="{!Account.Contacts}" var="con" border="1" cellspacing="5" cellpadding="5" columnClasses="red,green,blue"><br /><apex:columnheadervalue="Contact FirstName"> <apex:outputfieldvalue="{!con.FirstName}"/> </apex:column><br /><apex:columnheadervalue="Contact LastName"> <apex:outputfieldvalue="{!con.LastName}"/> </apex:column><br /><apex:columnheadervalue="Contact Name"> <apex:outputfield value="{!con.Title}"/></apex:column><br /> </apex:datatable><br /> </apex:form><br /></apex:page><br />The line in GREEN denotes the place where we actually reference our CSS file in our Visualforce Page<br />
  12. 12. CSS FOLDER with Visualforce<br />CSS Folder:<br />When you download/buy CSS from an external vendor like http://www.freecsstemplates.org/, the CSS usually comes as a bunch of files and folders as shown below. This is because your CSS uses IMAGES, and some other files, all of which are placed into the respective folders. For your CSS to work properly, you would need these folders.<br />In such a case, create a ZIP file of all the files shown above. For Example, Just place all the files shown above into a folder called “stylefiles”. Make a ZIP file of this, and call it “stylefiles.zip”. Now, upload this ZIP file into Static Resources with the name “stylezip”. Now, we will need to reference this CSS into our Visualforce Page.<br />
  13. 13. CSS FOLDER with Visualforce<br />Create a Visualforce Page with the following code.<br /><apex:pageshowheader="false" sidebar="false" standardcontroller="Account" standardstylesheets="false"><br /><apex:stylesheet value="{!URLFOR($Resource.stylezip ,'stylefiles/style.css')}"/><br /></apex:page><br />Note: “stylefiles/style.css”. Here, stylefiles is the folder name within which you have the style.css file. If you have the CSS within a different folder, you will have to specify the appropriate structure.<br />

×