一个css问题

有两个透明底(background-color: transparent)的div,我想让其中一个div的内容遮住另一个,用纯css能做到吗

问题:

想要类似的效果:

html代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    .wrapper {
      width: 400px;
      height: 400px;
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      margin: auto;
      border: 1px solid black;
      background: url(https://pc-index-skin.cdn.bcebos.com/hiphoto/57009887230.jpg?x-bce-process=image/crop,x_0,y_0,w_1800,h_1125);
    }

    .wrapper > * {
      position: absolute;
      color: blue;
      background-color: transparent;
      border: 1px solid black;
    }

    .above {
      top: 180px;
      left: 100px;
      z-index: 2;
    }

    .below {
      top: 200px;
      right: 200px;
      z-index: 1;
    }
  </style>
</head>
<body>
<div class="wrapper">
  <div class="above">我在上面我在上面我在上面我在上面我在上面我在上面我在上面我在上面我在上面我在上面</div>
  <div class="below">我在下面我在下面我在下面我在下面我在下面我在下面我在下面我在下面我在下面我在下面</div>
</div>
</body>
</html>
阅读 1.8k
3 个回答

你在above也设置一个和背景图一样的图片,然后利用background-position, 只显示和底部背景图那块重合的区域,就完成效果了。看上去透明的,实际是覆盖的。

image.png
这样的效果你看ok吗
还有个问题是底部的border还是会显示

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    .wrapper {
      width: 400px;
      height: 400px;
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      margin: auto;
      border: 1px solid black;
      background: url(https://pc-index-skin.cdn.bcebos.com/hiphoto/57009887230.jpg?x-bce-process=image/crop,x_0,y_0,w_1800,h_1125);
    }

    .wrapper > * {
      position: absolute;
      color: blue;
      background-color: transparent;
      border: 1px solid black;
    }

    .above {
      top: 180px;
      left: 100px;
      z-index: 2;
    }

    .below {
      top: 200px;
      right: 200px;
      z-index: 1;
    }
    .above-float{
      float: right;
      width: 300px;
      margin-right: -200px;
      margin-top: -20px;
      visibility: hidden;
    }
  </style>
</head>
<body>
<div class="wrapper">
  <div class="above">我在上面我在上面我在上面我在上面我在上面我在上面我在上面我在上面我在上面我在上面</div>
  <div class="below">
    <div class="above-float">我在上面我在上面我在上面我在上面我在上面我在上面我在上面我在上面我在上面我在上面</div>
  <div class="inline">我在下面我在下面我在下面我在下面我在下面我在下面我在下面我在下面我在下面我在下面</div></div>
</div>
</body>
</html>

css应该不行,如果能用canvas的话,可以用globalCompositeOperation。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题