Hey, I’m developing a GWT app and now facing the CSS part. I read a lot about this topic at the official site but still have a few questions and hope someone can give me a hint.
- When I’m using CSSResource the css styles will be compiled into the code – right? So it’s not possible to change it without recompile the app. But what I wanna do is to have the styles be editable from “outside”.
- So what is this CSSResource for, since you can’t just change a color or an image without compiling the app again?
- Whats the best way to use CSS since there are a few ways (.css file, CSSResource, styles in ui.xml) to do this?
My thoughts are now, that I’m using only the normal CSS file to handle all my “changeable” stuff and add this file to the head, since I can’t see the advantage of this CSSResource thing. Hopefully someone can help me with that. Thanks.
It all depends on the way you work with CSSes – if you need to apply some small changes, first test them “live”, in Firebug/similar tool. During the designing phase (when you don’t have a finalized view of how you want to style your application yet), you might want to work with normal CSS files, but as soon as the general style clarifies, you should switch to ClientBundle/CssResource.
Why? Let me answer, by writing up some thought about the goals listed on CssResource’s documentation page:
Primary
This does not imply that the stylesheet would necessarily make sense if you just displayed it in a browser – meaning, we want to only use valid CSS syntax, not some syntactic sugar that’s gibberish to other parsers – might not be that important from your point of view (although, as a result, you don’t need to change the syntax of your existing styles)
Secondary
CssResourceinternally) plus probably one global CssResource for application-wide styles. This results in a much cleaner, easier to maintain CSS styles (at least, in my experience). This also means, that if your code doesn’t use that particular Widget (maybe because you’ve used ocde splitting and it hasn’t been loaded yet), you won’t download those styles.Tertiary
.warningstyle in two Widgets and the compiler will understand that these two styles are in different namespaces (different Widgets) and thus should be treated differently (that is, obfuscated to different names).About style injection
The class
StyleInjectorallows injecting styles at runtime. Additionally, theCssResourceallows (at least according to the docs, I haven’t tried it yet) for runtime substitution. For example, when you inject aCssResourcecontaining the following:The
userBackgroundwill get evaluated and injected (as opposed to constants, which are resolved at runtime).