Forum Moderators: martinibuster

Message Too Old, No Replies

Hiding Google ads on specific screen size

I need to hide an ad only on screens less than 728px

         

ashinjohn001

11:57 pm on Jul 22, 2020 (gmt 0)

5+ Year Member



Hello,

I'm trying to hide an ad using Google approved CSS media queries, but for some reason, no ad is being shown in the space. Here is the code that I'm using


style> type="text/css">
.hideinmobile { display:inline-block; width: 320px; height: 50px; }
@media (max-width: 768px) { .hideinmobile { display: none; } }
</style>
<ins class="adsbygoogle hideinmobile"
data-ad-client="ca-pub-7XXXXXXXXX2"
data-ad-slot="6XXXXXXX"></ins>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
(adsbygoogle = window.adsbygoogle || [ ]).push({});
</script>


Thank you!

NickMNS

3:27 pm on Jul 23, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Maybe it's just a typo here, but your opening style tag is malformed should be either <style> or <style type="text/css">. Type attribute is not required.

csdude55

6:58 am on Aug 20, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Another option might be:

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
if (screen.width >= 768) {
document.write('<ins class="adsbygoogle"');
document.write(' data-ad-client="ca-pub-7XXXXXXXXX2"');
document.write(' data-ad-slot="6XXXXXXX"></ins>');

(adsbygoogle = window.adsbygoogle || []).push({});
}
</script>


I've been doing something like that for several years, but I measure the height instead of the width and show a banner when there's room for it underneath other content. Still, same general concept.

csdude55

6:59 am on Aug 20, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just realized that was a month ago... sorry about that! Man, the older I get the faster time seems to go by :-(