Changing the background color of a child in GTK3 using CSS in C language

Looking for help with setting the background of all 4 child elements in my App. I'm a bit lost on how to do it.

Here is an Example of what the App looks like:

I want to change the backgrounds as follows:

  • One - Red
  • Two - Yellow
  • Three - Green
  • Four - Blue

The code is below:

#include <gtk/gtk.h>

int main(int argc, char *argv[]){
    //---------- CSS -------------
    GtkCssProvider *provider;
    GdkDisplay *display;
    GdkScreen *screen;
    //---------------------------

    GtkWidget *window;
    GtkWidget *child, *child2, *child3, *child4;
    GtkWidget *grid;

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Equalizer");
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 250);
    gtk_container_set_border_width(GTK_CONTAINER(window), 5);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    // ---------------------------------------------------- CSS -----------------------------------------------------------
    provider = gtk_css_provider_new ();
    display = gdk_display_get_default ();
    screen = gdk_display_get_default_screen (display);
    gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER (provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

    const gchar *myCssFile = "mystyle.css";
    GError *error = 0;

    gtk_css_provider_load_from_file(provider, g_file_new_for_path(myCssFile), &error);
    g_object_unref (provider);
    // --------------------------------------------------------------------------------------------------------------------

    grid = gtk_grid_new ();
    gtk_container_add (GTK_CONTAINER (window), grid);

    child = gtk_label_new ("One");
    gtk_grid_attach (GTK_GRID (grid), child, 0, 1, 1, 1);

    child2 = gtk_label_new ("Two");
    g_object_set (child, "margin", 55, NULL);
    gtk_grid_attach_next_to (GTK_GRID (grid), child2, child, GTK_POS_RIGHT, 50, 50);


    child3 = gtk_label_new ("Three");
    g_object_set (child3, "margin", 55, NULL);
    gtk_grid_attach_next_to (GTK_GRID (grid), child3, child2, GTK_POS_RIGHT, 50, 50);

    child4 = gtk_label_new ("Four");
    g_object_set (child4, "margin", 0, NULL);
    gtk_grid_attach_next_to (GTK_GRID (grid), child4, child3, GTK_POS_RIGHT, 50, 50);


    gtk_widget_show_all(window);
    gtk_main();
}

CSS file content:

* {
    background-color: yellow;
}

GtkWindow {
    background-color: green;
    border-width: 3px;
    border-color: blue;
}

How can this be achieved in GTK3 using CSS?

Answer №1

If you want to apply styles by ID to your widgets, make sure to assign each widget a unique name like #child3.

In this case, the name you should set is child3.

void
gtk_widget_set_name (GtkWidget *widget,
                     const gchar *name);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Bootstrap Grid Leaves Blank Spaces Unfilled

View the screenshot here Take a look at the image above. I'm struggling to fill the empty space with div elements. I've experimented with display:inline-block and other display properties, but they haven't yielded the desired results. < ...

What are the Functions of Ctrl-K on Stack Overflow?

I'm intrigued by how to incorporate the Ctrl+K (code sample) feature for code. For example: public static void main(String args[]){ System.out.println.out("welcome"); } Is there a way to nicely format this? Do we need any specific package to ...

What is the best way to add animation to a button within a form using CSS?

Creating a Form in index.html: <form id="mc-embedded-subscribe-form" class="validate" name="mc-embedded-subscribe-form" method="post" action="mailing.php"><pre> <fieldset> <div class="mc-field-group"> <div class="input-cont"> ...

Discovering the top method to locate an Error Modal Message using Selenium

I am looking to automate a specific process that involves an Error modal popping up. I am trying to use Selenium to capture this modal which appears in multiple instances. So far, I have attempted to locate it by XPath without success. Using CSS selector o ...

Optimize my webpage for a specific location

While developing a game website, I am interested in learning how to enable only specific parts of my website to function while disabling the rest. How can this be achieved? ...

The CSS hamburger icon displayed in the browser features three bars of varying heights

Looking to create a hamburger icon menu using only CSS? Check out my implementation below which includes three span tags in the HTML document. .sidebar-toggle { display: inline-block; } .sidebar-toggle span { display: block; width: 1.5rem; heig ...

Customizing Each Menu Item in WordPress

I am currently working on customizing my WordPress Menu. My goal is to assign a unique color to each menu item, while ensuring that the background color of child elements within pages and posts matches the parent text color. Specifically, I envision the f ...

The issue of multiple columns not displaying correctly when set to a height of 100

I am struggling with a seemingly simple task. I want to create two columns on my page, each with a width of 50% and a height of 100% so they completely fill the screen side by side. However, no matter what I try, they do not align properly and end up float ...

Ways to define a variable based on a CSS class attribute?

I need help figuring out how to create a variable in my script that is defined by the width attribute of a CSS class. The snippet I have currently doesn't seem to work as expected. var dist = $('.nav').css(width()); The goal of my script i ...

Manipulating the visibility of components by toggling on click events according to the existing state in ReactJS

Disclosure: I am still getting familiar with ReactJS I'm currently working on incorporating a dialog window for selecting a country/language on the initial page of my application. Here's the concept: There's a small flag button in the to ...

Tips for adjusting the maximum width and content-based width for columns within an HTML/CSS table

I'm working on a simple HTML/CSS code featuring a container with two adjustable slots, controlled by a handler in the middle to modify the space each slot occupies. The first slot contains a table with fixed columns, automatic columns, and limited col ...

Steps for displaying the dropdown menu

I am having trouble with displaying the dropdown menu when hovering over the class (dropbtn). I tried adding .dropdown:hover .dropdown-content-strategy{..} but it's not working as expected. If I do manage to show the dropdown menu, it stays visible ev ...

Interested in retrieving the dynamically changing value of LocalStorage

Hopefully I can articulate my issue clearly. I am implementing a feature where CSS themes change upon button clicks. When a specific theme button is clicked, the corresponding classname is saved to LocalStorage. However, since the key and value in LocalSt ...

Tips on customizing the appearance of the dropdown calendar for the ngx-daterangepicker-material package

Is there a way to customize the top, left, and width styling of the calendar? I'm struggling to find the right approach. I've been working with this date range picker. Despite trying to add classes and styles, I can't seem to update the app ...

What is the secret to keeping a hover effect active?

As I hover over the buttons in my stack, a text box appears on the right side. The intention is for this text box to act as a scroll box, but it disappears when I move my mouse to scroll because it needs to be continuously hovered upon to stay active. To ...

Use CSS bracket attribute to conceal link with no HTML access

I am unable to modify the HTML code, but I need to find a way to hide the link (#wall). <td class="status_value"><span class="online"> - <a href="#wall"> Leave a Comment </a> <a href="#today"> ...

I am having trouble getting two similar Javascript codes to function simultaneously

Using a common JavaScript code, I am able to display a div when a certain value is selected. http://jsfiddle.net/FvMYz/ $(function() { $('#craft').change(function(){ $('.colors').hide(); $('#' + $(this ...

Error parsing in Visual Studio 2012

I have included the following code in my master page: <link rel='stylesheet' id='layer-styles-css' href='css/layerslider.css?ver=3.4.1' type='text/css' media='all' /> When debugging in MS Vis ...

Modifying CSS stylesheet does not alter the background color

body { background-color: bisque; } <!DOCTYPE html> <html> <head> <title>Reading</title> </head> <body> <h1> <button><a href="index.html">Home</a></button> & ...

Ways to emphasize the currently active tabs on a navigation bar

How can I highlight the active tabs and shift it when clicking on another link? $(function() { $('.nav ul li').on('click', function() { $(this).parent().find('li.active').removeClass('active'); $(this).a ...