使用position: absolute配合right和bottom可固定广告在右下角,通过z-index确保层级优先,添加关闭功能和响应式设计提升用户体验。

要制作一个悬浮广告,使用CSS的position: absolute配合right和bottom属性是最常见且有效的方法。这种方式可以让广告固定在容器或页面的右下角,不随内容滚动而移动(如果希望随页面滚动,则保持默认流布局行为即可)。
使用 absolute 定位 可以让元素脱离正常文档流,并相对于最近的已定位祖先元素进行定位。如果没有这样的祖先,则相对于初始包含块(通常是视口)。
通过设置 right: 20px; 和 bottom: 20px;,可以将广告固定在容器或页面右下角,并留出一定间距。
<div class="ad-banner">
<span class="close-btn">✕</span>
<p>限时优惠!立即领取福利</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/cb6835dc7db1" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">前端免费学习笔记(深入)</a>”;</p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/2373">
<img src="https://img.php.cn/upload/ai_manual/001/246/273/176239923777053.png" alt="论小文">
</a>
<div class="aritcle_card_info">
<a href="/ai/2373">论小文</a>
<p>可靠的论文写作助手,包含11种学术写作类型,万字论文一键生成,可降重降AIGC,参考文献真实可标注,图表代码均可自定义添加。</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="论小文">
<span>431</span>
</div>
</div>
<a href="/ai/2373" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="论小文">
</a>
</div>
</div>
.ad-banner {
position: absolute;
right: 20px;
bottom: 20px;
width: 250px;
height: 150px;
background-color: #ff6b6b;
color: white;
border-radius: 8px;
padding: 15px;
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
font-family: Arial, sans-serif;
text-align: center;
z-index: 1000; /* 确保悬浮在其他内容之上 */
}
<p>.close-btn {
float: right;
cursor: pointer;
font-weight: bold;
}</p>为了让悬浮广告体验更好,注意以下几点:
例如添加简单的关闭功能:
document.querySelector('.close-btn').onclick = function() {
document.querySelector('.ad-banner').style.display = 'none';
}
基本上就这些。利用 position: absolute + right + bottom 组合,就能轻松实现右下角悬浮广告,关键是定位准确、层级清晰、用户体验友好。
以上就是CSS定位如何制作悬浮广告_absolute和right bottom组合的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号