Skip to content

Instantly share code, notes, and snippets.

@jeroenvisser101
Last active May 9, 2023 01:31
Show Gist options
  • Save jeroenvisser101/9254cf27df85cf1c853aee17e4b49766 to your computer and use it in GitHub Desktop.
Save jeroenvisser101/9254cf27df85cf1c853aee17e4b49766 to your computer and use it in GitHub Desktop.
Hide Safari contacts auto-fill when [autocomplete="off"]
input[autocomplete="off"]::-webkit-contacts-auto-fill-button {
visibility: hidden;
display: none !important;
pointer-events: none;
height: 0;
width: 0;
margin: 0;
}
@hadagarcia
Copy link

hadagarcia commented Oct 19, 2017

Thanks! It worked for me :) .
It can be used to hide the credentials button:

input[autocomplete="off"]::-webkit-contacts-auto-fill-button,
input[autocomplete="off"]::-webkit-credentials-auto-fill-button {
visibility: hidden;
display: none !important;
pointer-events: none;
height: 0;
width: 0;
margin: 0;
}

@nathan-charrois
Copy link

Just a heads up, this will not hide Safari's 11 user dropdown, only the autofill button. Also pointer-events don't apply to hidden elements, so this property is not needed.

@vidit1
Copy link

vidit1 commented Jan 31, 2018

So is there any solution to remove the dropdown also. I am stuck with location suggestion in safari

@z5h
Copy link

z5h commented Jul 9, 2019

@vidit1 Did you find a solution?

@lekoala
Copy link

lekoala commented Sep 4, 2019

@z5h, @vidit1, so if anyone reads this (this is the first page i've found in google about this topic), here is my solution about the safari dropdown

basically, safari will scan the name, the placeholder and the label of the field. if it sees anything relevant, it will trigger the dropdown. the solution is to store the name in a data-name attribute and restore it on submit. As for the label, what I've done is to use a zero width joiner to garble the label while keeping it readable.

sample jquery code below to give an idea, not very good, but working

function makeid(length) {
    length = length || 10;
    var result = '';
    var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var charactersLength = characters.length;
    for (var i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result;
}

function applyFormDataAttribute(form) {
    form.find('input[data-name]').each(function() {
        var $this = $(this);
        var rid = makeid();
        // for chrome
        $this.attr("autocomplete", "new-" + rid);
        // unreadable name
        $this.attr('name', 'ac_' + makeid());
        $this.addClass('autocomplete-disabled');

        // warning ! safari will read the placeholder, it's better to use a label
        if ($this.data('placeholder')) {
            $this.attr('placeholder', $this.data('placeholder'));
        }
        if ($this.data('label')) {
            var chars = [];
            var zwLabel = '';
            // joins chars with a zero width joiner
            chars = $this.data('label').split('');
            for (var i = 0; i < chars.length; i++) {
                if (i != chars.length - 1) {
                    zwLabel += chars[i] + '‍&zwj;';
                } else {
                    zwLabel += chars[i];
                }
            }
            $this.closest('label').html(zwLabel);
        }
    });
}

function restoreDisabledAutocomplete(form) {
    form.find('.autocomplete-disabled').each(function() {
        var $this = $(this);
        if ($this.data('name')) {
            $this.attr('name', $this.data('name'));
        }
        $this.removeClass('autocomplete-disabled');
    });
}

$(function() {
    $('form.apply-form-data').each(function() {
        var form = $(this);
        applyFormDataAttribute(form);

        // on submit restore any remaining field that is faked
        form.on('submit', function() {
            restoreDisabledAutocomplete(form);
        });
    });
});

@AliveDD
Copy link

AliveDD commented Feb 4, 2020

Hello, you can try use autocomplete="new-password" - its crutch, but its works for me

@Gurvinder-Abhaypal
Copy link

it still is flaky and might not work for different versions

@oleh-evolutn
Copy link

input[autocomplete="off"]::-webkit-contacts-auto-fill-button,
input[autocomplete="off"]::-webkit-credentials-auto-fill-button {
visibility: hidden;
display: none !important;
pointer-events: none;
height: 0;
width: 0;
margin: 0;
}

It also works for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment