[SalesForce] How to ignore Salesforce CSS on VF Page (with header)

I've been working on a Visualforce page using remoting, and am trying out building a custom UI to go along with it (not using any VF tags for the UI). While I'm aware that you can leverage standardStylesheets=false if you turn off the header to get rid of all the Salesforce CSS, I am curious how to leverage the Salesforce header but ignore the Salesforce CSS in my VF implementation. I'm hoping to maintain the header to give the illusion that my page fits into the desktop application and can be navigated to/from.

I noticed that if I use the apex:stylesheet tag, it gets loaded ahead of the Salesforce CSS includes when I look at it in the markup, so the Salesforce CSS is overriding anything I set (for instance, the h1 tag) unless I declare it inline.

I assume one way that you could handle this is making your implementation page have no header, and then have a wrapper VF page that has the header and pulls in your main VF page via an iframe, but that opens another can of worms around navigation. Is there a best practice on how to handle this?

Best Answer

You won't be able to ignore the Salesforce CSS as it will be loaded by the browser and thus used whenever there is a class that matches the name.

You don't need to use an <apex:stylesheet /> tag to include a stylesheet in your page, you can just use a regular tag. This isn't supported by the specification, but browsers will accept it.

You could also put the styles directly in the page, which again isn't supported by the specification but the browser will accept it, or use the HTML5 scope attribute on a mid-document style. You do lose the benefits of CSS this way though, as the styles are in the page and thus not cached or reusable. It looks like browser support is pretty patchy for this too.

Adding !important to your styles is a potential way to ensure that your styles have precedence over the Salesforce styles regardless of order, but there are a couple of downsides:

  1. This tag was introduced as an accessibility feature to allow users with particular needs (e.g. large fonts due to impaired vision or different colour combinations due to colour blindness) to apply their preferred styles to a page, so your important styles could well break the page for them.
  2. Its more difficult for someone to customise the style on your page - for example, they add a style class to the page but it has no effect because somewhere in a stylesheet the important tag has been used and that takes precedence.

The way I tend to approach this though is to use JavaScript to append my tag to the existing ones, typically through the jQuery method outlined in the answer to this question:

https://stackoverflow.com/questions/12093509/jquery-add-css-to-head

Related Topic