使背景变暗但不使内部内容变暗 - React / Ionic /CSS

Darken background but not inside contents - React / Ionic /CSS

我需要使图像的背景变暗,而不是像下图那样使图像内部的内容变暗。该图像是以编程方式绘制的,所以我不能在 css.

中包含它的 url

下面应该是这样的:

现在它如何寻找我,我需要更白的角色:

我使用 ::before 或 ::after 查看了不同的答案,但考虑到我的图像是内联渲染的,它不起作用。在我的代码下面。

REACT.TSX

 {apartments.map(({ name, images, taskStatus }: any, index: number) => (
            <Link key={index} to={`/apartments/${index}`}>
              <div
                className="apartmentImage"
                style={{
                  backgroundImage: `url(${API_IMAGE}${images[0]})`,
                }}
              >
                <div
                  className="center ion-margin-top"
                  style={{ width: "100%" }}
                >
                  <h5 className="apartmentText">{name}</h5>
                </div>
                <div
                  className="center ion-margin-top"
                  style={{ width: "100%" }}
                >
                  <h6 className="subApartmentText">MALAGA</h6>
                </div>
              </div>
            </Link>
          ))}

CSS:

.apartmentImage {
  width: 98%;
  margin-left: 1%;
  height: 24.7vh;
  border-radius: 10px;
  margin-top: 3%;
  margin-bottom: -1%;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  filter: brightness(0.8);
}

.apartmentText {
  color: white;
  font-weight: bold;
}

知道要做什么吗?

非常感谢!

你用 css 试过吗?

background-image: linear-gradient( rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3) ),

尝试以下操作:

{apartments.map(({ name, images, taskStatus }: any, index: number) => (
            <Link key={index} to={`/apartments/${index}`}>
              <div
                className="apartmentImage"
                style={{
                  backgroundImage: `url(${API_IMAGE}${images[0]})`,
                }}
              >
                <div
                  className="center ion-margin-top"
                  style={{ width: "100%" }}
                >
                  <h5 className="apartmentText">{name}</h5>
                </div>
                <div
                  className="center ion-margin-top"
                  style={{ width: "100%" }}
                >
                  <h6 className="subApartmentText">MALAGA</h6>
                <div className='color-overlay'/>

                </div>
              </div>
            </Link>
          ))}

和CSS:

.apartmentImage {
  width: 98%;
  margin-left: 1%;
  height: 24.7vh;
  border-radius: 10px;
  margin-top: 3%;
  margin-bottom: -1%;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  position: relative;

}

.apartmentText {
  color: white;
  font-weight: bold;
}


.apartmentText, .subApartmentText {
  position: relative;
  z-index: 1;
}

.color-overlay{
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background-color: rgba(0,0,0,.3);
  pointer-events: none;
}

我试过了,它对我有用,但我在本地做了一些更改,所以我可以加载图像。如果有什么问题请告诉我,这样我可以仔细检查我是否正确复制了所有代码。