React 应用程序:如何为图片创建黑色叠加层?

React Application: How To Create A Black Overlay For A Picture?

我正在尝试使用 ReactJS 和 CSS3 为我的应用程序 header 中的图片创建黑色叠加层。但是,无论我做什么,它都不起作用。我曾尝试参考过去的旧项目,在那里我设法实现了它,并探索了 Whosebug 以寻求答案。但是,没有任何效果。

有人可以帮助我了解我做错了什么以及如何解决这个问题吗?非常感谢。

Header.js

import React from 'react';
// import { Link } from 'react-router-dom';
import Button from 'react-bootstrap/Button';
import ButtonToolBar from 'react-bootstrap/ButtonToolbar';
import './header.css';

const Header = () => {
    return (
        <div className="header">
            <h1 className="title">Linguist Otaku</h1>
            <ButtonToolBar>
                <Button href="/quiz" size="lg" id="quiz-btn">
                    Quiz Now
                </Button>
            </ButtonToolBar>
        </div>
    )
}

export default Header;

Header.css

:root{
    --mainOpacity: rgb(0, 0, 0, .55);
}

.header {
    background-image: url("../../images/italy.png");
    opacity: var(--mainOpacity);
    height: 30em !important;
}

h1 {
    font-family: "Fjalla One";
    text-align: center !important;
    padding-top: 2em !important;
    color: white !important;
    font-size: 4em !important;
}

.btn-toolbar {
    display: flex !important;
    justify-content: center !important;
}

#quiz-btn {
    font-family: "Roboto";
    color: white !important;
    margin-top: 3em !important;
    background-color: transparent !important;
    border: solid .05em white !important;
    border-radius: 0em !important;
}

#quiz-btn:hover {
    transition: all 0.5s ease-in-out;
        background-color: var(--mainOpacity) !important;
}

要创建叠加层,您应该将内容包裹在另一个 div 中。然后用 CSS,将它放在 header/background 的 in-front 右边,像这样:https://codesandbox.io/s/mystifying-ramanujan-sq491

Header.js

const Header = () => {
  return (
    <div className="header">
      <div className="dark-overlay">
        <h1 className="title">Linguist Otaku</h1>
        <ButtonToolBar>
          <Button href="/quiz" size="lg" id="quiz-btn">
            Quiz Now
          </Button>
        </ButtonToolBar>
      </div>
    </div>
  );
};

CSS

.dark-overlay {
  background-color: rgba(0, 0, 0, 0.35);
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

您可以在 css 中为 "header" 或您的 parent 提供背景图像和黑色叠加层,如下所示:

background-image: linear-gradient( rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7) ),url("../../images/italy.png");