首页 > 解决方案 > 如何将背景大小应用于背景颜色?

问题描述

我有一个图像列表,在悬停时,它会显示一个也有背景颜色的文本。我现在面临的问题是背景颜色仅填充文本。我想要它做的是跨越整个图像并设置高度。这可能吗?

#pictures {
  line-height: 0;
  letter-spacing: 0;
  font-size: 0;
}

#pictures li {
  display: inline-block;
  width: 33%;
  margin: 0;
  padding: 10px;
  text-align: center;
}

.imageText {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: black;
  padding: 15px;
}

.hoverelement:hover .imageText {
  background-color: rgba(235, 242, 238, .8);
  background-repeat: no-repeat;
  font-size: 24px;
}
<ul id="pictures">

  <li>
    <div style="position: relative">
      <a href="/" class="hoverelement"><img src="1.jpg" height="250" width="250">
        <p class="imageText">TEXT</p>
      </a>
    </div>
  </li>

</ul>

我的意思的例子: 在此处输入图像描述

标签: htmlcss

解决方案


#pictures li {
  display: inline-block;
  margin: 0;
  padding: 10px;
  text-align: center;
}

.imageText {
  position: absolute;
  top: 37.5%; /* (100% - height) / 2 */
  left: 10px;
  width: 100%;
  height: 25%;
  background-color: rgba(235, 242, 238, .8);
  color: black;
  font-size: 24px;
  margin: -10px; /* (-1 * padding of the li) */
  padding: 0px;
  display: none; /* hide */
}

.imageText:before { /* vertically centers the text within the grey bar */
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}

.hoverelement:hover .imageText {
  display: initial;
}
<ul id="pictures">

  <li>
    <div style="position: relative">
      <a href="/" class="hoverelement"><img src="https://picsum.photos/250" height="250" width="250">
        <p class="imageText">TEXT</p>
      </a>
    </div>
  </li>

</ul>


推荐阅读