Notification

Please make sure to visit Your AdSense Page where you can find personalized information about your account to help you succeed with AdSense.

Ad units

How to modify your responsive ad code

If you find that our responsive ad code doesn't do everything you need, you may modify your ad code to better meet the requirements of your responsive site. The examples in this article show you how to correctly make these modifications.

Before you start:

Note: The examples described in this article are acceptable modifications of the AdSense ad code. You won't violate the AdSense Program policies by modifying your responsive ad code in these approved ways.

Exact ad unit size per screen width example

This example shows you how to modify your responsive code to set specific ad unit sizes for three ranges of screen widths, i.e., mobile, tablet and desktop. You don't need to have any previous experience of CSS media queries or modifying AdSense ad code to follow this example.

Here's some modified responsive ad code that sets the following exact ad unit sizes per screen width:

  • For screen widths up to 500px: a 320x100 ad unit.
  • For screen widths between 500px and 799px: a 468x60 ad unit.
  • For screen widths of 800px and wider: a 728x90 ad unit.
<style>
.example_responsive_1 { width: 320px; height: 100px; }
@media(min-width: 500px) { .example_responsive_1 { width: 468px; height: 60px; } }
@media(min-width: 800px) { .example_responsive_1 { width: 728px; height: 90px; } }
</style>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ignored1234567890123456" crossorigin="anonymous"></script>
<!-- example_responsive_1 -->
<ins class="adsbygoogle example_responsive_1"
     style="display:block"
     data-ad-client="ignored1234567890123456"
     data-ad-slot="8XXXXX1"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

To adapt this sample code for your own site:

  1. Create a display ad unit in your AdSense account, making sure you leave Responsive selected in the "Ad size" section. Make a note of the following information from your responsive ad code:
    • Your publisher ID, for example, ignored1234567890123456
    • Your ad unit's ID (data-ad-slot), for example, 1234567890.
  2. In the sample code:
    • Replace all instances of example_responsive_1 with a unique name, e.g., Home_Page, front_page_123, etc.
      Notes:
      • Your unique name must only contain English letters (A-Z), numbers, and underscores, and the first character must be an English letter.
      • You must use a different unique name each time that you adapt this sample code.
    • Replace ignored1234567890123456 with your own publisher ID.
    • Replace 8XXXXX1 with your own ad unit's ID.
  3. Decide on the sizes you want your ad unit to take per screen width:
    • If you're happy with the existing ad unit sizes in the sample code, then you don't need to make any additional changes.
    • If you want to set different ad unit sizes per screen width, then, in the sample code:
      • Replace 320px and 100px with the width and height of the ad unit you want to use for screen widths up to 500px.
      • Replace 468px and 60px with the width and height of the ad unit you want to use for screen widths between 500px and 799px.
      • Replace 728px and 90px with the width and height of the ad unit you want to use for screen widths of 800px and wider.
  4. Copy and paste your modified ad code into the HTML source code of the page where you'd like the ads to appear.
    Tip: After you’ve placed your ad code, we recommend that you test your ads on different devices and screens to make sure that the responsive behavior is working correctly.

Advanced responsive ad code features examples

If you find that our responsive ad code doesn't do everything you need, you may modify your ad code to specify an exact size for your ad unit via CSS.

Note: We don't recommend using these advanced features unless you're confident about modifying your ad code.

Specify an expandable width and a fixed height

You may modify your responsive ad code to specify an expandable width and a fixed height for your ad unit via CSS. The following example shows you how to make these modifications:

Expandable width with fixed height example
This example shows you how to modify your responsive ad code to specify a fixed height of 90px and a variable width from min-width 400px to max-width 970px:
<ins class="adsbygoogle"
   style="display:block;min-width:400px;max-width:970px;width:100%;height:90px"
   data-ad-client="ignored1234567890123456"
   data-ad-slot="5678"></ins>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ignored1234567890123456" crossorigin="anonymous"></script>
<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>

Specify an exact size per screen width

You may modify your responsive ad code to specify the exact size for your ad unit via CSS. The following example shows you how to make these modifications:

Exact size per screen width example
If you know the exact ad unit sizes per device that best fit your responsive site, then you can use CSS3 media queries to set the size of your responsive ad unit. The following example shows you how to modify your ad code to use CSS3 media queries:
<style type="text/css">
.adslot_1 { width: 320px; height: 100px; }
@media (min-width:500px) { .adslot_1 { width: 468px; height: 60px; } }

@media (min-width:800px) { .adslot_1 { width: 728px; height: 90px; } }
</style>
<ins class="adsbygoogle adslot_1"
    style="display:block;"
    data-ad-client="ignored1234567890123456"
    data-ad-slot="5678"></ins>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ignored1234567890123456" crossorigin="anonymous"></script>
<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>
The @media rules are a CSS3 syntax, and are supported on all modern browsers. Note that setting the size of the ad unit via CSS in external style sheets is not officially supported.

Hiding an ad unit

In certain cases, particularly on smaller mobile devices, you might not want to show an ad at all. If you do want to hide an ad unit, you can set a parameter with CSS media queries so that no ad request is made and no ad is shown. The following example shows you how to make these modifications:

Hiding ads for specific screen sizes example
If you want to only show ads for certain screen sizes you can use CSS to accomplish this. The following example shows you how to modify your ad code to use CSS3 media queries to hide ads for specific screen sizes:
<style type="text/css">
.adslot_1 { display:block; width: 320px; height: 50px; }
@media (max-width: 400px) { .adslot_1 { display: none; } }
@media (min-width:500px) { .adslot_1 { width: 468px; height: 60px; } }
@media (min-width:800px) { .adslot_1 { width: 728px; height: 90px; } }
</style>
<ins class="adsbygoogle adslot_1"
   data-ad-client="ignored1234567890123456"
   data-ad-slot="5678"></ins>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ignored1234567890123456" crossorigin="anonymous"></script>
<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>

In this example, no ads are displayed if the screen width is less than 400px.

Note: Even with this flexibility, please remember to always follow our ad placement policies. Keep in mind that only limited modifications to your code are permitted by our Program policies.

Was this helpful?

How can we improve it?
true
Unlock Growth Potential

Don't miss out on valuable AdSense insights. Opt in to receive performance reports, personalized tips, and webinar invites that can help boost your earnings

Opt in

Search
Clear search
Close search
Google apps
Main menu
17148465703356459940
true
Search Help Center
true
true
true
true
true
157
false
false