Set Javafx. Root Styles in Css

I want to apply several styles (font, font-size) to my components initially via css-file.
(I am using the SceneBuilder 2.0 from oracle.)
So I added my css-file to the top component.

My css-File looks like this:
Application.css

.root {
    -fx-font: 14px Arial;
}

.button {
    -fx-font: 28px Arial;
}

My Application.fxml looks like this:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>


<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="240.0" prefWidth="320.0" stylesheets="@Application.css" xmlns="" xmlns:fx="">
   <children>
      <Text strokeType="OUTSIDE" strokeWidth="0.0" text="MyText" />
      <Label text="MyLabel" />
      <Button mnemonicParsing="false" text="MyButton" />
   </children>
</VBox>

now I add a Text-Element or Label-Element to my pane, the font doesn't get affected by the root node. But if I add a Button-Element to my pane, the style for .button gets applied.
I expected the .root node would affect the style of any other elements. like in this answered question but it did not work: Set Font globally in JavaFX
can anyone maybe explain what am I doing wrong?

the only way to archive somthing similar is: setting the style directly as:

-fx-font: 14px Arial;

under Style which i wish i could store in a css file.

4

1 Answer

I deleted my previous answer. It was not accurate and it was a little confusing.

A container control doesn't have a property -fx-font. So when you define:

.root {
    -fx-font: 14px Arial;
}

it has no effect at all.

If you want to set the font for all children controls you should do:

.root * {
    -fx-font: 14px Arial;
}

or if you want to change only specific controls of a container:

.table-view .label {
    -fx-font: 14px Arial;
}

or all labels of the application:

.label {
    -fx-font: 14px Arial;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest