CSS - 带有一些透明像素的图像周围的背景颜色

标签 css background background-image

我有一个固定大小的图像,其中一部分是透明的,我目前将其用作 DIV 背景。我必须将图像精确地放在屏幕中心

问题是我需要图像周围的所有空间都是黑色的。如果我将其设置为 background: black url(... - 图像的透明像素也会变成黑色,这是错误的。

另一个问题是网站内容是动态的,所以我不能用 2 个具有相同背景图像和不同过滤器的 DIV 作弊。我需要清楚地透过透明像素。

我当前的代码:

<html>
<head>
    <style>
        .image {
            height: 100%;
            background: black url(image.png) center center no-repeat;
            background-size: auto 100%;
        }   
    </style>
</head>

<body>
    <div class="image"></div>
</body>
</html>

简而言之——我需要通过图像的透明部分来查看内容,屏幕的另一部分必须是黑色的。我如何实现这一点?

最佳答案

我建议使用巨大框阴影;

body {
  background: pink;
  display: flex;
  height: 100vh;
}

.wrap {
  height: 200px;
  width: 200px;
  border: 1px solid green;
  margin: auto;
  background-image: url(https://www199.lunapic.com/editor/premade/transparent.gif);
  background-size: 200px;
  background-repeat: no-repeat;
  background-position: center;
  padding: 20px;
  box-shadow: 0 0 0 2000px red;
}
<div class="wrap"></div>

关于CSS - 带有一些透明像素的图像周围的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50682993/

相关文章:

javascript - 制作手机键盘有困难

objective-c - 没有位置服务的 iOS 后台轮询

ios - UITabBar 背景图像的图像缩放

Webpack 没有加载背景图片

javascript - 将视差滚动效果应用于多个背景,每个背景以不同的速度滚动

html - 链接在 Materialise 选项卡中不起作用

css - 关于 .well 类中 Bootstrap 图像绝对位置的问题

html - 我正在创建一个 chrome 应用程序来编辑纯文本,我正在使用 &lt;textarea&gt; 来完成此操作。它不会全尺寸

css - 居中的 DIV,两边有不同的背景

css - IE 边框图像和背景图像框问题