Specificity issues when using CSS API to update styles

,

Hey - I’m using the CSS API to update form styles, but I cannot get any of the styles to apply without use of an ‘!important’ tag. There’s obviously a specificity issue here but I can’t work out where.

All my style overrides are in a gravity-overrides.css files, which is then imported into a tailwind.css file and outputted to style.css. The style file is enqueued the usual way (wp_enqueue_style via functions).

Am I fundamentally missing something here? I’ve gone through the documentation but can’t find a solution.

Code in my overrides file is:

.gform-theme--framework.gform-theme--orbital {

    --gf-color-primary: var(--primary-500) !important;

--gf-ctrl-border-color: var(--neutral-300);

--gf-radius: var(--radius-md) !important;

}

As soon as I remove !important, the styles disappear.

Hi Simpson,

You are not missing anything in your setup, this is expected.

When a form uses the Orbital theme, Gravity Forms prints a small inline style block inside the form wrapper with the values from the form or block style settings. It prints them even if you did not change any setting. That block uses this selector:

#gform_wrapper_1[data-form-index="0"].gform-theme

It has an ID in it, so it is stronger than .gform-theme--framework.gform-theme--orbital. It also loads after your style.css. That is why your values only work with !important.

You have two options.

Option 1: Make your selector stronger

Add :not(#_) to your selector. It matches every form, but it counts like an ID, so your selector wins:

.gform-theme.gform-theme--framework.gform-theme--orbital:not(#_) {
    --gf-color-primary: var(--primary-500);
    --gf-ctrl-border-color: var(--neutral-300);
    --gf-radius: var(--radius-md);
}

Option 2: Set the values with the gform_default_styles filter

This changes the values in the inline block itself, so you don’t need to fight the specificity. For example, in your theme functions.php:

add_filter( 'gform_default_styles', function( $styles ) {
    $styles['inputPrimaryColor'] = '#1e40af';
    $styles['inputBorderColor']  = '#d4d4d4';
    $styles['inputBorderRadius'] = '6';
    return $styles;
} );

This filter needs real values, so your Tailwind CSS variables will not work here. If you want to keep using your Tailwind variables, use option 1.

Docs:

One small thing: in your post, gform-theme–framework and var(–neutral-300) show a single long dash. This could just be the forum formatting. Please check that your file uses two normal hyphens (--).

Let me know if it still does not work.

Thank you that’s really helpful! I just wanted to understand if I was missing something. Adding in the :not(#_) works perfectly, thanks for all your help :slight_smile:

You’re welcome, Simpson.

Glad to hear that :not(#_) resolved the issue. Thanks for confirming!