11

I am using responsive adsense ads. By default, the text ads are nicely left aligned but the image ads have a very wide left margin for some reason. I can't get rid of it, even if I take the most simple page, i.e. a page with only an ad on it:

<!DOCTYPE html>
<html>
<head>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
</head>
<body>
<!-- Responsive Ad -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="xxx"
     data-ad-slot="xxx"
     data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</body>
</html>

Textual ads are left aligned, e.g.:

enter image description here

but image ads contain some wide left margin, e.g.:

enter image description here

My question is how can I make sure that image ads are left-aligned?

Paul Jansen
  • 1,216
  • 1
  • 13
  • 35
  • I mean, `float: left` doesn't work? If not, do `margin-left: 0 !important` to see if that overwrites the huge margin... – Mingle Li Aug 23 '15 at 22:31
  • @TheJuniorProgrammer, thanks. I tried both of your solutions in the css for "adsbygoogle". When using `float: left;` the ads are not shown any more and `margin-left: 0;` doesn't change anything. So this isn't the right solution. – Paul Jansen Aug 23 '15 at 22:43
  • Could you make a [fiddle](http://jsfiddle.net) or a [pen](http://codepen.io/pen/) or just upload it to a webpage so we can see it? Plus, did you add `!important` to your `margin-left`? – Mingle Li Aug 23 '15 at 22:46
  • @TheJuniorProgrammer, the problem is that I have to use/show my adsense codes then, which is not allowed. – Paul Jansen Aug 23 '15 at 22:47
  • @TheJuniorProgrammer, for your information. I also added `!important`. – Paul Jansen Aug 23 '15 at 22:52
  • Have you used firebug or developer tools and hovered over the image to see if it's actually got margin / padding? – Jay Aug 26 '15 at 08:20
  • @Jay, according to the tools some nested iframe uses `left: 50%`. I can't figure out how to get rid of this. – Paul Jansen Aug 27 '15 at 19:04
  • @PaulJansen Have you solved your problem? – Jay Aug 28 '15 at 11:02
  • @Jay, no see my answer to the question of David Felipe below. – Paul Jansen Aug 28 '15 at 12:45
  • @PaulJansen can you provide your website url where your adsense ads appear?? – Rohit Kumar Sep 02 '15 at 04:01
  • Did you tried my solution below.. just give a try.. http://stackoverflow.com/a/32323867/2006386 – Rajesh kannan Sep 04 '15 at 05:04

11 Answers11

4

Try putting it in a container and give the container a max-width and maybe text-align: left.

<!DOCTYPE html>
<html>
<head>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
</head>
<body>
<!-- Responsive Ad -->
<div id="ad" style="width: 100%; max-width: 600px; text-align: left;">
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="xxx"
     data-ad-slot="xxx"
     data-ad-format="auto"></ins>
</div>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</body>
</html>

Hard to solve without having a test page that you can try playing with.

Jacob
  • 1,933
  • 2
  • 20
  • 30
2

Try this - put your adsense code in a div, and style the div like this:

<div style="margin: 5px !important; float: left !important;"> 
    --Put your AdSense ad code here --
</div>
Mingle Li
  • 1,322
  • 1
  • 15
  • 39
2

Maybe you can try to identify the DOM element that adsense create with the chrome developer tools(or try with the class in your ins tag) and in your code add some javascript to adjust the adsense as you wish for example you can do:

document.getElementsByClassName('adsbygoogle').style.cssFloat = "left";

if you prefer jquery you can do something like

$('.adsbygoogle').css('float','left');
  • Thanks David. We are getting close. According to the tools an iframe is used containing ``. If I change `left: 50%` to `left: 0%` and `margin-left: -364px` to `margin-left: 0px` in the tooling it works perfectly. The only thing that remains to be done is getting this work in my code. How? – Paul Jansen Aug 27 '15 at 19:08
  • You could detect when the ifram have content $(function(){ $('#ad_iframe').ready(function(){$('.adsbygoogle').css('float','left'); }); } and add the code that you want to fire in the callback – David Felipe Camargo Polo Aug 28 '15 at 21:13
  • I tried your solution, but unfortunately the ad is not shown any more then. I am afraid Google has made sure that it is impossible to manipulate their ads in whatever way. So I give up and just use the ads as they are provided. Thanks for your help. – Paul Jansen Aug 30 '15 at 22:41
2

To limit the size and alignment of your ad, it needs to be in a container element such as a paragraph or div. Assign a class to the container element, put a max-width limit on the container for the responsive ad and left align the contents, all in your css.

codehelper
  • 29
  • 1
2

I have seen your webpage. And I have applied the following style

Try the following css in your file

.container {
padding-left:0px;
margin-left:10px;
}

Let me know if it is helpful

Rajesh kannan
  • 624
  • 3
  • 16
0

Create a table with one row and two columns (or a div with display:table; propriety). Put your code inside the first cell. I think only single ads in a large block are centred. This example shows you how I see this:

<table width="100%" height="90" border="0" align="center">
    <tbody>
    <tr>
        <td>
            <ins class="adsbygoogle"
            style="min-width:400px;max-width:970px;width:100%;height:90px;left:0;margin-left:0;"
            data-ad-client="xxx"
            data-ad-slot="xxx"></ins>
            <script>
            (adsbygoogle = window.adsbygoogle || []).push({});
            </script>
        <td>
        <td>
        </td>
        </tr>
    </tbody>
</table>
Madalina Taina
  • 1,968
  • 20
  • 26
0

This seems to work for me, what do you think?

style="display:inline-block;max-width:728px;width:100%

Must add I'm a bit of newbie at any sort of coding, so please correct me if I'm wrong.

Liam
  • 27,717
  • 28
  • 128
  • 190
Gary
  • 1
0

If you are using wordpress, you can do this:

<div class="alignleft">
<! --- put your adsense code here -->
</div>

Works for me

0

This works. The key is to use flex and justify-content:left which packs the adsense ad to the left. Replace the XXX with your adsense publisher ID and the YYY with your ad slot. You may also want to wrap this code in divs for a container, row and column.

<ins class="adsbygoogle" style="display:block;display:flex;justify-content:left;" data-full-width-responsive="true"
data-ad-client="ca-pub-XXX" data-ad-slot="YYY" data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
Robert Stevens
  • 519
  • 5
  • 9
-1

Original Ad

Custom Ad

If you are looking for the result as shown in the 2nd image from the first image, I can think of only below solution:

    <!DOCTYPE html>
    <html>
       <head>
         <script async       src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
       </head>
       <body>
   <!-- Responsive Ad -->
          <div class = "adClass" style = "float:left;">
             <div id="ad" style="width: 100%; max-width: 600px; text-align: left;">
             <ins class="adsbygoogle"
               style="display:block"
               data-ad-client="xxx"
               data-ad-slot="xxx"
               data-ad-format="auto"></ins>
             </div>
           </div>
           <div class="yourContent" style = "float:right">
           <span>Your content here.</span>
           </div> 
          <script>
          (adsbygoogle = window.adsbygoogle || []).push({});
          </script>
         </body>
       </html>
-1

The only thing that works for me is:

<div class="admaxwidth">
    [your responsive ad code]
</div>

and in your .css:

.admaxwidth {max-width:728px;}
Michiel
  • 1
  • 1