78

We need to tweak the styling of the dropdown that shows the autocomplete place suggestions when using the Google Places/Maps Autocomplete API.

Does anyone know if this is even possible? If so, I guess we just need to know the CSS classnames/IDs.

There's a screen grab of the bit I am referring to here:

Screengrab >

Matt
  • 179
  • 7
Oskar Smith
  • 999
  • 2
  • 8
  • 9
  • Must-have development tool: http://getfirebug.com/ That will answer your questions about what's what in the document. – Wesley Murch Oct 25 '11 at 18:26
  • Thanks for the reply. We've got Firebug and did try looking at the Response for the Autosuggest script but it looks like the data might be encoded? Here's an example row data: ["Sao Paulo - São Paulo, Brazil",null, ["locality","political","geocode"],"CkQ2AAAAmYczUjki5Q7gIrfQPsUYANQF04pl_SDqNY5dGAmkVtZ-VaQHgEsa9LwrSlZ7udzS4tmF808EsRLH5VOYjWJyzxIQ3kMbArn71dQ9Ou2Ezv_GhBoU6LlzspE4kaKqWf_Dck-zde33x2I","fedb05012f42e79f038a58eac44e1bbc61b7c7aa",[["Sao Paulo",0],["São Paulo",12],["Brazil",23]],[[0,2]]] As you can see, there's no nice clean HTML in there we can use to discover what's going on. – Oskar Smith Oct 26 '11 at 08:01
  • P.S. Sorry, couldn't work out how to get markdown line breaks for the code quote above. – Oskar Smith Oct 26 '11 at 08:06

9 Answers9

153

This is now documented by google: https://developers.google.com/maps/documentation/javascript/places-autocomplete#style_autocomplete

enter image description here

jla
  • 4,191
  • 3
  • 27
  • 44
User
  • 62,498
  • 72
  • 186
  • 247
56

If you use firebug (as mentioned in a comment to your question...) you see that the container with the autocomplete results is a DIV with the class "pac-container" and the suggestions are inside it as a DIV with the class "pac-item". so just style with CSS.

DeyyyFF
  • 953
  • 7
  • 9
  • 2
    Excellent, thank you! I can confirm these are indeed the class names for the dropdown and can be used to style the dropdown. You can also change the width of the maps auto-complete dropdown using min-width. i.e. min-width:250px; – Oskar Smith Nov 17 '11 at 11:45
  • i needed to reposition the results and i did it with a padding on the input field for the autocomplete. this doesnt affect the position of the text in the inputfield but the position of the autocomplete results. – DeyyyFF Nov 17 '11 at 14:26
  • 2
    also `.pac-selected` on the `.pac-item` when you arrow down to a result – Aidan Feldman Jul 16 '13 at 00:10
  • Many thanks, as I read "pac-container" I understood everything!!! Many thanks again! – peppelauro Oct 22 '14 at 20:02
19

This CSS will allow the drop-down to resize to fit the width of the results:

.pac-container, .pac-item {
    width: inherit !important;
}
Brett Pennings
  • 1,489
  • 14
  • 19
15

It is pretty difficult to inspect the elements since it closes as soon as it loses focus.

Though we know that the container has the .pac-container class and items have .pac-item, upon further investigating the API I found that it embeds the CSS styles in the document.

Here's what initially there, so use it to change the pre-defined styles to fit your needs.

.pac-container {
    background-color: #fff;
    position: absolute!important;
    z-index: 1000;
    border-radius: 2px;
    border-top: 1px solid #d9d9d9;
    font-family: Arial, sans-serif;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    overflow: hidden
}

.pac-logo:after {
    content: "";
    padding: 1px 1px 1px 0;
    height: 16px;
    text-align: right;
    display: block;
    background-image: url(https://maps.gstatic.com/mapfiles/api-3/images/powered-by-google-on-white3.png);
    background-position: right;
    background-repeat: no-repeat;
    background-size: 120px 14px
}
.hdpi.pac-logo:after {
    background-image: url(https://maps.gstatic.com/mapfiles/api-3/images/powered-by-google-on-white3_hdpi.png)
}
.pac-item {
    cursor: default;
    padding: 0 4px;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
    line-height: 30px;
    text-align: left;
    border-top: 1px solid #e6e6e6;
    font-size: 11px;
    color: #999
}
.pac-item:hover {
    background-color: #fafafa
}
.pac-item-selected,
.pac-item-selected:hover {
    background-color: #ebf2fe
}
.pac-matched {
    font-weight: 700
}
.pac-item-query {
    font-size: 13px;
    padding-right: 3px;
    color: #000
}
.pac-icon {
    width: 15px;
    height: 20px;
    margin-right: 7px;
    margin-top: 6px;
    display: inline-block;
    vertical-align: top;
    background-image: url(https://maps.gstatic.com/mapfiles/api-3/images/autocomplete-icons.png);
    background-size: 34px
}
.hdpi .pac-icon {
    background-image: url(https://maps.gstatic.com/mapfiles/api-3/images/autocomplete-icons_hdpi.png)
}
.pac-icon-search {
    background-position: -1px -1px
}
.pac-item-selected .pac-icon-search {
    background-position: -18px -1px
}
.pac-icon-marker {
    background-position: -1px -161px
}
.pac-item-selected .pac-icon-marker {
    background-position: -18px -161px
}
.pac-placeholder {
    color: gray
}
Atural
  • 5,389
  • 5
  • 18
  • 35
Deepak Kamat
  • 1,880
  • 4
  • 23
  • 38
  • 2
    Tip: In Chrome, press F8 to pause the script while the dropdown is still open then you'll be able to inspect it. – Ikbel Nov 15 '21 at 13:29
13

I case anyone is interested in the hierarchy I was able to scrape the following using Firebug:

<div class="pac-container pac-logo" style="width: 557px; position: absolute; left: 66px; top: 106px; display: none;">
    <div class="pac-item">
        <span class="pac-icon pac-icon-marker"></span>
        <span class="pac-item-query">
            <span>France</span>
        </span>
    </div>
<div>
Community
  • 1
  • 1
apebeast
  • 368
  • 3
  • 15
7

This worked for me, and now I can run this on mobile!

.pac-container {
    z-index: 10000 !important;
    width: auto !important;
    position: initial !important;
    left: 0 !important;
    right: 0 !important;
    display: block !important;
}
.pac-container:empty{
    display: none !important;
}

And this somewhere!

$('selector').append('.pac-container');

Now the results will show in the selected div as a normal block element :)

Jack Franzen
  • 768
  • 7
  • 21
  • be careful though, the display block will cause the list to stay open if you click outside of it – Majid Mar 08 '18 at 09:38
3

To force the box to stay open for much easier styling and inspection from dev tools you can set the input value from the JS console and the container will stay open when inspecting from the dev tools.

Simply run document.querySelector('.pac-target-input').value = 'CB' in the console on your page with the input, then go back to the Elements tab and you can now individually inspect each element.

This gets around the issue where it always closes when focus is lost.

See this screenshot showing how it works

HastingsDirect
  • 628
  • 1
  • 9
  • 17
  • Above did not work for me. assuming there was some change in API Following JS code works as of today document.querySelector('.pac-container').style.display = 'block' – Syed Danial Jun 15 '22 at 17:17
3

if you want to create your custom html & css then AutocompleteService class is also available which will provide all data in json.

const service = new google.maps.places.AutocompleteService();
service.getQueryPredictions(
    { input: "provide location string here for search" },
    (suggestions) => console.log(suggestions)
);
Maulik
  • 2,881
  • 1
  • 22
  • 27
0

For easier debugging and styling. to keep the dropdown open. use the following code in chrome console.

document.querySelector('.pac-container').style.display = 'block' 

Also used following classes to style the suggested dropdown in google places auto complete Update icons

.pac-icon {
  background-image: url('./assets/locationMark.svg') !important;
  background-repeat: no-repeat;
  background-position: 0 0;
  background-size: 14px 18px;
}

Update text

.pac-item-query {
  font-size: 16px
}

Hide google logo

.pac-logo {
  padding: 10px 0;
  &::after {
    display: none;
  }
}
Syed Danial
  • 635
  • 1
  • 6
  • 16