简体   繁体   English

GWT + CSS样式重载

[英]GWT + CSS Style Overloading

I'm looking at making some custom GWT widgets styled in a uniform fashion without requiring the designer to go into each widget's UI file every time they want something to appear differently. 我正在考虑制作一些定制的GWT小部件,这些小部件以统一的方式设计,而不需要设计师每次想要显示不同的东西时都会进入每个小部件的UI文件。 I would provide a bunch of base styles for elements in the widget and the designer comes along later and sets, in UIBinder, HTML, CSS, anything really, the style using the same selector. 我会在窗口小部件中为元素提供一堆基本样式,设计器稍后出现并在UIBinder中设置HTML,CSS,任何事实,使用相同选择器的样式。

For example: I have a composite widget which I have setup to use a CSSResource. 例如:我有一个复合小部件,我已设置使用CSSResource。 This CSS resource has a style named .myHeaderStyle which is applied to an element on the composite. 此CSS资源具有名为.myHeaderStyle的样式,该样式应用于复合体上的元素。

This composite is used in another GWT Widget and needs to appear slightly differently when used in the enclosing widget. 此复合在另一个GWT窗口小部件中使用,并且在封闭窗口小部件中使用时需要略有不同。

My hope here is that I can specify another style in the UIBinder definition of that UI also named .myHeaderStyle and have this style override the style specified in the composite widget's CSSResource. 我希望这里是我可以在该UI的UIBinder定义中指定另一个样式,也命名为.myHeaderStyle,并使此样式覆盖复合窗口小部件CSSResource中指定的样式。

However, in my attempts to make this happen even with !important included on the style properties that are to override the initial style, I'm only getting the original .myHeaderStyle set on the composite widget. 但是,在我试图使这种情况发生时,甚至在重写包含在要覆盖初始样式的样式属性上,我只是在复合小部件上设置了原始的.myHeaderStyle。

I'm trying to specifically avoid adding/changing the style in the composite every time we compile, I want it to inherit from the enclosing page effectively overriding the composite widget's original styling. 我试图在每次编译时特别避免在复合中添加/更改样式,我希望它从封闭页面继承,有效地覆盖复合窗口小部件的原始样式。

Is what I'm trying to do possible in some form with GWT+CSS? 我想用GWT + CSS以某种形式做什么?

After building complex GWT apps for 6 years, I am a big proponent of using a single CSS file for the entire app, and never using CSS resources or UIBinder definitions. 在构建复杂的GWT应用程序6年后,我非常支持在整个应用程序中使用单个CSS文件,并且从不使用CSS资源或UIBinder定义。 Then you can set ".myWidget" style in your widget, and your designer can do: 然后,您可以在窗口小部件中设置“.myWidget”样式,您的设计师可以执行以下操作:

.myHeaderStyle {
   font-size: 1.4rem;
}
.myWidget .myHeaderStyle {
   font-size: 1.6rem;
}

In my opinion, this is the easiest way to maintain consistency throughout the app - all styles are in one place, using inheritance, rem, and other best practices. 在我看来,这是在整个应用程序中保持一致性的最简单方法 - 所有样式都在一个地方,使用继承,rem和其他最佳实践。 It's much easier for designers that CSS resources scattered throughout the app. 设计人员更容易将CSS资源分散到整个应用程序中。

By the way, this is also the easiest approach to implement themes (or skins), or change CSS based on the screen size without touching the code. 顺便说一下,这也是实现主题(或皮肤)或根据屏幕大小更改CSS而不触及代码的最简单方法。

EDIT: 编辑:

This is an example from one of my apps: 这是我的一个应用程序的示例:

<g:HTMLPanel>
    <g:Label ui:field="logo" styleName="logo">My App</g:Label>
    <div class="menu" >
        <div class="tab" >
            <g:Label ui:field="tab1" ><ui:text from="{constants.tab1}" /></g:Label>
            <g:Label ui:field="tab2" ><ui:text from="{constants.tab2}" /></g:Label>
            <g:Label ui:field="tab3" ><ui:text from="{constants.tab3}" /></g:Label>
        </div>
    </div>
</g:HTMLPanel>

Note that I use 'class' for div element, but styleName for a widget. 请注意,我为div元素使用'class',但为widget使用styleName。 I don't set style on my tab labels, because I use CSS to style all of them at once: 我没有在标签标签上设置样式,因为我使用CSS一次设置所有样式:

.tab>div {
    float: right;
    margin: 0 0 0 6px;
    padding: 2px 6px;
    width: 120px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM