Sign In With Google button responsive design

Viewed 798

Is there any way to make the new "Sign In With Google" button responsive? Specifically, vary the width based on the width of the containing element? I'd really just like to set the width to 100%.

I'm aware I can set the data-width attribute but this sets it to an explicit width and doesn't update if you change it after the initial script load - you have to reload the whole script to resize the width.

2 Answers

This isn't a perfect solution but it works for us. We're using Twitter Bootstrap.

The new JavaScript library has a renderButton method. You can therefore render the button multiple times on one page passing different widths to each button using something like this (400 is the max width allowed by the library)

private renderAllGoogleSignInButtons(): void {
  this.renderGoogleSignInButton(document.getElementById('google-signin-xs'), 400);
  this.renderGoogleSignInButton(document.getElementById('google-signin-sm'), 280);
  this.renderGoogleSignInButton(document.getElementById('google-signin-md'), 372);
  this.renderGoogleSignInButton(document.getElementById('google-signin-lg'), 400);
  this.renderGoogleSignInButton(document.getElementById('google-signin-xl'), 400);
}

private renderGoogleSignInButton(element: HTMLElement, width: number){
  const options {
    type: 'standard',
    ....
    width: width
  };

  google.accounts.id.renderButton(element, options);
}

We then use the display classes from bootstrap to hide/show each button depending on the size.

    <div class="mx-auto" style="max-width: 400px">
      <div class="d-none-sm d-none-md d-none-lg d-none-xl">
        <div id="google-signin-xs"></div>
      </div>
      <div class="d-none d-none-md d-none-lg d-none-xl">
        <div id="google-signin-sm"></div>
      </div>
      <div class="d-none d-none-sm d-none-lg d-none-xl">
        <div id="google-signin-md"></div>
      </div>
      <div class="d-none d-none-sm d-none-md d-none-xl">
        <div id="google-signin-lg"></div>
      </div>
      <div class="d-none d-none-sm d-none-md d-none-lg">
        <div id="google-signin-xl"></div>
      </div>
    </div>

We use a wrapper container with mx-auto and a max-width to center the buttons but you don't have to do this.

Our actual implementation is slightly different than the above as we're using Angular and the button is a component but you can get the idea from the above.

The only drawback with this method is that the "personalized button" doesn't seem to display for all rendered buttons but it doesn't seem to affect their functionality.

This answer is based on the new Google Identity Services.

You could try listening for a resize in the window using the resize event, then re-render the Google Sign In button on change. The assumption here is that the container will respond to match the window size:

addEventListener('resize', (event) => {});

onresize = (event) => {
  const element = document.getElementById('someContainer');
  if (element) {
    renderGoogleButton(document.getElementById('googleButton'), element.offsetWidth); // adjust to whatever proportion of the "container" you like
  }
}

renderGoogleButton(element, width) {
  const options = {
    type: 'outline',
    width: width
  }
  google.accounts.id.renderButton(element, options);
}

I've also had better results when the button is centered, not left aligned. The following in Bootstrap:

<div class="d-flex justify-content-center">
  <div id="googleButton"></div>
</div>

NB: The max width for the Google button as of the time of writing is 400px, so bear that value in mind as the limit.

Related