GalaXee95
GalaXee95

Reputation: 81

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

I'm trying to create a black overlay for a picture featured in the header of my application, using ReactJS and CSS3. However, no matter what I do, it's not working. I've tried referencing old projects in the past, where I managed to pull it off, as well as explored StackOverflow for answers. But, nothing's worked.

Could somebody please help me understand what I'm doing wrong and how I can solve this? I'd really appreciate it.

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

Upvotes: 3

Views: 7242

Answers (2)

user10808726
user10808726

Reputation:

You can give a background image and black overlay together in the css to the "header" or your parent like this:

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

Upvotes: 1

Cat_Enthusiast
Cat_Enthusiast

Reputation: 15688

To create an overlay, you should wrap the content in another div. Then with CSS, position it right in-front of the header/background like so: 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%;
}

Upvotes: 4

Related Questions