1

I am trying to assign two different styles to the same type of node of a JavaFX application.

For example, suppose I have two Text nodes

<Text text="Welcome" GridPane.columnSpan="2" GridPane.halignment="CENTER" 
        GridPane.rowIndex="0" />
<Text text="Goodbye" GridPane.columnSpan="2" GridPane.halignment="CENTER" 
        GridPane.rowIndex="0" />

and an application.css

Text {
    -fx-font-size: 15pt;
    -fx-font-family: Tahoma;
    -fx-font-weight: bold;
}

This way, I will style both texts with the same class. How can I create and assign two different styles to the two instances of Text?

1 Answer 1

2

You can use the id attribute, to differentiate the two Text nodes and apply different CSS on them

<Text text="Welcome" GridPane.columnSpan="2" GridPane.halignment="CENTER" 
        GridPane.rowIndex="0" id="text1"/>
<Text text="Goodbye" GridPane.columnSpan="2" GridPane.halignment="CENTER" 
        GridPane.rowIndex="0" id="text2"/>

and in the css file, you can have

#text1{
    -fx-font-size: 10pt;
    -fx-font-family: Tahoma;
    -fx-font-weight: bold;
}

#text2{
        -fx-font-size: 15pt;
        -fx-font-family: Tahoma;
        -fx-font-weight: regular;
}

P.S. You can also separate the same using different css class for the two texts. Please follow this link for more information

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.