我正在尝试使用 ReactJS 和 CSS3 为我的应用程序标题中的图片创建黑色叠加层。但是,无论我做什么,它都不起作用。我曾尝试引用过去的旧元素,在那里我设法实现了它,并探索了 StackOverflow 以寻求答案。但是,没有任何效果。
有人可以帮助我了解我做错了什么以及如何解决这个问题吗?我真的很感激。
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,将其定位在页眉/背景的正前方,如下所示: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 - react 应用程序 : How To Create A Black Overlay For A Picture?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56963074/