--- url: "https://web.dev/articles/embed-best-practices" titulek: "Best practices for using third-party embeds | Articles | web.dev" h1: "Best practices for using third-party embeds Stay organized with collections Save and categorize content based on your preferences." popis: "This document discusses performance best practices that you can use when loading third-party embeds, efficient loading techniques and the Layout Shift Terminator tool that helps reduce layout shifts for popular embeds." jazyk: "en" kanonicka: "https://web.dev/articles/embed-best-practices" drobecky: ["Articles", "Best practices for using third-party embeds"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 00:05:22" znaku: 20568 slov: 2941 kodovani: "iso-8859-2" sha256: "cb7b1458165a22e77c5d88675dd55e5086162d29bdc81910ea9086251f7643cc" --- Best practices for using third-party embeds Stay organized with collections Save and categorize content based on your preferences. An overview of techniques to load popular third-party embeds efficiently. Addy Osmani Katie Hempenius Leena Sohoni Many sites use third-party embeds to create an engaging user experience by delegating some sections of a web page to another content provider. The most common examples of third-party content embeds are video players, social-media feeds, maps, and advertisements. Third-party content can impact the performance of a page in many ways. It can be render-blocking, contend with other critical resources for network and bandwidth, or affect the Core Web Vitals metrics. Third-party embeds may also cause layout shifts as they load. This article discusses performance best practices that you can use when loading third-party embeds, efficient loading techniques, and the Layout Shift Terminator tool that helps reduce layout shifts for popular embeds. What is an embed A third-party embed is any content displayed on your site that is: Not authored by you Served from third-party servers Embeds are frequently used in the following: Websites related to sports, news, entertainment, and fashion use videos to augment textual content. Organizations with active Twitter or social media accounts embed feeds from these accounts to their web pages to engage and reach out to more people. Restaurant, park, and event venue pages often embed maps. Third-party embeds are typically loaded in [`` The loading attribute supports the following values: `lazy`: Indicates that the browser should defer loading the iframe. The browser will load the iframe when it is nearing the viewport. Use if the iframe is a good candidate for lazy-loading. `eager`: Loads the iframe immediately. Use if the iframe is not a good candidate for lazy-loading. If the `loading` attribute has not been specified, this is the default behavior—except in [Lite mode](https://support.google.com/chrome/answer/2392284?co=GENIE.Platform%3DAndroid). `auto`: The browser determines whether to lazy-load this frame. Browsers that don't support the `loading` attribute ignore it, so you can apply browser-level lazy-loading as a progressive enhancement. Browsers that support the attribute may have different implementations for the [distance-from-viewport](/articles/browser-level-image-lazy-loading#distance-from-viewport_thresholds) threshold (the distance at which the iframe starts loading). Following are some ways in which you can lazy load iframes for different types of embeds. YouTube videos: To lazy-load a YouTube video player iframe, include the `loading` attribute to the embed code provided by YouTube. Lazy loading the YouTube embed can save approximately 500 KB on the initial page load. `` Google Maps: To lazy-load a Google Map iframe, include the `loading` attribute in the code for the iframe embed generated by the [Google Maps Embed API](https://developers.google.com/maps/documentation/embed/get-started). Following is an example of the code with a placeholder for the Google Cloud API key. `` lazysizes library Because browsers use an embed's distance from the viewport, in addition to signals like [effective connection type](https://googlechrome.github.io/samples/network-information/) and Lite-mode, to decide when an iframe should be loaded, browser lazy-loading can be inconsistent. If you need better control on the distance thresholds or you want to provide a consistent lazy-loading experience across browsers, you can use the [lazysizes](https://github.com/aFarkas/lazysizes) library. [lazysizes](https://github.com/aFarkas/lazysizes) is a fast, SEO-friendly lazy loader for both images and iframes. Once you have downloaded the component, it can be used with an iframe for a YouTube embed as follows. ` ` Similarly, lazysizes may be used with iframes for other third-party embeds. Note that lazysizes uses the [Intersection Observer API](https://developer.mozilla.org/docs/Web/API/Intersection_Observer_API) to detect when an element becomes visible. Using data-lazy in Facebook Facebook provides different types of [social plugins](https://developers.facebook.com/docs/plugins) that can be embedded. This includes posts, comments, videos, and the most popular *Like* button. All plugins include a setting for `data-lazy`. Setting it to `true` ensures that the plugin will use the browser's lazy-loading mechanism by setting the `loading="lazy"` iframe attribute. Lazy-loading Instagram feeds Instagram provides a block of markup and a script as part of the embed. The script injects an `` Popular embeds like YouTube, Google Maps, and Facebook provide the embed code with size attributes specified. However, there may be providers who don't include this. For example, this code snippet does not indicate the dimensions of the resulting embed. `Tweets by ChannelNewsAsia ` You can use DevTools to inspect the injected `iframe`after this page is rendered. As seen in the following snippet, the height of the injected iframe is fixed while the width is specified in percentage. `` This information can be used to set the size of the containing element to ensure that the container does not expand on loading the feed and there is no layout shift. Following snippet may be used to fix the size of the embed included previously. `
Tweets by ChannelNewsAsia
` Layout Shift Terminator Since third-party embeds often omit the dimensions (width, height) for the final content they render, they can cause significant layout shifts on a page. This problem can be tricky to address without manually inspecting the final sizes using DevTools at a variety of different viewport sizes. Now there's an automated tool, [Layout Shift Terminator](https://googlechromelabs.github.io/layout-shift-terminator/), that can help you reduce layout shifts from popular embeds, such as from Twitter, Facebook, and other providers. Layout Shift Terminator: Loads the embed client-side in an iframe. Resizes the iframe to various popular viewport sizes. For each popular viewport, captures the dimensions of the embed to later generate media queries and container queries. Sizes a min-height wrapper around the embed markup using media queries (and container queries) until the embed initializes (after which the min-height styles are removed). Generates an optimized embed snippet that can be copy and pasted where you would otherwise be including the embed in your page. Try out the Layout Shift Terminator, and feel free to leave any feedback on [the Layout Shift Terminator GitHub project](https://github.com/GoogleChromeLabs/layout-shift-terminator). The tool is in a beta state and aims to improve over time with further refinements. Conclusion Third-party embeds can provide a lot of value to users, but as the number and size of embeds on a page increases, performance can suffer. That's why it is necessary to measure, judge, and use appropriate loading strategies for embeds based on their position, relevance, and potential users' needs. Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2021-10-05 UTC.