ReCaptcha API v2 Styling

前端 未结 18 1807
孤城傲影
孤城傲影 2020-11-28 02:59

I have not had much success finding how to style Google\'s new recaptcha (v2). The eventual goal is to make it responsive, but I am having difficulty applying styling for ev

相关标签:
18条回答
  • 2020-11-28 03:39

    Add a data-size property to the google recaptcha element and make it equal to "compact" in case of mobile.

    Refer: google recaptcha docs

    0 讨论(0)
  • 2020-11-28 03:44

    I use below trick to make it responsive and remove borders. this tricks maybe hide recaptcha message/error.

    This style is for rtl lang but you can change it easy.

    .g-recaptcha {
        position: relative;
        width: 100%;
        background: #f9f9f9;
        overflow: hidden;
    }
    
    .g-recaptcha > * {
        float: right;
        right: 0;
        margin: -2px -2px -10px;/*remove borders*/ 
    }
    
    .g-recaptcha::after{
        display: block;
        content: "";
        position: absolute;
        left:0;
        right:150px;
        top: 0;
        bottom:0;
        background-color: #f9f9f9;
        clear: both;
    }
    
    <div class="g-recaptcha" data-sitekey="Your Api Key"></div>
    <script src='https://www.google.com/recaptcha/api.js?hl=fa'></script>
    

    0 讨论(0)
  • 2020-11-28 03:44

    If someone is still interested, there is a simple javascript library (no jQuery dependency), named custom recaptcha. It lets you customize the button with css and implement some js events (ready/checked). The idea is to make the default recaptcha "invisible" and put a button over it. Just change the id of the recaptcha and that's it.

    <head>
        <script src="https://azentreprise.org/download/custom-recaptcha.min.js"></script>
        <style type="text/css">
            #captcha {
                float: left;
                margin: 2%;
    
                background-color: rgba(72, 61, 139, 0.5); /* darkslateblue with 50% opacity */
                border-radius: 2px;
    
                font-size: 1em;
                color: #C0FFEE;
            }
    
            #captcha.success {
                background-color: rgba(50, 205, 50, 0.5); /* limegreen with 50% opacity */
                color: limegreen;
            }
        </style>
    </head>
    <body>
        <div id="captcha" data-sitekey="your_site_key" data-label="Click here" data-label-spacing="15"></div>
    </body>
    

    See https://azentreprise.org/read.php?id=1 for more information.

    0 讨论(0)
  • 2020-11-28 03:47

    What you can do is to hide the ReCaptcha Control behind a div. Then make your styling on this div. And set the css "pointer-events: none" on it, so you can click through the div (Click through a DIV to underlying elements).

    The checkbox should be in a place where the user is clicking.

    0 讨论(0)
  • 2020-11-28 03:47

    Just adding a hack-ish solution to make it responsive.

    Wrap the recaptcha in an extra div:

    <div class="recaptcha-wrap">                   
        <div id="g-recaptcha"></div>
    </div>
    

    Add styles. This assumes the dark theme.

    // Recaptcha
    .recaptcha-wrap {
        position: relative;
        height: 76px;
        padding:1px 0 0 1px;
        background:#222;
        > div {
            position: absolute;
            bottom: 2px;
            right:2px;
            font-size:10px;
            color:#ccc;
        }
    }
    
    // Hides top border
    .recaptcha-wrap:after {
        content:'';
        display: block;
        background-color: #222;
        height: 2px;
        width: 100%;
        top: -1px;
        left: 0px;
        position: absolute;
    }
    
    // Hides left border
    .recaptcha-wrap:before {
        content:'';
        display: block;
        background-color: #222;
        height: 100%;
        width: 2px;
        top: 0;
        left: -1px;
        position: absolute;
        z-index: 1;
    }
    
    // Makes it responsive & hides cut-off elements
    #g-recaptcha {
        overflow: hidden;
        height: 76px;
        border-right: 60px solid #222222;
        border-top: 1px solid #222222;
        border-bottom: 1px solid #222;
        position: relative;
        box-sizing: border-box;
        max-width: 294px;
    }
    

    This yields the following:

    It will now resize horizontally, and doesn't have a border. The recaptcha logo would get cut off on the right, so I am hiding it with a border-right. It's also hiding the privacy and terms links, so you may want to add those back in.

    I attempted to set a height on the wrapper element, and then vertically center the recaptcha to reduce the height. Unfortunately, any combo of overflow:hidden and a smaller height seems to kill the iframe.

    0 讨论(0)
  • 2020-11-28 03:48

    I am just adding this kind of solution / quick fix so it won't get lost in case of a broken link.

    Link to this solution "Want to add link How to resize the Google noCAPTCHA reCAPTCHA | The Geek Goddess" was provided by Vikram Singh Saini and simply outlines that you could use inline CSS to enforce framing of the iframe.

    // Scale the frame using inline CSS
     <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> 
    
    // Scale the images using a stylesheet
    <style>
    #rc-imageselect, .g-recaptcha {
      transform:scale(0.77);
      -webkit-transform:scale(0.77);
      transform-origin:0 0;
      -webkit-transform-origin:0 0;
    }
    </style>
    
    0 讨论(0)
提交回复
热议问题