HubSpot Module Code Snippet: Convert Alignment Fields to CSS Flex Properties [HubL Macro]


HubSpot Module Code Snippet: Convert Alignment Fields to CSS Flex Properties [HubL Macro]

Below are macros you can use in your HubSpot CMS development projects to convert a text align field into flexbox justify content values. Sometimes text alignment and it’s icons are easier for content editors to understand. So when building functionality you may need to convert it into flexbox values like I have done here.

HubL Code

Macro for Text Alignment Field

{% macro text_align_to_flex(text_alignment) %}
    {% set justify_content_value = "" %}
    {% if text_alignment == "LEFT" %}
        {% set justify_content_value = "flex-start" %}
    {% elif text_alignment == "CENTER" %}
        {% set justify_content_value = "center" %}
    {% elif text_alignment == "RIGHT" %}
        {% set justify_content_value = "flex-end" %}
    {% endif %}
    
    justify-content:{{ justify_content_value }};

    {# Add this where you want to value to be placed - {{ text_align_to_flex(module.style.heading.textalignment.text_align) }} #}
{% endmacro %}

Macro for Alignment Field

{% macro get_flex_alignment(alignment) %}
    display:flex;
    {% if alignment.horizontal_align == "LEFT" %}
        justify-content: flex-start;
    {% elif alignment.horizontal_align == "CENTER" %}
        justify-content: center;
    {% elif alignment.horizontal_align == "RIGHT" %}
        justify-content: flex-end;
    {% endif %}

    {% if alignment.vertical_align == "TOP" %}
        align-items: flex-start;
    {% elif alignment.vertical_align == "MIDDLE" %}
        align-items: center;
    {% elif alignment.vertical_align == "BOTTOM" %}
        align-items: flex-end;
    {% endif %}
{% endmacro %}
{# output {{ get_flex_alignment(module.style.alignment_field) }} #}

About the Author

Jake Lett is a results-driven Detroit based B2B marketing consultant with 15+ years of hands-on experience managing SEO and PPC campaigns across manufacturing, SaaS, and professional services industries. He’s a Certified Google Ads Specialist and HubSpot CMS Developer who has personally managed budgets ranging from $500 to over $10,000/month.

Jake specializes in helping small businesses and solo marketers get more from lean ad budgets—using practical strategies that drive qualified leads, not just traffic. He shares real-world lessons on his blog, YouTube channel, and in his published books on digital marketing.



Related posts

Tags: ,

Want to Get Email Updates of New Articles?

Join My Email Newsletter