Object Hierarchy

Implemented Interfaces

Description

GtkCssProvider is an object implementing the GtkStyleProvider interface.
It is able to parse CSS-like
input in order to style widgets.

Default files

An application can cause GTK+ to parse a specific CSS style sheet by
calling gtk_css_provider_load_from_file() and adding the provider with
gtk_style_context_add_provider() or gtk_style_context_add_provider_for_screen().
In addition, certain files will be read when GTK+ is initialized. First,
the file XDG_CONFIG_HOME/gtk-3.0/gtk.css
is loaded if it exists. Then, GTK+ tries to load
HOME/.themes/theme-name/gtk-3.0/gtk.css,
falling back to
GTK_DATA_PREFIX/share/themes/theme-name/gtk-3.0/gtk.css,
where theme-name is the name of the current theme
(see the "gtk-theme-name" setting) and GTK_DATA_PREFIX
is the prefix configured when GTK+ was compiled, unless overridden by the
GTK_DATA_PREFIX environment variable.

Style sheets

The basic structure of the style sheets understood by this provider is
a series of statements, which are either rule sets or '@-rules', separated
by whitespace.

A rule set consists of a selector and a declaration block, which is
a series of declarations enclosed in curly braces ({ and }). The
declarations are separated by semicolons (;). Multiple selectors can
share the same declaration block, by putting all the separators in
front of the block, separated by commas.

Selectors

Selectors work very similar to the way they do in CSS, with widget class
names taking the role of element names, and widget names taking the role
of IDs. When used in a selector, widget names must be prefixed with a
'#' character. The '*' character represents the so-called universal
selector, which matches any widget.

To express more complicated situations, selectors can be combined in
various ways:

To require that a widget satisfies several conditions,
combine several selectors into one by concatenating them. E.g.
GtkButton#button1 matches a GtkButton widget
with the name button1.

To only match a widget when it occurs inside some other
widget, write the two selectors after each other, separated by whitespace.
E.g. GtkToolBar GtkButton matches GtkButton widgets
that occur inside a GtkToolBar.

In the previous example, the GtkButton is matched even
if it occurs deeply nested inside the toolbar. To restrict the match
to direct children of the parent widget, insert a '>' character between
the two selectors. E.g. GtkNotebook > GtkLabel matches
GtkLabel widgets that are direct children of a GtkNotebook.

In complicated widgets like e.g. a GtkNotebook, it may be desirable
to style different parts of the widget differently. To make this
possible, container widgets may define regions, whose names
may be used for matching in selectors.

Some containers allow to further differentiate between regions by
applying so-called pseudo-classes to the region. For example, the
tab region in GtkNotebook allows to single out the first or last
tab by using the :first-child or :last-child pseudo-class.
When used in selectors, pseudo-classes must be prefixed with a
':' character.

Another use of pseudo-classes is to match widgets depending on their
state. This is conceptually similar to the :hover, :active or :focus
pseudo-classes in CSS. The available pseudo-classes for widget states
are :active, :prelight (or :hover), :insensitive, :selected, :focused
and :inconsistent.

Widget state pseudoclasses may only apply to the last element
in a selector.

To determine the effective style for a widget, all the matching rule
sets are merged. As in CSS, rules apply by specificity, so the rules
whose selectors more closely match a widget path will take precedence
over the others.

@ Rules

GTK+'s CSS supports the @import rule, in order to load another
CSS style sheet in addition to the currently parsed one.

Example 25. Using the @import rule

1

@importurl("path/to/common.css");

GTK+ also supports an additional @define-color rule, in order
to define a color name which may be used instead of color numeric
representations. Also see the "gtk-color-scheme" setting
for a way to override the values of these named colors.

Example 26. Defining colors

1
2
3
4
5

@define-color bg_color #f9a039;*{ background-color: @bg_color;}

Symbolic colors

Besides being able to define color names, the CSS parser is also able
to read different color expressions, which can also be nested, providing
a rich language to define colors which are derived from a set of base
colors.

where start_x and end_x can be either a floating point number between
0 and 1 or one of the special values 'left', 'right' or 'center', start_y
and end_y can be either a floating point number between 0 and 1 or one
of the special values 'top', 'bottom' or 'center', position is a floating
point number between 0 and 1 and color is a color expression (see above).
The color-stop can be repeated multiple times to add more than one color
stop. 'from (color)' and 'to (color)' can be used as abbreviations for
color stops with position 0 and 1, respectively.

Border images

Images can be used in 'slices' for the purpose of creating scalable
borders.

The syntax for specifying border images of this kind is:

url(path) toprightbottomleft [repeat|stretch]? [repeat|stretch]?

The sizes of the 'cut off' portions are specified
with the top, right, bottom and left parameters.
The 'middle' sections can be repeated or stretched to create
the desired effect, by adding the 'repeat' or 'stretch' options after
the dimensions. If two options are specified, the first one affects
the horizontal behaviour and the second one the vertical behaviour.
If only one option is specified, it affects both.

Example 32. A border image

This border image was specified with

url("gradient1.png") 10 10 10 10

Example 33. A repeating border image

This border image was specified with

url("gradient1.png") 10 10 10 10 repeat

Example 34. A stretched border image

This border image was specified with

url("gradient1.png") 10 10 10 10 stretch

Styles can specify transitions that will be used to create a gradual
change in the appearance when a widget state changes. The following
syntax is used to specify transitions:

duration [s|ms] [linear|ease|ease-in|ease-out|ease-in-out] [loop]?

The duration is the amount of time that the animation will take for
a complete cycle from start to end. If the loop option is given, the
animation will be repated until the state changes again.
The option after the duration determines the transition function from a
small set of predefined functions.

Figure 3. Linear transition

Figure 4. Ease transition

Figure 5. Ease-in-out transition

Figure 6. Ease-in transition

Figure 7. Ease-out transition

Supported properties

Properties are the part that differ the most to common CSS,
not all properties are supported (some are planned to be
supported eventually, some others are meaningless or don't
map intuitively in a widget based environment).

There is also a difference in shorthand properties, for
example in common CSS it is fine to define a font through
the different font-family, font-style, font-size
properties, meanwhile in GTK+'s CSS only the canonical
font property is supported.

GtkThemingEngines can register their own, engine-specific style properties
with the function gtk_theming_engine_register_property(). These properties
can be set in CSS like other properties, using a name of the form

-namespace-name

, where namespace is typically
the name of the theming engine, and name is the
name of the property. Style properties that have been registered by widgets
using gtk_widget_class_install_style_property() can also be set in this
way, using the widget class name for namespace.