没有不值得去解决的问题,也没有不值得去学习的技术!

Troubleshooting WordPress Archive AdSense availableWidth=0 Error: From Horizontal Scrollbar to Gutenberg Block Structure

图 4:控制台出现 availableWidth=0

Recently, while adjusting the sidebar of my blog’s archive page, I encountered an issue that seemed simple but took a long time to troubleshoot.

On the surface, it appeared to be a mobile horizontal scrollbar issue.

But upon further investigation, I discovered it also involved a Google AdSense error:

Plaintext
Uncaught TagError: adsbygoogle.push() error:
No slot size for availableWidth=0

What made it more interesting was:

When I fixed one of the issues, the other would reappear.

The root cause ultimately identified was neither the CSS nor AdSense, but rather the Gutenberg block structure.


Background

An AdSense ad unit was placed in the right sidebar of the archive page.

The initial ad code was as follows:

HTML
<ins class="adsbygoogle"
     style="display:block; width:450px; max-width:100%;"
     data-ad-client="ca-pub-xxxxxxxxxxxxxxxx"
     data-ad-slot="4220340824"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>

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

The reason for setting:

CSS
width:450px;

was because AdSense kept throwing the error:

Plaintext
No slot size for availableWidth=0

After adding a fixed width, the error disappeared.

But a new problem emerged.


Phase 1: Horizontal scrollbar appears on mobile

Figure 1: Horizontal scrollbar appearing on mobile
Figure 1: Horizontal scrollbar appearing on mobile

While testing on mobile, I noticed the right side of the page could scroll horizontally.

Initially, I suspected:

  • The category dropdown
  • The tag cloud
  • The calendar
  • Custom CSS

I even suspected it was an issue with the Twenty Twenty-Five theme.

To locate the problem, I executed the following in the browser console:

JavaScript
const vw = document.documentElement.clientWidth;

[...document.querySelectorAll('*')]
  .filter(el => el.getBoundingClientRect().right > vw)
  .map(el => ({
    tag: el.tagName,
    class: el.className,
    width: Math.round(el.getBoundingClientRect().width)
  }))

I eventually found that the widths of many elements had become:

Plaintext
450px

Including:

  • The ad
  • The tag cloud
  • The calendar
  • The sidebar content

Continuing to trace upwards, I found that the culprit was exactly this in the ad code:

CSS
width:450px;

Phase 2: Fixing the horizontal scrollbar

So, I modified the ad code to:

Figure 2: Ad code modification
Figure 2: Ad code modification
HTML
<ins class="adsbygoogle"
     style="display:block; width:100%; max-width:100%; box-sizing:border-box;"
     data-ad-client="ca-pub-xxxxxxxxxxxxxxxx"
     data-ad-slot="4220340824"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>

After the modification:

Figure 3: Horizontal scrollbar disappeared
Figure 3: Horizontal scrollbar disappeared

The mobile page returned to normal.

The horizontal scrollbar completely disappeared.


Phase 3: A new problem emerges

However, right after the horizontal scrollbar disappeared,

the previous error reappeared in the console:

Figure 4: availableWidth=0 appearing in the console
Figure 4: availableWidth=0 appearing in the console
Plaintext
Uncaught TagError: adsbygoogle.push() error:
No slot size for availableWidth=0

That is to say:

Plaintext
width:450px
↓
AdSense normal
↓
Mobile abnormal

width:100%
↓
Mobile normal
↓
AdSense error

The two issues seemed impossible to resolve simultaneously.


Phase 4: Starting to suspect the Gutenberg block structure

At this point, I began inspecting the archive page template structure.

The initial sidebar structure was as follows:

Figure 5: Original structure (Stack)
Figure 5: Original structure (Stack)
Plaintext
Column
└── Column
    └── Stack
        ├── Search
        ├── Category List
        ├── Ad (WPCode)
        ├── Tag Cloud
        └── Calendar

During this period, I tried many approaches:

  • Modifying the Column width
  • Adjusting the sidebar ratio
  • Adding a min-width to the ad
  • Adding a min-height
  • Wrapping the ad in a Group
  • Using a Custom HTML block
  • Delaying the execution of adsbygoogle.push()

None of them completely resolved the issue.


Final solution

Later, I tried replacing:

Plaintext
Stack

directly with:

Plaintext
Group

The modified structure is as follows:

Figure 6: Modified structure (Group)
Figure 6: Modified structure (Group)
Plaintext
Column
└── Column
    └── Group
        ├── Search
        ├── Category List
        ├── Ad (WPCode)
        ├── Tag Cloud
        └── Calendar

After saving, I refreshed the page.

The problem disappeared immediately.


Final result

Figure 7: Final result
Figure 7: Final result

Ultimately, it simultaneously satisfied:

1. AdSense no longer throws errors

The console returned to normal:

Plaintext
No slot size for availableWidth=0

no longer appeared.

2. The ad displays normally

Without needing a fixed width:

CSS
width:100%;
max-width:100%;

it loaded properly.

3. No horizontal scrollbar on mobile

The page returned to normal.

4. The sidebar layout restored to natural spacing

Previously, the right side of the calendar was flush against the screen edge.

After the modification:

  • The calendar displays normally
  • The tag cloud displays normally
  • The spacing of the entire sidebar returned to normal

Cause analysis

Although the official WordPress documentation does not explicitly explain the difference between the two.

Based on actual behavior:

Plaintext
Stack

During the layout calculation phase, the width of internal elements might not yet be finalized.

And AdSense’s:

Plaintext
(adsbygoogle = window.adsbygoogle || []).push({});

executed earlier.

Thus leading to:

Plaintext
Ad initialization
↓
Detect available width
↓
availableWidth = 0
↓
Throw exception

Whereas:

Plaintext
Group

does not have this problem.


Troubleshooting summary

The entire troubleshooting process actually went through:

Plaintext
AdSense error
↓
Add width:450px
↓
Error disappears

Mobile horizontal scrollbar appears
↓
Remove width:450px
↓
Horizontal scrollbar disappears

AdSense error again
↓
Troubleshoot Gutenberg structure
↓
Stack → Group
↓
Problem completely resolved

The biggest takeaway this time was not fixing a bug, but rather validating an experience once again:

When page layout anomalies occur, prioritize locating the specific elements and block structure, rather than immediately suspecting the CSS or writing a lot of patch code.

Often, the real problem might just be an incorrect block type selection.

系列导航

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系