Page Summary
-
Ad slots must define eligible ad sizes, which can be fixed, multi-size, fluid, or responsive to the viewport.
-
Fixed-size ads require a defined
element to prevent layout shifts, while multi-size ads need layout flexibility to accommodate the largest size.Fluid ads, primarily used for native ads, automatically adapt to creative content but can impact performance and require careful consideration.
Responsive ads tailor ad sizes to different devices by mapping viewport sizes to eligible ad sizes, offering greater control over ad delivery.
Ad Manager's ad selection process considers only creatives matching the defined ad sizes, ensuring relevant ad delivery.
Every ad slot you define must specify the ad size(s) eligible to serve in that slot. The way ad sizes are specified varies depending on the type of ads to be displayed, as well as the size and flexibility of the ad slots themselves.
In some cases, ad size might be overridden at the line item level within Google Ad Manager. Visit the help center to learn more.
Full code for the examples included in this guide can be found on the ad sizes sample page.
Fixed-size ads
You can define an ad slot with a single fixed size.
googletag .defineSlot("/6355419/Travel/Europe/France/Paris", [300, 250], "fixed-size-ad") .addService(googletag.pubads());In this example, only creatives with size
300x250will be served.Key point: When working with fixed-size ads, we strongly recommend that you define the size of the
element where the creative will render. Since creatives are often rendered asynchronously, they might cause other elements on the page to shift if insufficient space is reserved for them.Multi-size ads
If an ad supports multiple sizes, provide a list of supported sizes when defining the ad slot.
googletag .defineSlot( "/6355419/Travel/Europe", [[300, 250], [728, 90], [750, 200], "fluid"], "multi-size-ad", ) .addService(googletag.pubads());In this example, creatives with sizes
300x250,728x90, and750x200can be served. Ad Manager only considers creatives matching these sizes during the ad selection process.If dimensions are not specified for the ad slot
in CSS, GPT automatically sets the dimensions equal to the shortest declared height and widest declared width over 1px whendisplay()is called. In this case, that would be750x90. However, this sizing might occur after other content on the page has loaded, causing that content to shift. To avoid layout shifts, reserve space using CSS as shown in the minimize layout shift guide.When working with multi-size ads, ensure that your layout is flexible enough to support an ad with the largest size specified. This will avoid creatives being inadvertently cropped.
Fluid ads
Fluid ads have no fixed size, but rather adapt to fit the creative content they display. Native ads are currently the only fluid ad type supported by Ad Manager.
When working with fluid ads, a custom
fluidsize might be specified.googletag .defineSlot("/6355419/Travel", ["fluid"], "native-ad") .addService(googletag.pubads());In this example, the ad slot will have the width of its parent container and resize its height to fit the creative content. The steps GPT takes to resize the ad slot are as follows:
- Ad response is received.
- Creative content is written into an iframe, with initial height set to
0pxand width set to100%. - Once all resources in the iframe have finished loading, the creative is made
visible by setting the height of the iframe equal to the height of the
iframe's
element.
Responsive ads
Responsive ads extend multi-size ads and allow you to specify the size of the creatives to serve based on the size of the viewport of the browser making the request. This functionality can be used to control the size of creatives served to different types of devices (desktop, tablet, mobile, etc.).
This is accomplished by defining a mapping between viewport size and ad size, then associating that mapping with an ad slot.
const responsiveAdSlot = googletag .defineSlot( "/6355419/Travel/Europe", [ [300, 250], [728, 90], [750, 200], ], "responsive-ad", ) .addService(googletag.pubads()); const mapping = googletag .sizeMapping() .addSize( [1024, 768], [ [750, 200], [728, 90], ], ) .addSize([640, 480], [300, 250]) .addSize([0, 0], []) .build(); responsiveAdSlot.defineSizeMapping(mapping);
In this example, the mapping specifies:
- When viewport >=
1024x768, ads sized750x200or728x90can serve. - When
1024x768> viewport >=640x480, ads sized300x250can serve. - When viewport <
640x480, no ads can serve.
GPT will detect the size of the viewport of the browser making the request and use the largest mapping that fits. To determine the largest mapping GPT first considers width, then height (i.e.,
[100, 10]>[10, 100]). In the event of an error in the mapping or if the viewport size can't be determined, the sizes specified indefineSlot()will be used.The mapping is then associated with an ad slot by calling the Slot.defineSizeMapping() method. This method accepts an array of mappings in the following format:
[ [ [viewport-width-1, viewport-height-1], [[ad-width-1, ad-height-1], [ad-width-2, ad-height-2], ...] ], [ [viewport-width-2, viewport-height-2], [[ad-width-3, ad-height-3], [ad-width-4, ad-height-4], ...] ], ... ]
The order of viewport sizes within this array defines their priority. The
SizeMappingBuilderused in the example above is a convenient way to generate an array of this format, with sizes automatically ordered from largest to smallest. In that example, the output ofSizeMappingBuilder.build()is:[ [[1024, 768], [[750, 200], [728, 90]]], [[640, 480], [[300, 250]]], [[0, 0], []] ]Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-10-31 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-10-31 UTC."],[],["Ad slots require specified ad sizes, varying by ad type and slot flexibility. Fixed-size ads use a single size, like `300x250`. Multi-size ads support various sizes, such as `300x250`, `728x90`, and `750x200`, with Ad Manager selecting matching creatives. Fluid ads adapt to creative content, often used for native ads. Responsive ads define different creative sizes based on viewport dimensions, mapping viewport sizes to ad sizes for various devices, and will adapt dynamically based on the browser's detected size.\n"]]