KutaBeach
KutaBeach

Reputation: 1495

Is it possible to create a parametrized css style?

I have CSS style which draws a button. I need to reuse this style to draw a button of different color.

.button {
   color: white;
   background-color: black;
   border: 1px solid orange;
   (...)
}

I know I can override CSS attributes. But the problem here is that I have to override almost all code of the button and it doesn't make sense. Its easier to create another CSS class which differs from this only by color values. Is it possible to create some style which can take the colors as parameters, preserving all other css attributes?

Upvotes: 2

Views: 564

Answers (3)

fred02138
fred02138

Reputation: 3361

GWT supports parameterized CSS at compile time. This enables you to embed compile-time directives in your CSS file, e.g.

@def TEXT_COLOR   #A0A0A0

Then you can use the value in CSS rules:

.some-class {
   color: TEXT_COLOR;

}

You also have access to the values from Java (at compile time). You can read about the feature here: http://code.google.com/p/google-web-toolkit/wiki/CssResource

Upvotes: 1

F. Geraerts
F. Geraerts

Reputation: 1190

You can use the runtime substitution of GWT (Here) with the annotation @eval

Runtime substitution:

Provides runtime support for evaluating static methods when the stylesheet is injected. Triggered / dynamic updates could be added in the future if we allow programmatic manipulation of the style elements. (cfr the doc)

For example:

@eval buttonColor com.myApp.MyTheme.getButtonColor();

.button {
   color: buttonColor ;
   background-color: black;
   border: 1px solid orange;
   (...)
}

The method used during the evaluation must be static

 public class MyTheme {

...

    public static String getButtonColor(){
       return "white";
    }
}

Upvotes: 1

Mirage
Mirage

Reputation: 1487

You can add a new class with your button class.

.newclass{
color:#ffffff !important;
}

Just make sure you have !important added to the new class color property.

Upvotes: -1

Related Questions