28th Jan '24
/
7 comments

Conditional CSS in Bricks based on Post Meta

In the Bricks Facebook group a user asked:

Hi, how do you normally control margin, padding and font-size using dynamic data? I'm looking for the best workflow to accommodate these rather unusual scenarios.

This Pro tutorial covers two ways this can be done in Bricks.

  • Using style attribute and adding the CSS inline
  • Registering a blank custom stylesheet, enqueuing it, building the CSS string with property values from post meta, and using wp_add_inline_style() to push the CSS from the string into the enqueued stylesheet.

Sample Scenario

ACF Field group: Page Fields

When a specific Page is being edited:

Method 1: Using style attribute and adding the CSS inline

Add a style attribute for the element, say a Button like this:

font-size: {acf_button_font_size}rem; padding: {acf_button_vertical_padding}em {acf_button_horizontal_padding}em

Method 2: wp_add_inline_style() to a custom stylesheet

Select the Button element → STYLE → CSS → CSS classes: btn--lg

Add the following in child theme's functions.php (w/o the opening PHP tag) or a code snippets plugin:

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Get access to all 610 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Checking ‘PublishPress Future’ plugin’s Future Action enabled posts

Checking ‘PublishPress Future’ plugin’s Future Action enabled posts

PublishPress Future is a handy plugin for scheduling actions like unpublishing or deleting posts (of any post type) at a specified date and time in…
Categories:
Is WooCommerce Cart Empty Condition in Bricks

Is WooCommerce Cart Empty Condition in Bricks

Updated on 5 Nov 2024 Looking to conditionally render an element depending on whether the user's cart is empty or not when using WooCommerce? Add…
Pro
Custom Conditions in Bricks Builder

Custom Conditions in Bricks Builder

Updated on 22 Jun 2022 This Pro tutorial is a continuation of the previous Conditions in Bricks Builder tutorial. We are going to keep updating…
Categories:
[Function] Current Term Has Children

[Function] Current Term Has Children

Updated on 20 Jul 2024 A user asks: I'm working on an archive template for WooCommerce products. I'm trying to hide a section if there…
Pro
Conditional Related Posts in Bricks

Conditional Related Posts in Bricks

How to output a section only if there is at least 1 related post for the current post being viewed.
Categories:
Pro
Conditionally Outputting Query Loop Item Based on Post Meta in Bricks

Conditionally Outputting Query Loop Item Based on Post Meta in Bricks

Rendering query-loop enabled posts depending on the value of each post's custom field value is tricky because by default, the custom field plugins' functions or…
Categories:
Pro
Conditional Output in Bricks based on if Content has Headings

Conditional Output in Bricks based on if Content has Headings

How to prevent the output of the table of contents if the content has no headings.
Categories:
Tags: