谷歌AdSense广告匹配不到显示空白时用CSS隐藏或替换为图片美化

前端开发   发布日期:2023年05月30日   浏览次数:543

谷歌AdSense广告经常会出现匹配不到的时候,出现此种情况后会直接占位成空白,页面效果上看非常不好看,所以通过官方的介绍可以用CSS隐藏或替换成占位图片的链接。

Google 官方推荐使用 CSS,在广告代码中属性有使用 data-ad-status 来指示广告展示位置是否已填充,可以定位到应用 CSS 来隐藏元素。

  1. ins.adsbygoogle[data-ad-status="unfilled"] {
  2. display: none !important;
  3. }

方案一:增加一个图片链接,如果广告未填充显示出来就用指定的图片显示占位

  1. <div style="width:920px; height:auto; clear:both; margin:5px auto; text-align: center">
  2. <ins class="adsbygoogle"
  3. style="display:inline-block;width:880px;height:110px"
  4. data-ad-client="ca-pub-746454xxxxx"
  5. data-ad-slot="5151889170">
  6. <a href="https://www.19jp.com" target="_blank" class="zhanwei_a"><img src="/images/ad/ad_2.gif" style="width:880px; height:125px;"></a>
  7. </ins>
  8. <script>
  9. (adsbygoogle = window.adsbygoogle || []).push({});
  10. </script>
  11. </div>
  12. <style>
  13. ins.adsbygoogle .zhanwei_a {
  14. display: none !important;
  15. }
  16. ins.adsbygoogle[data-ad-status="unfilled"] .zhanwei_a {
  17. display: block !important;
  18. }
  19. </style>

方案二:如果广告未显示出来就直接隐藏

  1. <div class="art_ad_w2" style="width:920px; height:auto; clear:both; margin:10px auto; text-align: center;">
  2. <!-- 文章底部 -->
  3. <div class="left_column">
  4. <ins class="adsbygoogle w_2"
  5. style="display:inline-block;width:880px;height:110px"
  6. data-ad-client="ca-pub-746454xxxxx"
  7. data-ad-slot="5151889170"></ins>
  8. <script>
  9. (adsbygoogle = window.adsbygoogle || []).push({});
  10. </script>
  11. </div>
  12. </div>
  13. <style>
  14. ins.w_2[data-ad-status="unfilled"] {
  15. display: none !important;
  16. }
  17. </style>

以上就是谷歌AdSense广告匹配不到显示空白时用CSS隐藏或替换为图片美化的详细内容,更多关于谷歌AdSense广告匹配不到显示空白时用CSS隐藏或替换为图片美化的资料请关注九品源码其它相关文章!