I want to change the size of a PrimeFaces component. For example, a <p:orderList>. It has a class called ui-orderlist-list which is defined in primefaces.css with a fixed 200×200 dimension. No matter what I do in my theme.css, it is overwritten by this attribute and there is no way I can make the content part of a <p:orderList> wider.
For other components I might want to override just one instance of a component, not all.
Can anyone please tell me how can I do all this?
There are several things you need to take into account of which one or more might be relevant you your specific case
Load your CSS after PrimeFaces one
You need to ensure that your CSS is loaded after the PrimeFaces one. You can achieve this by placing the
<h:outputStylesheet>referencing your CSS file inside<h:body>instead of<h:head>:JSF will automatically relocate the stylesheet to the end of the generated HTML
<head>and this will thus ensure that the stylesheet is loaded after the PrimeFaces’ default styles. This way the selectors in your CSS file which are exactly the same as in PrimeFaces CSS file will get precedence over the PrimeFaces one.You’ll probably also see suggestions to put it in
<f:facet name="last">of<h:head>which is understood by PrimeFaces-specificHeadRenderer, but this is unnecessarily clumsy and would break when you have your ownHeadRenderer.Understand CSS specificity
You also need to ensure that your CSS selector is at least as specific as the PrimeFaces’ default CSS selector on the particular element. You need to understand CSS Specificity and Cascading and Inheritance rules. For example, if PrimeFaces declares a style by default as follows
and you declare it as
and the particular element with
class="ui-bar"happen to have a parent element withclass="ui-foo", then the PrimeFaces’ one will still get precedence because that’s the most specific match!You can use the webbrowser developer tools to find the exact CSS selector. Rightclick the element in question in the webbrowser (IE9/Chrome/Firefox+Firebug) and choose Inspect Element to see it.
Partial overriding
If you need to override a style for only a specific instance of the component and not all instances of the same component, then add a custom
styleClassand hook on that instead. It is another case where specificity is used/applied. For example:If a component does not support a
styleClassand you are on jsf 2.2 or up, you can also use passtrough attributes and add apt:classand have it end-up on the output.Never use !important
In case you fail to properly load the CSS file in order or to figure the right CSS selector, you’ll probably grab the
!importantworkaround. This is Plain Wrong. It’s an ugly workaround and not a real solution. It only confuses your style rules and yourself more in long term. The!importantshould only be used in order to override the values hardcoded in HTML element’sstyleattribute from a CSS stylesheet file on (which is in turn also a bad practice, but in some rare cases unfortunately unavoidable).See also: