html - <div> 背景图片透明但不是文字

标签 html css transparency

<分区>

我正在尝试创建一个 <div>作为 opencart 中 HTML 模块的一部分,具有缓慢平移的透明背景。作为一个模块,样式必须是独立的。

我目前拥有的:

<style>
@keyframes backgroundScroll {
0% { background-position: 50% 50%; }
33% { background-position: 1% 50%; }
40% { background-position: 1% 50%; }
66% { background-position: 99% 50%; }
75% { background-position: 99% 50%; }
100% { background-position: 50% 50%; }}
/* added pauses to each edge of the image */

#mtn-scroll {
width: 800px;
height: 100%;
background: url(coolpanoramapicture.jpg) no-repeat; opacity:0.1;
background-size: 250%;
background-position: 50% 50%;
animation: backgroundScroll 60s ease-in-out 1s infinite; }
</style>

<div id="mtn-scroll">
  <div>
  Content text goes here!
  </div>
</div>

我想要的是不透明度也不会影响文本。我听到了关于使用 ::before 的铃声,但我不知道锤子卡在哪里。我在做些什么吗?

有谁知道如何只使背景图像透明,而不必借助透明的 PNG 文件进行欺骗?

最佳答案

您使用 ::before 伪元素走在正确的轨道上。以下是实现方法:

html,
body {
  height: 100%;
}

#mtn-scroll {
  width: 800px;
  height: 100%;
  position: relative;
}

#mtn-scroll::before {
  background: url('https://i.imgur.com/4HLnakJ.jpg') no-repeat;
  opacity: 0.1;
  background-size: 250%;
  background-position: 50% 50%;
  animation: backgroundScroll 60s ease-in-out 1s infinite;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 0;
  content: '';
}

#mtn-scroll div {
  position: relative;
}

@keyframes backgroundScroll {
  0% {
    background-position: 50% 50%;
  }
  33% {
    background-position: 1% 50%;
  }
  40% {
    background-position: 1% 50%;
  }
  66% {
    background-position: 99% 50%;
  }
  75% {
    background-position: 99% 50%;
  }
  100% {
    background-position: 50% 50%;
  }
}
<div id="mtn-scroll">
  <div>
    Content text goes here!
  </div>
</div>

关于html - <div> 背景图片透明但不是文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49564612/

上一篇:css - Font Awesome 5 在使用 JS+SVG 版本时显示空方 block

下一篇:html - Bootstrap 高级下拉搜索

相关文章:

python - Pygame 或 Python 中的透明窗口

javascript - 使用背景图片在响应式父 div 上叠加 div

css - 设置具有透明度的文本背景颜色

html - 底部 Bootstrap 带有文本的响应式图像

html - R Shiny - 垂直对齐标签并以水平形式输入

html - 绝对div放在div之上

html - 使用 col-push 和 col-pull bootstrap 定位元素 3

python - 如何转换图像的背景颜色以匹配 Pygame 窗口的颜色?

javascript - 如何防止子 div 中 anchor 的链接影响主 url?

html - 定位表单标签