31

Hi I just added Google's No CAPTCHA reCAPTCHA to my website, and I am running into a small little issue. It does NOT fit on my mobile website, and that is a HUGE issue. I have tried everything such as:

HTML

<div id="captchadisplay">
  <div class="g-recaptcha" data-sitekey="???"></div>
</div>

CSS

#captchadisplay {
  width: 50% !important;
}

and

CSS

.g-recaptcha {
  width: 50%;
}

Yet I can not seem to shrink it down for my mobile website. Any ideas? :)

michael jones
  • 710
  • 1
  • 8
  • 17

14 Answers14

50

By using the CSS transform property you can achieve changing the width by changing the entire scale of the reCAPTCHA.

By adding in just two inline styles, you can make the reCAPTCHA fit nicely on your mobile device:

<div class="g-recaptcha"
     data-theme="light"
     data-sitekey="XXXXXXXXXXXXX"
     style="transform:scale(0.77);-webkit-transform:scale(0.77);transform-origin:0 0;-webkit-transform-origin:0 0;">
</div>

More details can be found on my site: https://www.geekgoddess.com/how-to-resize-the-google-nocaptcha-recaptcha/

Pang
  • 9,564
  • 146
  • 81
  • 122
Geek Goddess
  • 609
  • 4
  • 3
  • This doesn't work for all browsers, I would suggest using a [lesselements](http://lesselements.com/) mixin though to cover all browser variations of scale... `.scale(0.77)` works for all browsers with origin transform. – SliverNinja - MSFT Jan 28 '16 at 01:33
  • 10
    why 0.77 and not anything else? is .77 somehow calculated to be best size here? – Sourabh Jul 27 '16 at 12:59
  • Your site ranks higher than this on Google. – dewd Aug 19 '20 at 12:40
18

I successfully implemented Geek Goddess' solution. The main issue with it is that it may not work on all browsers. Now, however, there is a simple solution provided by Google.

With reCAPTCHA version 2.0, there are new rules for the display of the widget and Google added tag attributes that change the way it is rendered.

The tag data-size can take the values of "normal", which is the default, and "compact", which fits in mobile device screens. This is what the compact widget looks like.

new compact reCAPTCHA v2.0

It is not the best looking widget, but it fits with less work than the other solutions.

For more attributes, check Google's reCAPTCHA v2.0 documentation

Pang
  • 9,564
  • 146
  • 81
  • 122
11

I have been using some JQuery, since putting transform(0.77) in the style attribute wasn't a truly responsive solution.

I add this media query in my CSS, with the max-width being the threshold where the ReCaptcha box is considered too large once passed:

@media(max-width: 390px) {
    .g-recaptcha {
        margin: 1px;
    }
}

I then add this JQuery:

$(window).resize(function() {
    var recaptcha = $(".g-recaptcha");
    if(recaptcha.css('margin') == '1px') {
        var newScaleFactor = recaptcha.parent().innerWidth() / 304;
        recaptcha.css('transform', 'scale(' + newScaleFactor + ')');
        recaptcha.css('transform-origin', '0 0');
    }
    else {
        recaptcha.css('transform', 'scale(1)');
        recaptcha.css('transform-origin', '0 0');
    }
});

The 304 I use is the default width of the ReCaptcha box if unstyled.

Now the ReCaptcha will properly scale down no matter how small its parent container becomes, and it will behave as if it has a maximum width at its original width.

Note that the media query is simply a mechanism to detect a screen size change.

Ozymandias
  • 2,533
  • 29
  • 33
6

According to the documentation from Google shows a data-size attribute which can be set and this worked for me.

 <div class="g-recaptcha" data-sitekey="XXXXXXXX" data-size="compact"></div>

But, the answer from @GeekGoddess provides a little more flexibility in sizing.

L_7337
  • 2,650
  • 28
  • 42
4

For me the compact mode implementation of Google re-captcha 2.0 is just lame. It looks ugly.

Just expanding from "Geek Goddess" solution.

You can do the following:

<div class="g-recaptcha" data-sitekey="..." style="-moz-transform:scale(0.77); -ms-transform:scale(0.77); -o-transform:scale(0.77); -moz-transform-origin:0; -ms-transform-origin:0; -o-transform-origin:0; -webkit-transform:scale(0.77); transform:scale(0.77); -webkit-transform-origin:0 0; transform-origin:0; filter: progid:DXImageTransform.Microsoft.Matrix(M11=0.77,M12=0,M21=0,M22=0.77,SizingMethod='auto expand');"></div>

That will resize on almost all browsers IE, Chrome, FF, Opera (DXImageTransform is for IE <= 8).

Furthermore we can make it responsive by combining this transform scale with CSS max-width.

It's not the perfect way, but until we get the proper responsive fix from Google.

danronmoon
  • 3,814
  • 5
  • 34
  • 56
Felix
  • 336
  • 2
  • 6
3

If you don't like the CSS solution, you may try the JS.

The idea is to dynamically switch between compact and normal mode of the recaptcha plugin.

I will provide an example with jQuery onboard, but it shouldn't be much to port it to pure JS.

I assume you have following HTML code on the site.

<div>
    <div class="g-recaptcha" data-sitekey="[your-key-here]"></div>
</div>

Firstly you need to load gRecaptcha 2 explicitly and provide onload callback:

<script type='text/javascript' src='https://www.google.com/recaptcha/api.js?hl=en&#038;onload=recaptchaCallback&#038;render=explicit'>

Next, create your callback function which will also be your javascript media query.

function recaptchaCallback()
{
    var mq = window.matchMedia("(max-width: 400px)");
    mq.addListener(recaptchaRenderer);
    recaptchaRenderer(mq);
}

The last thing is to render the recaptcha widget.

function recaptchaRenderer(mq)
{
    var recaptcha = $('.g-recaptcha').eq(0);
    var data = recaptcha.data();
    var parent = recaptcha.parent();

    recaptcha.empty().remove();

    var recaptchaClone = recaptcha.clone();
    parent.append(recaptchaClone);
    recaptchaClone.data(data);

    var options = {
        'sitekey': data['sitekey'],
        'size': 'compact'
    };
    if(!mq.matches)
    {
        options['size'] = 'normal';
    }
    grecaptcha.render(recaptchaClone.get(0), options);
}

You may wonder why I empty the div and clone all the g-recaptcha content. It's because gRecaptcha 2 wouldn't let you render second time to the same element. There could be a better way, but it's all I found for now.

icetique
  • 386
  • 2
  • 5
2

Hope this works for you.

<div class="g-recaptcha" data-theme="light" data-sitekey="XXXXXXXXXXXXX" style="transform:scale(0.77);transform-origin:0 0"></div>

Just add style="transform:scale(0.77);transform-origin:0 0"

Sumit Nayak
  • 307
  • 3
  • 13
  • please support your answer explaining why this will work compared to what is already solutioned. – Sunil Kumar May 13 '15 at 07:10
  • It can fit on any screen size and even tough your solution works its an alternative to it. Please will use which they feel convenient as per their need – Sumit Nayak May 13 '15 at 09:59
2

For who might be interested, I changed a little AjaxLeung solution and came up with this:

 function resizeReCaptcha() {
    if ($(".g-recaptcha").length) {
        var recaptcha = $(".g-recaptcha");
        recaptcha.parent().addClass('col-xs-12 padding0');
        var innerWidth = recaptcha.parent().innerWidth();
        if (innerWidth < 304) {
            var newScaleFactor = innerWidth / 304;
            recaptcha.css('transform', 'scale(' + newScaleFactor + ')');
            recaptcha.css('-webkit-transform', 'scale(' + newScaleFactor + ')');
            recaptcha.css('transform-origin', '0 0');
            recaptcha.css('-webkit-transform-origin', '0 0');
        } else {
            recaptcha.css('transform', 'scale(1)');
            recaptcha.css('-webkit-transform', 'scale(1)');
            recaptcha.css('transform-origin', '0 0');
            recaptcha.css('-webkit-transform-origin', '0 0');
        }
    }
}

$(window).resize(function() {
    resizeReCaptcha();
});

$(document).ready(function () {
    resizeReCaptcha();
});
Community
  • 1
  • 1
Cosmin
  • 125
  • 11
2

Here's my spin on the resize:

<script>
function resizeReCaptcha() {

  var width = $( ".g-recaptcha" ).parent().width();

  if (width < 302) {
      var scale = width / 302;
  } else {
      var scale = 1;
  }

  $( ".g-recaptcha" ).css('transform', 'scale(' + scale + ')');
  $( ".g-recaptcha" ).css('-webkit-transform', 'scale(' + scale + ')');
  $( ".g-recaptcha" ).css('transform-origin', '0 0');
  $( ".g-recaptcha" ).css('-webkit-transform-origin', '0 0');
};

$( document ).ready(function() {

    $( window ).on('resize', function() {
        resizeReCaptcha();
    });

    resizeReCaptcha();

});
</script>
phoenix
  • 1,629
  • 20
  • 11
1

Unfortunately, NoCaptcha uses an iframe so at most you can control the height/width and use overflow:hidden; to cut off the excess. I would not recommend cutting off more than a few pixels of the Captcha for best usability.

Example:

.g-recaptcha {
max-width: 300px;
overflow: hidden;
}
1

On my site the re-captcha was getting cut off and looked bad.

enter image description here

After some experimentation I was able to fix the cutoff issue with this style update:

<style>
  .g-recaptcha>div>div>iframe {
     width: 380px;
     height: 98px;
  }
</style>

enter image description here

Dave
  • 359
  • 2
  • 5
1

Hope you find this useful

@media only screen and (max-width : 480px) {
    .smallcaptcha{
        transform:scale(0.75);
        transform-origin:50% 50%;
    }

}
Avi E. Koenig
  • 360
  • 5
  • 13
0
<div class="g-recaptcha" data-theme="light" data-sitekey="your site key" style="transform:scale(0.77);-webkit-transform:scale(0.77);transform-origin:0 0;-webkit-transform-origin:0 0;"></div>

This working for me, you try it..

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
0
<div class="g-recaptcha" data-theme="dark"></div>
Fahimeh Ahmadi
  • 813
  • 8
  • 13
  • 4
    Can you explain how a) how this addresses the question, and b) how it improves upon the previous answers from five years ago? On initial glance, it doesn’t seem to answer the question. But perhaps I’m missing something you could clarify? – Jeremy Caney May 11 '20 at 05:21