Building a custom Google Sign-In button

  • Support for the Google Sign-In library is deprecated, and users should refer to the deprecation guide.

  • You can customize the automatically rendered Google Sign-In button by adding a container element, calling signin2.render() with options, and including the platform.js script with an onload function.

  • Alternatively, you can specify custom settings for a Google Sign-In button using data- attributes on a div element with the class g-signin2.

  • For a fully custom button graphic that fits your site's design, you must adhere to Google's branding guidelines for colors and icons and ensure the button is prominent.

To create a Google Sign-In button with custom settings, add an element to contain the sign-in button to your sign-in page, write a function that calls signin2.render() with your style and scope settings, and include the https://apis.google.com/js/platform.js script with the query string onload=YOUR_RENDER_FUNCTION.

The following is an example of a Google Sign-In button that specifies custom style parameters: [フレーム]

The following HTML, JavaScript, and CSS code produces the button above:

<html>
<head>
<metaname="google-signin-client_id"content="YOUR_CLIENT_ID.apps.googleusercontent.com">
</head>
<body>
<divid="my-signin2"></div>
<script>
functiononSuccess(googleUser){
console.log('Logged in as: '+googleUser.getBasicProfile().getName());
}
functiononFailure(error){
console.log(error);
}
functionrenderButton(){
gapi.signin2.render('my-signin2',{
'scope':'profile email',
'width':240,
'height':50,
'longtitle':true,
'theme':'dark',
'onsuccess':onSuccess,
'onfailure':onFailure
});
}
</script>
<scriptsrc="https://apis.google.com/js/platform.js?onload=renderButton"asyncdefer></script>
</body>
</html>

You can also specify settings for a custom Google Sign-In button by defining data- attributes to a div element with the class g-signin2. For example:

<div class="g-signin2" data-width="300" data-height="200" data-longtitle="true">

Building a button with a custom graphic

You can build a Google Sign-In button to fit your site's design. You must follow the branding guidelines and use the appropriate colors and icons in your button. The branding guidelines also provide icon assets that you can use to design your button. You must also ensure that your button is as prominent as other third-party login options.

The following is an example of a Google Sign-In button built with a custom graphic: [フレーム]

The following HTML, JavaScript, and CSS code produces the button above:

<html>
<head>
<linkhref="https://fonts.googleapis.com/css?family=Roboto"rel="stylesheet"type="text/css">
<scriptsrc="https://apis.google.com/js/api:client.js"></script>
<script>
vargoogleUser={};
varstartApp=function(){
gapi.load('auth2',function(){
//RetrievethesingletonfortheGoogleAuthlibraryandsetuptheclient.
auth2=gapi.auth2.init({
client_id:'YOUR_CLIENT_ID.apps.googleusercontent.com',
cookiepolicy:'single_host_origin',
//Requestscopesinadditionto'profile'and'email'
//scope:'additional_scope'
});
attachSignin(document.getElementById('customBtn'));
});
};
functionattachSignin(element){
console.log(element.id);
auth2.attachClickHandler(element,{},
function(googleUser){
document.getElementById('name').innerText="Signed in: "+
googleUser.getBasicProfile().getName();
},function(error){
alert(JSON.stringify(error,undefined,2));
});
}
</script>
<styletype="text/css">
#customBtn {
display:inline-block;
background:white;
color:#444;
width:190px;
border-radius:5px;
border:thinsolid#888;
box-shadow:1px1px1pxgrey;
white-space:nowrap;
}
#customBtn:hover {
cursor:pointer;
}
span.label{
font-family:serif;
font-weight:normal;
}
span.icon{
background:url('/identity/sign-in/g-normal.png')transparent5px50%no-repeat;
display:inline-block;
vertical-align:middle;
width:42px;
height:42px;
}
span.buttonText{
display:inline-block;
vertical-align:middle;
padding-left:42px;
padding-right:42px;
font-size:14px;
font-weight:bold;
/*UsetheRobotofontthatisloadedinthe<head>*/
font-family:'Roboto',sans-serif;
}
</style>
</head>
<body>
<!--Inthecallback,youwouldhidethegSignInWrapperelementona
successfulsignin-->
<divid="gSignInWrapper">
<spanclass="label">Signinwith:</span>
<divid="customBtn"class="customGPlusSignIn">
<spanclass="icon"></span>
<spanclass="buttonText">Google</span>
</div>
</div>
<divid="name"></div>
<script>startApp();</script>
</body>
</html>

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年06月06日 UTC.