在CSS蒙版后面添加图像

时间:2018-12-27 03:28:17

标签: html css

如何使背景图片看起来像在手机上显示的那样?因此,我将iPhone蒙版了,我想在手机屏幕上放置图像。我该怎么做呢?我尝试使用z-index,但是它不起作用。这就是我要实现的enter image description here

div{
  width: 302px;
  height: 605px;
  background-image: url(https://www.apple.com/v/iphone-xs/d/images/overview/hardware_display_iphonexsmax_gold_portrait_large_2x.jpg);
  background-size: 302px 605px;
  background-repeat: no-repeat;
  -webkit-mask-size: 302px 605px;
  -webkit-mask-image: url(https://www.apple.com/v/iphone-xs/d/images/overview/hardware_display_iphonexsmax_gold_portrait_mask_large.svg);
  -webkit-mask-repeat: no-repeat;
  position: relative;
  z-index: 2;
}

.background{
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  width: 265px;
  height: 571px;
  background-image: url(https://www.apple.com/v/iphone-xr/d/images/overview/screen_display_iphonexr_large_2x.jpg);
  background-size: 265px 571px;
}
<div>
</div>

<div class="background"></div>

1 个答案:

答案 0 :(得分:1)

您遇到的问题是,您应用于手机的div样式也正在应用于.background。通过为手机提供单独的课程,您可以分别设置其样式。

我在下面更新了您的示例。

    .phone {
  width: 302px;
  height: 605px;
  background-color: white;
  background-image: url(https://www.apple.com/v/iphone-xs/d/images/overview/hardware_display_iphonexsmax_gold_portrait_large_2x.jpg);
  background-size: 302px 605px;
  background-repeat: no-repeat;
  -webkit-mask-size: 302px 605px;
  -webkit-mask-image: url(https://www.apple.com/v/iphone-xs/d/images/overview/hardware_display_iphonexsmax_gold_portrait_mask_large.svg);
  -webkit-mask-repeat: no-repeat;
  position: absolute;
  z-index: 2;
}

.background {
  position: absolute;
  top: 20px;
  left: 20px;
  z-index: 1;
  width: 280px;
  height: 580px;
  border-radius: 10px;
  background-image: url(https://www.apple.com/v/iphone-xr/d/images/overview/screen_display_iphonexr_large_2x.jpg);
  background-size: cover;
}
<div class="phone"></div>

<div class="background"></div>