【问题标题】:Displaying responsive Google Ads - CSS problems显示自适应 Google Ads - CSS 问题
【发布时间】:2014-04-23 04:05:17
【问题描述】:

我正在尝试在我的网站上展示自适应 Google Ads。包含 Google Ads 代码的框也是响应式的,它会根据Google's Ad unit sizes 将自身转换为不同的尺寸。

因此,对于不同的屏幕尺寸,我会从该页面显示合适的广告尺寸,就像这样(广告空间的完整 LESS):

.ads {
    width: 733px;
    height: 116px;
    margin: 70px 0 0 30px;
    border: 2px solid @dry-red;
    background-color: white;
    padding: 0;
    border-radius: 4px;
    margin-right: 15px;
    float: right;

    .headline {
        background-color: @dry-red;
        width: 100%;
        font-size: .8em;
        padding: 2.5px 0 2.5px 4px;
        color: white;
        margin: 0;
        border-bottom: 1px solid @dry-red-xl;
    }

    @media (max-width: 1200px){
        width: 468px;
        height: 86px;
    }

    @media (max-width: 992px){
        width: 323px;
        height: 126px;
    }

    @media (max-width: 768px){
        width: 468px;
        height: 61px;
        margin: 50px 20px;
        float: none;
    }

    @media (max-width: 766px){
        margin: 5px auto 5px auto;
    }

    @media (max-width: 470px){
        /* display: none; */
        width: 323px;
        height: 126px;
    }
}

到目前为止一切顺利。盒子在达到顶峰时会发生变化。请注意,Google 的原始广告尺寸增加了大约 26 像素以上,以适应 .headline

现在,我将 Google Ads 单元的代码放在 HTML 中,如下所示:

<section class="ads col-lg-8 col-md-7 col-sm-7">
    <div class="headline">Advertisement</div>
    <!-- IMGzer_Header --> 
    <style type="text/css">
        .adslot_1 { width: 728px; height: 90px; }
        @media (max-width:470px) { .adslot_1 { width: 320px; height: 100px; } }
        @media (max-width:768px) { .adslot_1 { width: 468px; height: 60px; } }
        @media (max-width:992px) { .adslot_1 { width: 320px; height: 100px; } }
        @media (max-width:1200px) { .adslot_1 { width: 468px; height: 60px; } }
    </style>
    <ins class="adsbygoogle adslot_1" style="display:inline-block" data-ad-client="SOME_NUMBER" data-ad-slot="SOME_NUMBER"></ins>
    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> 
    <script>(adsbygoogle = window.adsbygoogle || []).push({});</script>
</section>

您可能会注意到,我尝试使用两种变体:

  • 使用 CSS3 媒体查询(已注释)
  • 并使用data-ad-format="horizontal" 数据属性。

我在Google Ads article 中读到了这两个。

这些都不起作用,我的广告尺寸仍然是728x90px。当浏览器屏幕变小时,我的 Google 广告根本不会调整到它们各自的尺寸。

我们将不胜感激。

My actual website having this problem


更新

所以我玩了一遍,仍然一无所获。我试图更接近 Google 的示例,并在媒体查询中使用 min-width 而不是 max-width。没有结果。我仍然得到 728x90 尺寸,尽管除了 min-width: 1200px 媒体查询之外它不包含在其他任何地方,并且它仍然拒绝调整大小。这真的很烦人。

<section class="ads col-lg-8 col-md-7 col-sm-7">
    <div class="headline">Advertisement</div>
    <style>
        .adslot_1 { width: 320px; height: 100px; }
        @media (min-width: 768px) { .adslot_1 { width: 468px; height: 60px; } }
        @media (min-width: 992px) { .adslot_1 { width: 320px; height: 100px; } }
        @media (min-width: 1200px) { .adslot_1 { width: 728px; height: 90px; } }
    </style>
    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> 
    <!-- IMGzer_Header -->
    <ins class="adsbygoogle adslot_1" style="display:inline-block" data-ad-client="ca-pub-1234" data-ad-slot="XXXXXX"></ins>
    <script>(adsbygoogle = window.adsbygoogle || []).push({});</script>
</section>
<div class="clearfix"></div>

.ads 的 CSS/LESS 仍然与上面相同。我注意到的是,当我通过 Firebug 检查我的代码时,我看到了这一行:

<ins data-ad-slot="XXXXX" data-ad-client="ca-pub-1234" style="display: inline-block; width: 728px; height: 90px;" class="adsbygoogle adslot_1" data-adsbygoogle-status="done"><ins style="display:inline-table;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:728px;background-color:transparent"><ins style="display:block;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:728px;background-color:transparent" id="aswift_0_anchor"><iframe width="728" scrolling="no" height="90" frameborder="0" style="left:0;position:absolute;top:0;" name="aswift_0" id="aswift_0" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){setTimeout(h,0)}else if(h.match){try{h=s.upd(h,i)}catch(e){}w.location.replace(h)}}" allowtransparency="true" hspace="0" vspace="0" marginheight="0" marginwidth="0"></iframe></ins></ins></ins>

这段代码是从哪里得到宽度和高度的?宽度为 728,高度为 90。就好像他们以某种方式固定在那里,他们完全忽略了我的媒体查询。我绝对确定我在 AdSense 设置中选择了响应式单元

我没有看到什么?

【问题讨论】:

    标签: html css media-queries ads


    【解决方案1】:

    类 (.) 小于 ID(#)(样式术语)。 谷歌广告有自己的风格,写在JS

    <div class="least" id="medium"></div>
    

    <style type="text/css">
         .least{
                   width:100px;
                   height:100px;
                   background-color:red;
               }
         #medium{
                   width:200px;
                   height:200px;
                   background-color:green;
               }
    </style> 
    

    处理样式谷歌广告的最佳方式

    <div id="google-ads-1"></div>
    
     //Calculate the width of available ad space:
    
    <script type="text/javascript"> 
          ad = document.getElementById('google-ads-1');
    
          if (ad.getBoundingClientRect().width) {
              adWidth = ad.getBoundingClientRect().width; // for modern browsers 
          } else {
              adWidth = ad.offsetWidth; // for old IE 
          }
    
          /* Replace ca-pub-XXX with your AdSense Publisher ID */ 
                google_ad_client = "ca-pub-XXX";
    
          /* Replace 1234567890 with the AdSense Ad Slot ID */ 
                google_ad_slot = "1234567890";
    
          /* Do not change anything after this line */
                if ( adWidth >= 728 )
            google_ad_size = ["728", "90"];  /* Leaderboard 728x90 */
          else if ( adWidth >= 468 )
            google_ad_size = ["468", "60"];  /* Banner (468 x 60) */
          else if ( adWidth >= 336 )
            google_ad_size = ["336", "280"]; /* Large Rectangle (336 x 280) */
          else if ( adWidth >= 300 )
            google_ad_size = ["300", "250"]; /* Medium Rectangle (300 x 250) */
          else if ( adWidth >= 250 )
            google_ad_size = ["250", "250"]; /* Square (250 x 250) */
          else if ( adWidth >= 200 )
            google_ad_size = ["200", "200"]; /* Small Square (200 x 200) */
          else if ( adWidth >= 180 )
            google_ad_size = ["180", "150"]; /* Small Rectangle (180 x 150) */
          else
            google_ad_size = ["125", "125"]; /* Button (125 x 125) */
    
          document.write (
           '<ins class="adsbygoogle" style="display:inline-block;width:' 
            + google_ad_size[0] + 'px;height:' 
            + google_ad_size[1] + 'px" data-ad-client="' 
            + google_ad_client + '" data-ad-slot="' 
            + google_ad_slot + '"></ins>'
          );
    
          (adsbygoogle = window.adsbygoogle || []).push({});
    
    </script>
    
    
    <script async src="http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">
    </script>
    

    documentationeasy。

    【讨论】:

    • 我认为在这种情况下使用 JS 将是矫枉过正,因为即使 Google 本身也不建议使用它来制作响应式广告。他们要么希望发布者让 Google 在环境允许的情况下处理响应,要么像我一样使用媒体查询。我想我的 .ads 课程只是失败了,我看不到确切的位置。
    • 不使用类.,因为它不起作用。使用身份#,它应该可以顺利运行。
    • 在您指出后也尝试过(我将adslot_1 设为 ID 而不是类) - 结果相同。
    • 谷歌广告都有特定的尺寸。 JS 的目的是仅更改先前定义广告商的最小 atro 公告。如果 JS 不工作我觉得有点困难。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    相关资源
    最近更新 更多