html - 背景颜色和图像之间的 CSS 淡入淡出

标签 html css

我用一些 Tiles 制作了一个简单的 CSS Grid,目前,我正在尝试设置这些 tiles,以便当您将鼠标悬停在它们上方时出现背景,我尝试了过渡标签,但它不起作用。尝试了一些来自 Web 的解决方案,但它们不适用于网格系统。我正在尝试在图像出现之前添加一点延迟。

.box1 {
  background: #6fc3df;
  grid-area: 1 / 1 / 2 / 3;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.box1 h1 {
  margin-left: 1em;
}

.box1:hover {
  background-image: url("images/pic02.jpg");
}
<div class="tiles">
  <div class="box box1">
    <h1>Test</h1>
  </div>
  <div class="box box2">
    <h1>Test</h1>
  </div>
</div>

最佳答案

只是一个小技巧。

包装 div,它需要用预期的背景淡化另一个 div,并在鼠标悬停在其上时使 child 的背景透明。

注意:OP 想用多个具有不同图像的 div 来做到这一点。所以这是一个包含两个 div 的示例。

#wrapper-1{
  background: url("https://via.placeholder.com/100x50") rgba(111, 195, 223, 1);
}

#wrapper-2{
  background: url("https://via.placeholder.com/200x100") rgba(111, 195, 223, 1);
}

.box1, .box2 {
    grid-area: 1 / 1 / 2 / 3 ;
    display: flex;
    flex-direction: column;
    justify-content: center;
    transition: background 0.5s ease;
    background: rgba(111, 195, 223, 1);
}

.box1 h1, .box2 h1{
    margin-left: 1em;
}

.box1:hover, .box2:hover{
  background: rgba(111, 195, 223, 0);
}
<div class="tiles">
    <div id="wrapper-1">
      <div class="box box1">
        <h1>Test</h1>
      </div>
    </div>
    <div id="wrapper-2">
      <div class="box box2">
          <h1>Test</h1>
      </div>
    </div>
</div>

关于html - 背景颜色和图像之间的 CSS 淡入淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58218776/

相关文章:

html - 使用 anchor 标记调用 Action

javascript - jQuery 保持其他框未编辑状态

css - 仅在焦点上输入自动填充背景黄色(谷歌浏览器)

html - Flexbox align-self 被忽略

php - 可调整大小的表格区域 PHP

html - 向 HTML/CSS 中的部分添加背景颜色时出错

javascript - 如何以 plist 格式显示输出。可能还是不可能?文本区域中的数据也应该能够下载?

javascript - 过渡效果不起作用jquery

CSS 更改 <path> 的 d 属性

css - 黑线穿过盒子