【问题标题】:Darken background but not inside contents - React / Ionic /CSS变暗背景但不是内部内容 - React / Ionic /CSS
【发布时间】:2020-08-02 01:52:48
【问题描述】:

我需要使图像的背景变暗,而不是像下图那样将其内部的内容变暗。图像是以编程方式绘制的,所以我不能在 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 reactjs filter background background-color


    【解决方案1】:

    尝试以下方法:

    {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;
    }
    

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

    【讨论】:

    • 你是超级明星@Enchew!!你解决了我的问题!对于尝试使背景变暗而不是内部元素变暗的人,请遵循此建议。
    • 非常欢迎你,我的朋友!我很高兴我能帮上忙!请将答案标记为已接受,以便其他人知道这是解决方案。
    【解决方案2】:

    你用 css 试过了吗?

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

    【讨论】:

      猜你喜欢
      • 2015-09-12
      • 2017-11-10
      • 1970-01-01
      • 2021-09-15
      • 2017-03-07
      • 2019-06-11
      • 2013-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多