html中设置一段广告scripts脚本居中显示

本文来源:https://blog.csdn.net/weixin_35698659/article/details/117861461

办法1 – 直接在AdSense代码中修改。示例:

style="display:inline-block;width:300px;height:250px"data-ad-client="ca-pub-xxxxxxxxxxxxxxxx"data-ad-slot="xxxxxxxxxx">
(adsbygoogle = window.adsbygoogle || []).push({});
修改为
style="display:block;width:300px;height:250px;margin:auto"data-ad-client="ca-pub-xxxxxxxxxxxxxxxx"data-ad-slot="xxxxxxxxxx">
(adsbygoogle = window.adsbygoogle || []).push({});

其中display:inline-block改为display:block、增加margin:auto,就可以让广告居中。
修改前广告居左
修改后广告居中
方法2 – 修改外围div的CSS。示例:
amp广告本身不能带css,也只有修改外围,原amp广告代码外面添加一个div:

type="adsense"
data-ad-client="ca-pub-xxxxxxxxxxxx"
data-ad-slot="xxxxxxxxxx"
>

再在CSS文件中定义这个

.left_column {
display: flex;
justify-content: center;
}

广告就可以居中了,这个办法其实比第一个办法更通用,对amp广告或者普通web广告在现在流行的浏览器中都有效,只是在IE9等很老的浏览器无效。

滚动至顶部