【问题标题】:how can I have background-image underneath background-color? [duplicate]我怎样才能在背景颜色下面有背景图像? [复制]
【发布时间】:2019-08-05 20:14:39
【问题描述】:

我想知道是否有办法在背景颜色后面放置背景图像。

我有这个带滑块的颜色选择器,当我在 Alpha 通道上迭代时,我希望方格图像显示在颜色下 我想我可以在 html 中添加图像,然后将其放置在 div 下,但是没有“更清洁”的解决方案而无需添加position: absolute;?如果可能的话,我真的想避免这种情况

我尝试使用::before::after,但到目前为止没有成功,图像不会显示。我尝试使用content: url(""),但它始终保持在顶部,并且似乎无法使用z-index 将其置于下方

let red = document.querySelector("#red");
let green = document.querySelector("#green");
let blue = document.querySelector("#blue");
let alpha = document.querySelector("#alpha");
let paletteColor = document.querySelector(".paletteColor")
paletteColor.style.backgroundColor = `rgba(${red.value}, ${green.value}, ${blue.value}, ${alpha.value/100})`

red.addEventListener("input", rgbaValue);
green.addEventListener("input", rgbaValue);
blue.addEventListener("input", rgbaValue);
alpha.addEventListener("input", rgbaValue);

function rgbaValue() {
  let rgba = `rgba(${red.value}, ${green.value}, ${blue.value}, ${alpha.value/100})`
  paletteColor.style.backgroundColor = rgba
}
.palette {
  border: 3px solid black;
}

.sliders {
  list-style: none;
  left: 80px;
  display: inline-block;
}

.paletteColor {
  height: 100px;
  width: 100px;
  display: inline-block;
}

.paletteColor::after {
  height: 100px;
  width: 100px;
  display: inline-block;
  background-color: black;
}

.paletteColor::before {
  height: 100px;
  width: 100px;
  display: inline-block;
  background-image: url("./checkered.jpg");
}
<div class="palette">
  <div class="paletteColor"></div>
  <ul class="sliders">
    <li><input id="red" type="range" min="0" max="255" value="150"></li>
    <li><input id="green" type="range" min="0" max="255" value="222"></li>
    <li><input id="blue" type="range" min="0" max="255" value="22"></li>
    <li><input id="alpha" type="range" min="0" max="100" value="100"></li>
  </ul>
  <div>

【问题讨论】:

  • IMO 绝对定位的背景棋盘图像是这里最简单的解决方案
  • 问题是图像不会被看到,因为背景颜色会不透明......
  • 这篇文章可能会有所帮助stackoverflow.com/questions/8195215/…

标签: html css


【解决方案1】:

您可以将::before 与背景颜色一起使用。

div {
  position: relative;
  width: 100px;
  height: 100px;
  background-image: url(//img.pranavc.in/100)
}

div::after {
  position: absolute;
  content: '';
  background-color: rgba(100, 0, 1, .5);
  width: 100px;
  height: 100px;
}
&lt;div&gt;&lt;/div&gt;

或者更好的替代方法是将图像放在::before 中,然后减少它的z-index 值,这使得 div 可以访问。

div {
  position: relative;
  width: 100px;
  height: 100px;
  background-color: rgba(100, 0, 1, .5);
}

div::after {
  position: absolute;
  content: '';
  width: 100px;
  height: 100px;
  background-image: url(//img.pranavc.in/100);
  z-index: -1;
}
&lt;div&gt;&lt;/div&gt;

【讨论】:

    【解决方案2】:

    #img-wrapper {
      background: red;
    }
    
    img {
      width: 100%;
      opacity: 0.5;
      vertical-align: middle;
    }
    &lt;div id="img-wrapper"&gt;&lt;img src="https://images.freeimages.com/images/large-previews/25d/eagle-1523807.jpg" alt=""&gt;&lt;/div&gt;

    这会在实际图像上添加叠加层。

    【讨论】:

      【解决方案3】:

      好的,我发现如果我将位置设置为绝对位置并使用z-index 进行操作,它可以工作,唯一的问题是我使用的是content 而不是background-color

      .paletteColor{
          height: 100px;
          width: 100px;
          display: inline-block;
          z-index: 0;
      }
      
      .paletteColor::before{
          height: 100px;
          width: 100px;
          display: inline-block;
          position: absolute;
          z-index: -1;
          content: url("./checkered.png");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-20
        • 1970-01-01
        • 1970-01-01
        • 2019-01-19
        • 2019-02-28
        • 1970-01-01
        • 2017-12-29
        相关资源
        最近更新 更多