如何用自定义样式覆盖默认的PrimeFaces CSS? [英] How do I override default PrimeFaces CSS with custom styles?

查看:2113
本文介绍了如何用自定义样式覆盖默认的PrimeFaces CSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了自己的主题作为单独的Maven项目,并且正确加载。



现在我想更改组件的大小。例如,

  • Mozilla开发人员网络> CSS>特定性(很棒的文章,一定要阅读!)

  • 了解CSS中的样式优先级:特定性,继承和级联


  • I've created my own theme as a separate Maven project, and it is loaded correctly.

    Now I want to change the size of an component. For example, a <p:orderList>. It has a class called ui-orderlist-list which is defined in primefaces.css with a fixed 200x200 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.

    I haven't checked for the other components.

    Can anyone please tell me how can I do this?

    解决方案

    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>:

    <h:head>
        ...
    </h:head>
    <h:body>
        <h:outputStylesheet name="style.css" />
        ...
    </h:body>
    

    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-specific HeadRenderer, but this is unnecessarily clumsy and would break when you have your own HeadRenderer.

    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

    .ui-foo .ui-bar {
        color: pink;
    }
    

    and you declare it as

    .ui-bar {
        color: purple;
    }
    

    and the particular element with class="ui-bar" happen to have a parent element with class="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 styleClass and hook on that instead. For example:

    <p:dataTable styleClass="borderless">
    

    .ui-datatable.borderless tbody,
    .ui-datatable.borderless th
    .ui-datatable.borderless td {
        border-style: none;
    }
    

    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 !important workaround. 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 !important should only be used in order to override the values hardcoded in HTML element's style attribute from a CSS stylesheet file on (which is in turn also a bad practice, but in some rare cases unfortunately unavoidable).

    See also:

    这篇关于如何用自定义样式覆盖默认的PrimeFaces CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆