HTML Formatterhtmlformatteronline.com

Google Maps embed

A responsive map with a title, lazy loading and an optional click-to-load consent gate.

embedgoogle mapsiframeresponsiveprivacy
<!-- 1. the straightforward version -->
<div class="map">
  <iframe
    src="https://www.google.com/maps?q=Leeds+City+Museum,+Leeds&output=embed"
    title="Map showing Leeds City Museum, Millennium Square, Leeds"
    loading="lazy"
    referrerpolicy="no-referrer-when-downgrade"
    allowfullscreen></iframe>
</div>

<p class="under">
  <a href="https://www.google.com/maps?q=Leeds+City+Museum,+Leeds" target="_blank" rel="noopener">
    Open in Google Maps
  </a> · 12 Mill Lane, Leeds LS1 4DT
</p>

<!-- 2. click to load, so nothing third-party runs until the visitor asks -->
<h3>Consent-friendly version</h3>
<div class="map facade" id="map2"
     data-src="https://www.google.com/maps?q=Leeds+City+Museum,+Leeds&output=embed"
     data-title="Map showing Leeds City Museum, Millennium Square, Leeds">
  <button type="button" class="load">
    <svg viewBox="0 0 24 24" aria-hidden="true">
      <path d="M12 21s7-6.2 7-11a7 7 0 1 0-14 0c0 4.8 7 11 7 11z"/><circle cx="12" cy="10" r="2.6"/>
    </svg>
    <strong>Show the map</strong>
    <span>Loads Google Maps, which sets cookies.</span>
  </button>
</div>

How it works

You do not need an API key for a basic map. The ?q=…&output=embed URL works with any address or place name and needs no account. The Maps Embed API — google.com/maps/embed/v1/place?key=… — gives you directions, search and street view modes, and that one does need a key.

aspect-ratio replaced the padding hack. For years the only way to size an iframe responsively was a wrapper with padding-bottom: 56.25% and an absolutely positioned child. aspect-ratio on the wrapper plus width: 100%; height: 100% on the iframe does the same thing in two lines and is readable.

The title attribute is not optional. An iframe without one is announced as just "frame" by a screen reader, with no indication of what is inside. This is one of the most common accessibility failures on the web, and it is one attribute. Describe what the map shows, not that it is a map.

loading="lazy" means the map is not fetched until it is near the viewport. A Google Maps embed pulls several hundred kilobytes and runs a lot of script; if it sits in your footer, this alone is a measurable speed win.

Always put the address in real text near the map. Search engines cannot read an iframe, screen reader users should not have to interact with a map to get an address, and if the embed fails the information is still there. The map is an enhancement, not the content.

Accessibility notes

The title attribute gives the frame its accessible name. Without it the frame is unlabelled and effectively unnavigable.

Maps are a keyboard trap for some users: tabbing into the embed hands focus to Google's controls, and getting back out is not always obvious. Putting the address and an "Open in Google Maps" link outside the frame means nobody has to enter it at all.

The click-to-load version is a real <button>, so it is focusable and activates on Enter. After loading, focus moves to the new iframe rather than being lost.

In the EU and UK, a Google Maps embed sets cookies and transfers data before any interaction, which generally requires consent. The facade version loads nothing until the visitor presses the button, which sidesteps the problem entirely.

Making it yours

Directions rather than a pin: use https://www.google.com/maps/dir/?api=1&destination=Leeds+City+Museum as the link target, and the Embed API's directions mode for the iframe.

Coordinates instead of a name: ?q=53.8008,-1.5491&output=embed. More reliable than a place name when several places share it.

Dark mode: the embed has no dark option, but filter: invert(90%) hue-rotate(180deg) on the iframe gives a passable dark map. It is a hack and it also inverts the Google logo, so check whether that breaks the terms of use for your case.

The address block is a good candidate for LocalBusiness structured data — that is what actually puts your opening hours and location into search results, which the embed does not.

Related templates

Check your version

Once you have edited this, the HTML validator will catch any tag you left unclosed, and the formatter will tidy the indentation. Both run in your browser.