How to style gtk4 rust widgets?

Hi Team,

How do I style the Gtk widgets such as button? I’m aware that gtk supports CSS but I want to do it using rust.

Say for example I create a button as below. How do I add additional styling such as background-color, padding etc,.

let button = Button::builder()
               .label("Press me!")


You add CSS classes using the ButtonBuilder.css_classes() method.

The CSS classes are loaded by a CSSProvider.

Is it not possible to add other css styling like we add margin_top, margin_bottom using rust methods?

Those are not CSS properties: the margin set using the Widget API is applied regardless of the style.

I assume you want to do something similar to the DOM API where you can set inline styles on an HTML element, this is not currently possible although someone could probably come up with a way to emulate it using Widget::set_name and CssProvider.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.