css如何写body背景图被正文盖住,body文字一直在背景特定位置,且所有浏览器位置固定

本文介绍了一个CSS技巧,如何设置body背景图使其在正文下方,并确保文字始终覆盖在背景图的特定位置,同时该效果在各浏览器中保持一致。通过使用绝对定位、z-index和百分比计算,创建了一个100px高的header,其背景图居中并固定,文字位于右下角。主要内容包括HTML结构和相应的CSS样式代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果:


代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>


 <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <style>
  *{  margin:0;  padding:0 }


  a{  text-decoration: none; }
  img{
    border: none;
  }
  .background{
     height: 200px;
     position: absolute;
     width: 100%;
     background: url(一张width大于900px,height大于100px的banner图)  50% 0 no-repeat;
     z-index: -1;

      left:0;

     right:0;
  }
  .back{
     height: 100px;
     position: relative;
     width: 900px;
     margin: 0 auto;
  }
  .back .content{
     bottom: 20px;
     font-size: 24px;
     color: orange;
     position: absolute;
     right: 20px;
  }
  .main{
      margin:0 auto;
      width:900px;
      background: gray;
      height: 900px;
  }
  </style>
</head> 
<body>
    <div class="background">
    </div>
    <div class="back">
      <a href="" target="_blank"><div class="content">
        返回首页
      </div>
      </a>
    </div>
    <div class="main">文章内容</div>
<body>
</html>
知识点:

百分比的计算方法:
x=(容器宽度-背景图片宽度)*百分数
y=(容器高度-背景图片高度)*百分数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值