how to center align buttons

Hi all,

I’ve been building a few landingpages with a copy of the default HS template. Works very good, except I can’t figure out for the world how to make the buttons center align within a column. They always stick to the left. I’ve been looking in the settings > themes part, I’ve been through the CSS code, but nothing worked. Anyone got a sugestion?

Thnx!

@SvanWijngaarden , could you add a link to a page with the issue?

Hi Dennis,

A good example of this is https://samenrisicobewust.schoutenzekerheid.nl. De page is in dutch, but when you scroll down you can see that the last button is aligned quite akwardly. Hope you be able to put me in the right direction!

Thnx,

Sjoerd

Thanks @SvanWijngaarden

The quick fix is adding a style to the head of the page which you can find in the advanced setting of the page editor.

<style>
#hs_cos_wrapper_widget_1616077634088{text-align: center}
</style>

The better solution would add justification option to the button module that is being used.

@Anton , do you have any additional ideas?

Hi @SvanWijngaarden,

seems like @dennisedson trick solved everything!

Nevertheless - If you plan to build multiple pages with a CTA section as the last element and would like to always center it you could do it a bit more generic like this:

.dnd-module:last-of-type a.button{
text-align:center;
}

Also: @dennisedson idea of putting a justification option to the button is great since it’s a “cleaner” solution but requires a bit more work. But I’ll try to guide you. Since you’re using the “session”-theme you’d need to:

  1. find the button.module of your theme(most likely located in the modules folder of your theme in the design-manager).
  2. modify it
    1. First I would add a select option (can be found in the right-sidebar) for simple handling in the future I would add a select option and give it following values/names
      Name of select option(very top in the right sidebar): Button position
      - name: Left / value: flex-start
      - name: Center / value: center
      - name: Right / value: flex-end
    2. copy/paste the code below
  3. save the button
  4. go to your page and select the option where you’d like to position of the button

code for replacement:

{{ require_css }}
<style>
.horz-{{ module.button_position }}{
display:flex;
flex-direction:column;
flex-wrap:no-wrap;
justify-content:flex-start;
align-items: {{ module.button_position }};
align-content:center;
}
</style>
{{ end_require_css}}
{% if module.button_link.url.type is equalto 'EMAIL_ADDRESS' %}
 {% set href = 'mailto:' ~ module.button_link.url.href %}
{% elif module.button_link.url.href == '/' %}
 {% set href = module.button_link.url.href %}
{% else %}
 {% unless (module.button_link.url.href is string_containing '//') or !module.button_link.url.href %}
 {% set href = '//' ~ module.button_link.url.href %}
 {% else %}
 {% set href = module.button_link.url.href || '' %}
 {% endunless %}
{% endif %}

{% set rel = (module.button_link.open_in_new_tab ? 'noopener ' : null) ~ (module.button_link.no_follow ? 'nofollow' : null) %}
<div class="horz-{{module.button_position}}">
<{% if module.button_options.button_link_toggle == true %}a href="{{ href }}" {% else %}button {% endif %} class="button{% if module.style.button_type == 'secondary' %} button--secondary{% elif module.style.button_type == 'simple' %} button--simple{% endif %}{% if module.style.button_size == 'small' %} button--small{% elif module.style.button_size == 'large' %} button--large{% endif %}{% if module.button_icon.icon_position == 'right' %} button--icon-right{% endif %}"
 id="button_{{ name }}"
 {% if module.button_link.open_in_new_tab %}target="_blank"{% endif %}
 rel="{{ rel }}">

 {% if module.button_options.button_icon_toggle == true and module.button_icon.icon_field.unicode %}
 {% icon
 extra_classes='button__icon'
 name='{{ module.button_icon.icon_field.name }}'
 purpose='decorative'
 style='{{ module.button_icon.icon_field.type }}'
 unicode='{{ module.button_icon.icon_field.unicode }}'
 %}
 {% endif %}
 {{ module.button_text }}

</{% if module.button_options.button_link_toggle == true %}a{% else %}button{% endif %}>
</div>

It’s a very technical solution but eliminates the need of editing your template/CSS permanently :grinning_face:

@dennisedson - how about implementing something like that as a default feature of the default button module?

best,

Anton

Hi both! Thanks for your help! I think this is the solution. @Anton can you please tell me what code I need to replace, or where to put the code above? I managed to add the justification option to the buttons, but they’re obviously not yet working.

Thanks!

Best,

Sjoerd

Hi @SvanWijngaarden,

  1. open the design-manager(marketing/files/design-manager)
  2. open your theme folder. It seems that you’re using session (you’ll find it in the @hubspot folder)
  3. navigate to the module folder and open the button module
  4. cut the hole part from the html+hubl section and paste the code from above into it.
  5. Add a select option how described above

best,

Anton

Thats it! Thanks!

Best,

Sjoerd

Thanks for this solution, I’m new to Hubspot and it worked great. I had trouble finding the “select option” in step 2 (perhaps it was renamed). Adding clarity to step 2.1 of the answer:

  1. In the “Fields” section, click “Add field”
  2. Select “Choice”, located under the Boolean label
  3. Proceed with the rest of step 2

Another note: these instructions add a selector dropdown to the Button module UI. They do not add the “Alignment” button group that’s in the Styles tab of other modules—see screenshot for reference. I named my field “Button alignment” to match the labeling pattern of the other options.

Hi Anton,

thank you.

I just used your code and done every step and it works for the alignment.

Now i have a new problem cause the internal links it doesn’t work.

Usually in my landing page i use to give id to some elements and use it for the buttons.

I don’t know how to resolve it.

Im gonna attach here a screenshot of the code:


Thank you,

Riccardo

Hi Anton,
I have tried this for the center button. Though it does create a centered button, the text is no longer centered and aligned to the left. I added the justification section as described above but that does not seem to do anything. Can you please provide an update if this still works?

Hello I’ve tried this but when I add the code the button gets its own gravity and pulls in other elements xD

In the Editor:

**On live site:


**

Here is the code for my button before changes:

{% if module.link.url.type is equalto 'EMAIL_ADDRESS' %}
 {% set href = 'mailto:' ~ module.link.url.href %}
{% elif module.link.url.href == '/' %}
 {% set href = module.link.url.href %}
{% else %}
 {% unless (module.link.url.href is string_containing '//') or !module.link.url.href %}
 {% set href = '//' ~ module.link.url.href %}
 {% else %}
 {% set href = module.link.url.href || '' %}
 {% endunless %}
{% endif %}

{% set rel = [] %}
{% if module.link.no_follow %}
 {% do rel.append("nofollow") %}
{% endif %}
{% if module.link.open_in_new_tab %}
 {% do rel.append("noopener") %}
{% endif %}

<a class="button bg-{{ module.use_custom_colors }}" href="{{ href }}"

 {% if module.link.open_in_new_tab %}target="_blank"{% endif %}
{% if rel %}rel="{{ rel|join(' ') }}"{% endif %}
>
 {{ module.button_text }}
</a>

Here is the code for when I try to add your code:

{{ require_css }}
<style>
.horz-{{ module.button_position }}{
display:flex;
flex-direction:column;
flex-wrap:no-wrap;
justify-content:flex-start;
align-items: {{ module.button_position }};
align-content:center;
}
</style>
{{ end_require_css}}

{% if module.link.url.type is equalto 'EMAIL_ADDRESS' %}
{% set href = 'mailto:' ~ module.link.url.href %}
{% elif module.link.url.href == '/' %}
{% set href = module.link.url.href %}
{% else %}
{% unless (module.link.url.href is string_containing '//') or !module.link.url.href %}
{% set href = '//' ~ module.link.url.href %}
{% else %}
{% set href = module.link.url.href || '' %}
{% endunless %}
{% endif %}

{% set rel = [] %}
{% if module.link.no_follow %}
{% do rel.append("nofollow") %}
{% endif %}
{% if module.link.open_in_new_tab %}
{% do rel.append("noopener") %}
{% endif %}

<div class="horz-{{module.button_position}}">
<{% if module.button_options.button_link_toggle == true %}a href="{{ href }}" {% else %}button {% endif %} class="button
{% if module.style.button_type == 'secondary' %} button--secondary{% elif module.style.button_type == 'simple' %} button--simple{% endif %}
{% if module.style.button_size == 'small' %} button--small{% elif module.style.button_size == 'large' %} button--large{% endif %}
{% if module.button_icon.icon_position == 'right' %} button--icon-right{% endif %}"
id="button_{{ name }}"
{% if module.button_link.open_in_new_tab %}target="_blank"{% endif %}
rel="{{ rel }}">

<a class="button bg-{{ module.use_custom_colors }}" href="{{ href }}"

{% if module.link.open_in_new_tab %}target="_blank"{% endif %}
{% if rel %}rel="{{ rel|join(' ') }}"{% endif %}
>
{{ module.button_text }}
</a>